JTextField method - setColumns () doesn't work for me

my problem is that my JTextField -setColumns (int)

"field1.setColumns (5);"

... does not work. I assume this is a layout manager problem. However, I am learning from a notebook. The only layouts I know are flowLayout, borderLayout and gridlayout.

To explain this code in short, whenever β€œfield2” launches an ActionEvent (by pressing the enter key), β€œfield1” must resize.

I placed "System.out.println (" detected by ActionEvent ") in" actionPerformed "to prove that the actionevent was fired, so this is not a problem. I even printed" field1.getColumn "and it shows the correct modified value of 5, however. .. It’s not just not noticeably resizing at runtime.

Instead of working, I was hoping that someone could explain the problem. Working around will not help me learn, which is the whole task of solving these book exercises.

Hit it important, I code in netbeans. Thanks in advance for your 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); } 

}

+4
source share
2 answers

This works, you just need to get the container to compose its components again. This can be done by calling revalidate and then issuing a repaint request (to remove any visual artifacts).

+5
source

The .setColumns() method (used with the JFormattedTextField component) did not work due to using the TitledBorder for the container in which the JFormattedTextFields was placed inside:

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

Source: https://habr.com/ru/post/1391326/


All Articles