• Post Reply Bookmark Topic Watch Topic
  • New Topic

Problem with Generating TextField by actionPerformed  RSS feed

 
Huzefa Zohaib
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I had wrote the following code so that it can generate the
Text Field when I press Add button. but it is not doing that
even though it is not throwing any exception.
So please guide that how should be the code.
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.event.*;
import java.applet.*;
public class Field implements ActionListener {
private JFrame aWindow;
private JTextField textField;
private Container container;
private JPanel textFieldPanel, panel;
private JButton button;
private Field obj1;
private JTextField setTextField (int columns) {
return this.textField = new JTextField (columns);
} // giveField
private JButton setButton (String name) {
return this.button = new JButton (name);
} // giveButton
private JPanel setPanel () {
return this.panel = new JPanel ();
} // givePanel
private JTextField getTextField () {
return textField;
} // giveField
private JButton getButton () {
return button;
} // giveButton
private JPanel getPanel () {
return panel;
} // givePanel
private void runIt () {
aWindow = new JFrame ("Adding Rows");
aWindow.setBounds(140, 100, 400, 300);
aWindow.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
container= aWindow.getContentPane();
container.setLayout (new GridLayout (2,2));
obj1 = new Field ();
obj1.setPanel();
obj1.setTextField (10);
obj1.setButton ("Add");
obj1.getPanel().add (obj1.getTextField ());
obj1.getPanel().add (obj1.getButton ());
obj1.getButton ().addActionListener (this);
container.add (obj1.getPanel ());
aWindow.setVisible(true);
} // runIt()
public void actionPerformed (ActionEvent theEvent) {
if (theEvent.getSource () == obj1.getButton ()) {
System.out.println ("Yes");
obj1.getPanel().add (obj1.getTextField ());
container.add (obj1.getPanel ());
} // if
} // actionPerformed ()
public static void main (String[]args) {
new Field ().runIt ();
} // main ()
} // Class Field
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
call container.validate() and container.repaint() after you add the textfield... that makes the container lay itself out again and repaint itself so the textfield will show up.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!