Results 1 to 15 of 15
- 09-17-2009, 05:01 AM #1
Member
- Join Date
- Sep 2009
- Posts
- 44
- Rep Power
- 0
-
The best way to do this is to read up on how to use the various layouts that are available for use in Swing and then to nest JPanels each using the best layout for the job to hold your components. Start here:
Lesson: Laying Out Components Within a Container
Much luck!
- 09-17-2009, 06:26 AM #3
Member
- Join Date
- Sep 2009
- Posts
- 44
- Rep Power
- 0
thanks for the link that you give it to me but i cannot understand... on how to use those layouts. for the instance if my codes are going to destroy or rumbled.
this is my codes:
the red text is my codes for my textboxes, buttons and labels.Java Code:import java.awt.Container; import java.awt.FileDialog; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.ItemEvent; import java.awt.event.ItemListener; import java.awt.event.KeyEvent; import java.io.File; import javax.swing.JButton; import javax.swing.JFileChooser; import javax.swing.JFrame; import javax.swing.JTextField; import javax.swing.JRadioButton; import javax.swing.JCheckBox; import javax.swing.ButtonGroup; import javax.swing.event.ChangeEvent; import javax.swing.event.ChangeListener; public class FileBrowse extends JFrame { [COLOR="Red"]JButton browseSFile = new JButton("Browse..."); JButton browseDir = new JButton("Browse..."); JButton browseEn = new JButton("Browse..."); JTextField textField = new JTextField(20); JTextField textField1 = new JTextField(20); JTextField textField2 = new JTextField(20); JCheckBox checkforEn = new JCheckBox("English File"); JRadioButton radioforSFile = new JRadioButton("Single Files"); JRadioButton radioforDir = new JRadioButton("Directories");[/COLOR] public FileBrowse() { setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setResizable(false); browseSFile.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { onBrowseSFile(); } }); browseDir.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { onBrowseDir(); } }); browseEn.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { onBrowseEn(); } }); ButtonGroup btnGroup = new ButtonGroup(); btnGroup.add(radioforSFile); btnGroup.add(radioforDir); radioforSFile.addItemListener(new ItemListener() { public void itemStateChanged(ItemEvent e) { if (e.getStateChange() == ItemEvent.SELECTED) { textField1.setEnabled(false); browseDir.setEnabled(false); textField.setEnabled(true); browseSFile.setEnabled(true); } } }); radioforDir.addItemListener(new ItemListener() { public void itemStateChanged(ItemEvent e) { if (e.getStateChange() == ItemEvent.SELECTED) { textField.setEnabled(false); browseSFile.setEnabled(false); textField1.setEnabled(true); browseDir.setEnabled(true); } } }); Container container = getContentPane(); container.setLayout(new GridLayout(4, 4)); container.add(checkforEn); container.add(textField2); container.add(browseEn); container.add(radioforSFile); container.add(textField); container.add(browseSFile); container.add(radioforDir); container.add(textField1); container.add(browseDir); pack(); } protected void onBrowseSFile() { JFileChooser fileChooser = new JFileChooser(); int result = fileChooser.showDialog(this, "Open/Save"); if (result == JFileChooser.APPROVE_OPTION) { File file = fileChooser.getSelectedFile(); textField.setText(file.toString()); } } protected void onBrowseDir() { JFileChooser fileChooser1 = new JFileChooser(); int result = fileChooser1.showDialog(this, "Open/Save"); if (result == JFileChooser.APPROVE_OPTION) { File file1 = fileChooser1.getSelectedFile(); textField1.setText(file1.toString()); } } protected void onBrowseEn() { JFileChooser fileChooser2 = new JFileChooser(); int result = fileChooser2.showDialog(this, "Open/Save"); if (result == JFileChooser.APPROVE_OPTION) { File file2 = fileChooser2.getSelectedFile(); textField2.setText(file2.toString()); } } public static void main(String[] args) { new FileBrowse().setVisible(true); } }
- 09-17-2009, 08:09 AM #4
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
You are going to have to read and understand that tutorial. If you can't understand it then you can't write your program successfully.
- 09-17-2009, 08:15 AM #5
Member
- Join Date
- Sep 2009
- Posts
- 44
- Rep Power
- 0
Actually my problem is about resizing my textboxes. but my labels and buttons are also adjust... i want to know what is the best way to do that my labels and buttons are not adjust when i need to adjust my textbox... and i do not know how to do it...
- 09-17-2009, 08:23 AM #6
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
We all understand what your problem is. Your solution is to read about Layout managers and understand how they affect sizes of components. I know reading is now considered archaic but it is a necessary part of solving your current problem.
- 09-17-2009, 08:27 AM #7
Member
- Join Date
- Sep 2009
- Posts
- 44
- Rep Power
- 0
Ok thanks... im trying to read about layout managers and understand it... but i don't know how to use and imports it...
- 09-17-2009, 08:31 AM #8
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
It's all explained in the tutorial. They even have some example code.
- 09-18-2009, 03:19 AM #9
Member
- Join Date
- Sep 2009
- Posts
- 44
- Rep Power
- 0
I've read about the FlowLayout and also i apply with my codes... the labels and buttons are not adjust when i adjust my textbox. but my problem is how can i break in new line..? i want to do is my radiobutton, label, textbox and button are in column and my another radiobutton, label, textbox and button are in a new column..
how to solve it..?
-
1) Please explain better what you're trying to achieve as it's hard to figure out based on the statements just above.
2) FlowLayout is probably one of the weaker layout managers. You're probably better learning BoxLayout, BorderLayout, GridLayout, and then after learning these, GridBagLayout.
- 09-18-2009, 04:03 AM #11
Member
- Join Date
- Sep 2009
- Posts
- 44
- Rep Power
- 0
ahh okay...!
because im trying to make a program for browsing.
1.) browsing for Files only and 1 i have my radio button, labels, text box and buttons in 1 column itself.
2.) browsing for Files only too and 2 i have my radio button, labels, text box and buttons in 1 column itself.
3.) browsing for Directory and 3 i have my radio button, labels, text box and buttons in 1 column itself.
i want to do know is how to break a new line.
new line for 1.
new line for 2.
new line for 3.
I'm trying to grasp about the boxlayout, borderlayout, gridlayout and hope so to gridbaglayout.
-
Some ways to have new lines are:
1) Have a JPanel use BoxLayout oriented to BoxLayout.PAGE_AXIS, and add JPanels that hold a JRadioButton, JTextField, and JTextField to this.
2) Use GridBagLayout (probably the best solution for this one -- sorry), and with each new line, increment the GridBagConstraints variable gridy field by one.
- 09-18-2009, 04:42 AM #13
Member
- Join Date
- Sep 2009
- Posts
- 44
- Rep Power
- 0
so this a combination of BoxLayout and GridBagLayout..? to do this my codes are rumbled...
but i'll try to do this..
thanks for your help..
-
No, I'd either do one or the other. For instance, you can have a JPanel that uses BoxLayout oriented along the PAGE_AXIS then adding JPanels that use either FlowLayout or BoxLayout but oriented LINE_AXIS.
Or you can have a JPanel that uses GridBagLayout that holds the JRadioButtons, JButtons, and JTextFields.
- 09-18-2009, 04:48 AM #15
Member
- Join Date
- Sep 2009
- Posts
- 44
- Rep Power
- 0
Similar Threads
-
need to adjust a code I have to work with hidden fields
By carag in forum New To JavaReplies: 2Last Post: 07-29-2009, 12:23 PM -
Setting the label of radio buttons from Resource bundle
By rajeeshankar in forum JavaServer Faces (JSF)Replies: 0Last Post: 12-17-2007, 09:23 AM -
How To:Use a JSlider to adjust Text size in a JPanel
By louiebagz in forum AWT / SwingReplies: 2Last Post: 07-01-2007, 07:37 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks