Results 1 to 15 of 15
  1. #1
    mlibot is offline Member
    Join Date
    Sep 2009
    Posts
    44
    Rep Power
    0

    Default Why Buttons and label also adjust when i adjust my textbox?

    Hi again... i have problem with my labels and buttons. when i adjust my textbox my labels and buttons is also adjust. what is the best way to do..?

    hope you reply thanks..

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    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!

  3. #3
    mlibot is offline Member
    Join Date
    Sep 2009
    Posts
    44
    Rep Power
    0

    Default

    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:

    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);
    		
        }
    }
    the red text is my codes for my textboxes, buttons and labels.

  4. #4
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    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.

  5. #5
    mlibot is offline Member
    Join Date
    Sep 2009
    Posts
    44
    Rep Power
    0

    Default

    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...

  6. #6
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    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.

  7. #7
    mlibot is offline Member
    Join Date
    Sep 2009
    Posts
    44
    Rep Power
    0

    Default

    Ok thanks... im trying to read about layout managers and understand it... but i don't know how to use and imports it...

  8. #8
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    It's all explained in the tutorial. They even have some example code.

  9. #9
    mlibot is offline Member
    Join Date
    Sep 2009
    Posts
    44
    Rep Power
    0

    Default

    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..?

  10. #10
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    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.

  11. #11
    mlibot is offline Member
    Join Date
    Sep 2009
    Posts
    44
    Rep Power
    0

    Default

    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.

  12. #12
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    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.

  13. #13
    mlibot is offline Member
    Join Date
    Sep 2009
    Posts
    44
    Rep Power
    0

    Default

    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..

  14. #14
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    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.

  15. #15
    mlibot is offline Member
    Join Date
    Sep 2009
    Posts
    44
    Rep Power
    0

    Default

    ahhmm... so the best way to do is to use a gridbaglayout..?

Similar Threads

  1. Replies: 2
    Last Post: 07-29-2009, 01:23 PM
  2. Setting the label of radio buttons from Resource bundle
    By rajeeshankar in forum JavaServer Faces (JSF)
    Replies: 0
    Last Post: 12-17-2007, 10:23 AM
  3. How To:Use a JSlider to adjust Text size in a JPanel
    By louiebagz in forum AWT / Swing
    Replies: 2
    Last Post: 07-01-2007, 08:37 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •