Results 1 to 4 of 4
  1. #1
    shai is offline Member
    Join Date
    Jul 2010
    Posts
    10
    Rep Power
    0

    Default Problem with setColumns in JTextField

    The following program basically shows a GUI that has a text field with the options to align the text to the left, center or right, and to change the number of columns in the text field.
    The only problem is that when you change the number of columns the text field's size doesn't update.

    It is the action listener near the end, just above the main method:

    Java Code:
    import javax.swing.*;
    import javax.swing.border.*;
    import java.awt.*;
    import java.awt.event.*;
    
    /**
     *
     * @author Shai
     */
    public class PropertiesJTextField extends JFrame {
            private JPanel panelTop = new JPanel();
            private JPanel panelsBottom = new JPanel();
            private JPanel panelBottom1 = new JPanel();
            private JPanel panelBottom2 = new JPanel();
            private JLabel textFieldLbl = new JLabel("Text field");
            private JButton left = new JButton("Left");
            private JButton center = new JButton("Center");
            private JButton right  = new JButton("Right");
            private ButtonGroup directions = new ButtonGroup();
            private JLabel columnSize = new JLabel("Column size");
            private JTextField textField = new JTextField("Type anything...");
            private JTextField sizeField = new JTextField("30", 5);
    
            public PropertiesJTextField() {
                    setLayout(new BorderLayout());
                    add(panelTop, BorderLayout.NORTH);
                    panelTop.add(textFieldLbl);
                    panelTop.add(textField);
                    add(panelsBottom, BorderLayout.SOUTH);
                    panelsBottom.add(panelBottom1);
                    panelsBottom.add(panelBottom2);
                    panelBottom1.setBorder(new TitledBorder("Horizontal Alignment"));
                    panelBottom1.add(left);
                    panelBottom1.add(center);
                    panelBottom1.add(right);
                    directions.add(left);
                    directions.add(center);
                    directions.add(right);
                    panelBottom2.setBorder(new TitledBorder(""));
                    panelBottom2.add(columnSize);
                    panelBottom2.add(sizeField);
    
                    left.addActionListener(new ActionListener() {
                       public void actionPerformed(ActionEvent e) {
                               textField.setHorizontalAlignment(JTextField.LEFT);
                       }
                    });
    
                    center.addActionListener(new ActionListener() {
                       public void actionPerformed(ActionEvent e) {
                               textField.setHorizontalAlignment(JTextField.CENTER);
                       }
                    });
    
                    right.addActionListener(new ActionListener() {
                       public void actionPerformed(ActionEvent e) {
                               textField.setHorizontalAlignment(JTextField.RIGHT);
                       }
                    });
    
                    sizeField.addActionListener(new ActionListener() {
                       public void actionPerformed(ActionEvent e) {
                               textField.setColumns(Integer.parseInt(sizeField.getText()));                        
                       }
                    });
            }
    
            public static void main(String [] args) {
                    PropertiesJTextField frame = new PropertiesJTextField();
                    frame.pack();
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.setVisible(true);
            }
    }
    Any ideas about why this happens would be appreciated.

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

    Default

    Have you tried calling revalidate on the Container (panelTop) that holds the JTextFields after changing the column size?

  3. #3
    shai is offline Member
    Join Date
    Jul 2010
    Posts
    10
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    Have you tried calling revalidate on the Container (panelTop) that holds the JTextFields after changing the column size?
    That solved it.

    Thanks.
    Last edited by shai; 07-26-2010 at 05:41 AM.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Quote Originally Posted by shai View Post
    Yeah that did the trick.
    You're welcome. What that does is tells the layout managers of the Container and its children to re-layout the components they hold.

Similar Threads

  1. Manually position JTextfield problem
    By castiel in forum AWT / Swing
    Replies: 3
    Last Post: 02-20-2010, 05:24 AM
  2. Applet JTextField Size Problem
    By a13w in forum New To Java
    Replies: 4
    Last Post: 09-19-2009, 07:52 AM
  3. Replies: 1
    Last Post: 01-30-2009, 06:44 PM
  4. SWT_AWT bridge ,jtextfield edit problem
    By Sureshgurram in forum SWT / JFace
    Replies: 3
    Last Post: 12-09-2008, 12:40 PM
  5. Got problem with JtextField
    By hungleon88 in forum AWT / Swing
    Replies: 4
    Last Post: 12-06-2008, 03:01 PM

Posting Permissions

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