• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Problem with Generating TextField by actionPerformed

 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 4121
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic