JTextField - setColumns() method not working for me

戏子无情 提交于 2019-12-12 12:12:54

问题


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:


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




回答2:


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"));>


来源:https://stackoverflow.com/questions/8885860/jtextfield-setcolumns-method-not-working-for-me

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!