import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
public class Main extends JFrame implements ActionListener,
ItemListener {
public static JTextArea tA = new JTextArea("", 20, 50);
JScrollPane scroller = new JScrollPane(tA,
ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
JMenuBar mB = new JMenuBar();
JMenu fM = new JMenu("File");
JMenuItem newFile = new JMenuItem("New");
JMenuItem openFile = new JMenuItem("Open...");
JMenuItem saveFile = new JMenuItem("Save...");
JMenuItem exit = new JMenuItem("Exit");
JMenu eM = new JMenu("Edit");
JMenuItem cut = new JMenuItem("Cut");
JMenuItem copy = new JMenuItem("Copy");
JMenuItem paste = new JMenuItem("Paste");
JMenuItem selectAll = new JMenuItem("Select All");
JMenuItem time = new JMenuItem("Time & Date");
JMenu foM = new JMenu("Format");
JCheckBoxMenuItem wordWrap = new JCheckBoxMenuItem("Word Wrap", true);
JMenuItem font = new JMenuItem("Fonts...");
public static void getTime() {
String time;
Calendar now = Calendar.getInstance();
int hour = now.get(Calendar.HOUR_OF_DAY);
int minute = now.get(Calendar.MINUTE);
int month = now.get(Calendar.MONTH) + 1;
int day = now.get(Calendar.DAY_OF_MONTH);
int year = now.get(Calendar.YEAR);
String monthName = "";
switch (month) {
case (1):
monthName = "January";
break;
case (2):
monthName = "February";
break;
case (3):
monthName = "March";
break;
case (4):
monthName = "April";
break;
case (5):
monthName = "May";
break;
case (6):
monthName = "June";
break;
case (7):
monthName = "July";
break;
case (8):
monthName = "August";
break;
case (9):
monthName = "September";
break;
case (10):
monthName = "October";
break;
case (11):
monthName = "November";
break;
case (12):
monthName = "December";
}
time = monthName + " " + day + ", " + year + " "
+ hour + ":" + minute;
tA.append(time);
}
public Main() {
super("nText 0.0.5");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(Frame.MAXIMIZED_VERT, Frame.MAXIMIZED_HORIZ);
newFile.setToolTipText("Create a New File");
openFile.setToolTipText("Open a File");
saveFile.setToolTipText("Save Current File");
exit.setToolTipText("Exit nText");
copy.setToolTipText("Copy Selected Text");
paste.setToolTipText("Paste Text");
cut.setToolTipText("Cut Current Text");
time.setToolTipText("Add Time & Date");
wordWrap.setToolTipText("");
font.setToolTipText("Change Current Font");
newFile.addActionListener(this);
openFile.addActionListener(this);
saveFile.addActionListener(this);
exit.addActionListener(this);
copy.addActionListener(this);
paste.addActionListener(this);
cut.addActionListener(this);
selectAll.addActionListener(this);
time.addActionListener(this);
wordWrap.addItemListener(this);
font.addActionListener(this);
eM.add(copy);
eM.add(paste);
eM.add(cut);
eM.addSeparator();
eM.add(selectAll);
eM.add(time);
fM.add(newFile);
fM.add(openFile);
fM.add(saveFile);
fM.addSeparator();
fM.add(exit);
foM.add(wordWrap);
foM.add(font);
mB.add(fM);
mB.add(eM);
mB.add(foM);
tA.setWrapStyleWord(true);
tA.setLineWrap(true);
BorderLayout bord = new BorderLayout();
setLayout(bord);
JPanel pane = new JPanel();
pane.add(scroller, BorderLayout.CENTER);
add(pane);
setJMenuBar(mB);
setVisible(true);
pack();
}
public void itemStateChanged(ItemEvent ie) {
Object itemSource = ie.getSource();
if (itemSource == wordWrap) {
if (wordWrap.getState() == true) {
tA.setLineWrap(true);
} else if (wordWrap.getState() == false) {
tA.setLineWrap(false);
}
}
}
public void actionPerformed(ActionEvent evt) {
Object source = evt.getSource();
if (source == exit) {
System.exit(0);
} else if (source == saveFile) {
new saveFileClass();
} else if (source == openFile) {
new openFileClass();
} else if (source == newFile) {
tA.setText("");
} else if (source == copy) {
tA.copy();
} else if (source == paste) {
tA.paste();
} else if (source == cut) {
tA.cut();
} else if (source == selectAll) {
tA.selectAll();
} else if (source == time) {
getTime();
} else {
}
}
public static void main(String[] args) {
Main mW = new Main();
}
}
class openFileClass {
openFileClass() {
final JFileChooser fc = new JFileChooser();
int returnVal = fc.showOpenDialog(fc);
if (returnVal == JFileChooser.APPROVE_OPTION) {
Main.tA.setText("");
try {
Scanner scan = new Scanner(new FileReader(fc.getSelectedFile().getPath()));
while (scan.hasNext())
Main.tA.append(scan.nextLine() + "\n");
}
catch (Exception ex) {
}
}
}
}
class saveFileClass {
saveFileClass() {
final JFileChooser fc = new JFileChooser();
int returnVal = fc.showSaveDialog(fc);
if (returnVal == JFileChooser.APPROVE_OPTION) {
try {
BufferedWriter out = new BufferedWriter(new FileWriter(fc.getSelectedFile().getPath()));
out.write(Main.tA.getText());
out.close();
}
catch (Exception ex) {
}
}
}
}
Resizing Components with the Window
Page 1 of 16 Replies - 8731 Views - Last Post: 28 October 2008 - 04:35 PM
#1
Resizing Components with the Window
Posted 28 October 2008 - 05:07 AM
I've been working on a simple text editor but I can't seem to figure out how to resize the JTextArea when the window resizes. Here's the source:
Replies To: Resizing Components with the Window
#2
Re: Resizing Components with the Window
Posted 28 October 2008 - 09:45 AM
add the jscrollpane to the panel borderlayout.center. You probably shouldn't set preferred size or minimal size on the jscrollpane, just let the borderlayout figure out what it should be. You'll probably need to set the preferred size on your panel though.
#3
Re: Resizing Components with the Window
Posted 28 October 2008 - 10:48 AM
Nope, it didn't work.
#4
Re: Resizing Components with the Window
Posted 28 October 2008 - 01:07 PM
You could try adding an event listener for the form's resize event, then resize the components upon the form resize.
Alternatively, you could try doing something similar to what I did in my tutorial.
Alternatively, you could try doing something similar to what I did in my tutorial.
#5
Re: Resizing Components with the Window
Posted 28 October 2008 - 01:50 PM
Thanks gab! I'm not quite sure how it works, but it works and that's all that matters.
#6
Re: Resizing Components with the Window
Posted 28 October 2008 - 02:03 PM
Glad I could help 
My brain's practically shut down for today, so I probably can't explain it too well.
BUT, I'll try anyway. It's the BorderLayout. It's basically like saying "our content pane is always going to match the border of the JFrame~ It will never be too big to extend it."
Then, you're adding the text panel to the content pane, and it fills it gets applied to the entire content pane.
My brain's practically shut down for today, so I probably can't explain it too well.
BUT, I'll try anyway. It's the BorderLayout. It's basically like saying "our content pane is always going to match the border of the JFrame~ It will never be too big to extend it."
Then, you're adding the text panel to the content pane, and it fills it gets applied to the entire content pane.
#7
Re: Resizing Components with the Window
Posted 28 October 2008 - 04:35 PM
Absynthe is right
don't bother about the resizing Swing will do it for you
You must have a setMinimumSize() or setMaximumSize() or setPreferredSize() lying somewhere around
don't bother about the resizing Swing will do it for you
You must have a setMinimumSize() or setMaximumSize() or setPreferredSize() lying somewhere around
Page 1 of 1
|
|

New Topic/Question
Reply




MultiQuote








|