-1

I'm new to Java and I'm working on an airplane seat reservation program. I would like to know how to display a JTextfield text box and a JLabel after clicking a button that is already displayed in a JFrame window. I have tried to add the button after an if statement but it doesn't work. I have also tried button.setVisible(false) and then making it true later. I have a button.actionlistener(this) for each of the buttons used.

 public void actionPerformed(ActionEvent event){
    Container contentPane = null;
    JButton clickedButton = (JButton) event.getSource();
        if (event.getSource() == coach){
            five.setForeground(Color.green);
            eleven.setForeground(Color.green);
            six.setForeground(Color.green);
            two.setForeground(Color.black);
                if (event.getSource() == five) {
                    inputLine = new JTextField();
                    inputLine.setBounds(110, 180, 185, 22);
                    contentPane.add(inputLine);
                }

        } else if (event.getSource() == firstClass){
            two.setForeground(Color.green);
            five.setForeground(Color.black);
            eleven.setForeground(Color.black);
            six.setForeground(Color.black);

        }
user3398975
  • 1
  • 1
  • 2
  • 5
    An actually [runnable example that demonstrates your problem](https://stackoverflow.com/help/mcve) would involve less guess work and better responses. First, try adding them to the container you want to add them to. You may be required to call `revalidate` in order to get the UI to update – MadProgrammer Apr 28 '14 at 06:30
  • For many components in one space, use a [`CardLayout`](http://docs.oracle.com/javase/7/docs/api/java/awt/CardLayout.html) as seen in this [short example](http://stackoverflow.com/a/5786005/418556). By 'many' in this case we'd mean a blank panel as the default, while a 2nd panel contains the label and text field. Add both to the card layout and flip to the one with input controls when needed. – Andrew Thompson Apr 28 '14 at 06:58

1 Answers1

0

Am not totally clear by your information, whether u mean to display the created textfields and labels on the created button or create the same on the created button.

But I have some code for you, see if it helps.

JTextField[] desc=new JTextField[20];
 JButton more=new JButton("More");
    Container c=getContentPane();
    public void actionPerformed(ActionEvent a1)
    {
    String s1=a1.getActionCommand();
    String s="";
           for(i=0;i<cnt;i++)
        s=s+desc[i].getText();

     if(s1.equals("More"))
    {
                 System.out.println("Created");
                 desc[i]=new JTextField();
                 desc[i].setBounds(50,y,150,30);
                 c.add(desc[i]);
    }
    }

Same way create for JLabel.

Deepika
  • 331
  • 2
  • 7
  • 20