6

my problem is that my JTextField -setColumns(int)

"field1.setColumns(5);"

...doesn't work. I'm guessing its a layout manager problem. However, I'm learning from an exercise book. The only layouts I know are flowLayout, borderLayout and gridlayout.

To explain this code in short, whenever "field2" triggers an ActionEvent (by pressing the enter key) "field1" should change size.

I've placed a "System.out.println("ActionEvent detected") in the "actionPerformed" to prove an actionevent is being fired, so that isn't the problem. I've even printed "field1.getColumn" and it shows the correct changed value of 5, however... Its not just not visibly changing size at runtime.

Rather than a work around I was hoping somebody could explain the problem. A work around isn't going to help me learn, which is the whole purpose of tackling these book exercises.

Incase its important, I'm coding in netbeans. Thanks in advance for the help.

public class Exercise13_11 extends JFrame implements ActionListener
{
    private JTextField textField1, textField2;
    private JLabel label1, label2;
    private JRadioButton rButton1, rButton2, rButton3;

    public static void main(String[] args)
    {
        JFrame frame = new Exercise13_11();
        frame.setTitle("Exercise 13.11");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(450, 200);
        frame.setVisible(true);
    }

public Exercise13_11()
{
    // North Panel aligned and filled.
    JPanel northPanel = new JPanel();
    northPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
    northPanel.add(label1 = new JLabel("Text Field"));
    northPanel.add(textField1 = new JTextField(20));
    northPanel.setToolTipText("Demonstrate JTextField");
    getContentPane().add(northPanel, BorderLayout.CENTER);

    // South panel now being filled...
    JPanel southPanel = new JPanel();
        southPanel.setLayout(new FlowLayout());

    JPanel alignmentPanel = new JPanel();
    alignmentPanel.setBorder(
                new javax.swing.border.TitledBorder("Horizontal Alignment"));
    alignmentPanel.add(rButton1 = new JRadioButton("Left"));
    alignmentPanel.add(rButton2 = new JRadioButton("Center"));
    alignmentPanel.add(rButton3 = new JRadioButton("Right"));

    ButtonGroup buttonGroup = new ButtonGroup();
    buttonGroup.add(rButton1);
    buttonGroup.add(rButton2);
    buttonGroup.add(rButton3);

    JPanel columnPanel = new JPanel();
    columnPanel.setBorder(new javax.swing.border.EtchedBorder());
    columnPanel.setLayout(new FlowLayout());
    columnPanel.add(label2 = new JLabel("Column Size"));
    columnPanel.add(textField2 = new JTextField(10));

    southPanel.add(alignmentPanel);
    southPanel.add(columnPanel);
    getContentPane().add(southPanel, BorderLayout.SOUTH);

    textField1.addActionListener(this);
    rButton1.addActionListener(this);
    rButton2.addActionListener(this);
    rButton3.addActionListener(this);        
}

public void actionPerformed(ActionEvent e)
{
    if (e.getSource() == textField1)
    {
        textField1.setColumns(5);
    }
    else if (e.getSource() == rButton1)
            textField1.setHorizontalAlignment(textField1.LEFT);
    else if (e.getSource() == rButton2)
            textField1.setHorizontalAlignment(textField1.CENTER);
    else if (e.getSource() == rButton3)
            textField1.setHorizontalAlignment(textField1.RIGHT);
 }

}

1
  • Try adding a frame.repaint() after you change the TextField's column size. It could be an issue with Frame not realizing that the text field has changed, so it doesn't bother to redraw it.
    – aoi222
    Commented Jan 16, 2012 at 20:32

2 Answers 2

6

It's working, you just need to force the container to layout its components again. This can be achieved by invoking revalidate and then issuing a repaint request (to remove any visual artifacts).

4
  • Wicked, I've not covered validate() in my book. But screw it, a repaint() by itself didn't work. Thank you so much
    – cworner1
    Commented Jan 16, 2012 at 20:50
  • @cworner1, that was revalidate() not validate() that you should be using.
    – camickr
    Commented Jan 16, 2012 at 21:16
  • Netbeans doesn't recognise revalidate(). I'm guessing its not a method found in the Component class.
    – cworner1
    Commented Jan 17, 2012 at 9:24
  • +1 it's a bug (which I wasn't aware of until today ;-) - JTextField should invalidate itself on setting the columns ...
    – kleopatra
    Commented Jan 17, 2012 at 10:58
-1

The method .setColumns() (used with JFormattedTextField component) failed to work, due to use of a TitledBorder for the container where the JFormattedTextFields was put inside:

<container.setBorder(javax.swing.BorderFactory.createTitledBorder("central"));>

Not the answer you're looking for? Browse other questions tagged or ask your own question.