Adding JTextField to a JPanel and showing them
Adding JTextField to a JPanel and showing them
I'm building a little app using Java and Swing in NetBeans. Using NetBeans design window, I created a JFrame with a JPanel inside.
Now I want to dynamically add some jTextFields to the JPanel. I wrote something like that:
Vector textFieldsVector = new Vector(); JTextField tf; int i = 0; while (i < 3) { tf = new JTextField(); textFieldVector.add(tf); myPanel.add(tf); //myPanel is the JPanel where I want to put the JTextFields i++; } myPanel.validate(); myPanel.repaint();
But nothing happens: when I run the app, the JFrame shows with the JPanel inside, but the JTextFields don't.
I'm a total newbie in writing graphical Java apps, so I'm surely missing something very simple, but I can't see what.
Answer by Bombe for Adding JTextField to a JPanel and showing them
Your while loop is wrong. i
never gets incremented so your window creation is in an endless loop and your CPU consumption should be at 100% until you abort the program. Also, the GUI should be completely non-responsive when you run your program.
Answer by MrWiggles for Adding JTextField to a JPanel and showing them
It's been a while since I've done some Swing but I think you'll need to recall pack() to tell the frame to relayout its components
EDIT: Yep, I knew it had been too long since I did Swing. I've knocked up the following code which works as expected though and adds the textfields...
JFrame frame = new JFrame("My Frame"); frame.setSize(640, 480); JPanel panel = new JPanel(); panel.add(new JLabel("Hello")); frame.add(panel); frame.setLayout(new GridLayout()); frame.pack(); frame.setVisible(true); Vector textFieldVector = new Vector(); JTextField tf; int i = 0; while (i < 3) { tf = new JTextField(); textFieldVector.add(tf); panel.add(tf); //myPanel is the JPanel where I want to put the JTextFields i++; } panel.validate(); panel.repaint();
Answer by Tom Hawtin - tackline for Adding JTextField to a JPanel and showing them
The usual way to use GroupLayout
is to add a component to a Group
. GroupLayout
keeps a reference to the Container
it is responsible for (which makes sense). You shouldn't be adding the component to the panel without constraints.
Answer by James Schek for Adding JTextField to a JPanel and showing them
In the Netbeans GUI, set the layout manager to something like GridLayout or FlowLayout (just for testing). You can do this by going to the GUI editor, clicking on your panel, and then right-clicking and selecting a layout.
Once you've changed to a different layout, go to the properties and alter the layout properties. For the GridLayout, you want to make sure you have 3 grid cells.
Instead of myPanel.validate(), try myPanel.revalidate().
The more canonical way to do this is to create a custom JPanel (without using the GUI editor) that sets its own layout manager, populates itself with components, etc. Then, in the Netbeans GUI editor, drag-and-drop that custom JPanel into the gui editor. Matisse is certainly capable of handling the runtime-modification of Swing components, but that's not the normal way to use it.
Answer by Cork for Adding JTextField to a JPanel and showing them
Just use the .setVisible()
method of JTextField:
JTextField tf = new JTextField() ; tf.setVisible(true) ; panel.add(tf) ;
Answer by Raymond Gao for Adding JTextField to a JPanel and showing them
Don't use GroupLayout with new (dynamically added) component. It won't show up.
Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 72
0 comments:
Post a Comment