1

In my application ,When the user clicks the JButton, display a JLabel that prompts the user to enter an integer, a JTextField into which the user can type the integer, and a secondJButton that contains the text Double me. When the user clicks the second button, the integer is doubled and the answer is displayed in the JTextField.

I am not able to display second button and text fields, when i click first button...please help

import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JTextField;


public class JDouble extends JApplet implements ActionListener {
    private Container container = getContentPane();

    /**
     * The JButton.
     */
    private JButton button = new JButton("Click me");
    private JButton button2 = new JButton("Double me");

    /**
     * The JLabel.
     */
    private JLabel label = new JLabel("Enter Integer");
    private JTextField textfield = new JTextField(4);
    private JTextField textfield2 = new JTextField(4);

    public void init() {
        // set the layout to FlowLayout
        container.setLayout(new FlowLayout());

        // register the 'this' action listener for the button
        button.addActionListener(this);

        container.add(button);
    }

    public void init1(){
        container.setLayout(new FlowLayout());
        container.add(textfield);
        container.add(button2);
        container.add(textfield2);
        button2.addActionListener(this);
    }

    public void actionPerformed(ActionEvent actionEvent) {
        container.add(label);
    }

    public void actionPerformed1(ActionEvent actionEvent) {
        String me = textfield.getText();
        int computation = Integer.parseInt(me);
        computation = computation*2;
        String changecomputation = Integer.toString(computation);
        textfield2.setText(changecomputation);
        container.remove(button);

        container.add(label);

        repaint();
        validate();
    }

}
raffian
  • 31,267
  • 26
  • 103
  • 174
James Blent
  • 35
  • 1
  • 6

1 Answers1

3

Your init() method:

   public void init() {  
        // set the layout to FlowLayout
        container.setLayout(new FlowLayout());
        // register the 'this' action listener for the button
        button.addActionListener(this);
        container.add(button);
}

We'll use this function to show the other fields when 'Click Me' is clicked...

  private showInputFields(){
        container.add(textfield);
        button2.addActionListener(this);
        container.add(button2);
        container.add(textfield2);
  }

Let's fix your action listener. If you want 'Click Me" button displayed on startup, init() takes care of that. When user clicks 'Click Me', we invoke showInputFields() to display the other components; we handle 'Double me' click using the same listener, we just check event source to handle appropriately...

    private boolean inputFieldsDisplayed; 
    public void actionPerformed(ActionEvent actionEvent) {
        if( actionEvent.getSource() == button && !inputFieldsDisplayed){
            showInputFields();
            inputFieldsDisplayed = true;

        } else if ( actionEvent.getSource() == button2){                
            String me = textfield.getText();
            int computation = Integer.parseInt(me);
            computation = computation*2;
            String changecomputation = Integer.toString(computation);
            textfield2.setText(changecomputation);                
        }
        validate();
        repaint();            
    }
raffian
  • 31,267
  • 26
  • 103
  • 174
  • but two buttons appearing at the same time..what should i do.whein i click the button1..button2,textfield1,textfield2 appears? – James Blent Aug 06 '13 at 17:26
  • when i click it other controls should appear – James Blent Aug 06 '13 at 17:43
  • 1
    @raffian : `validate()/revalidate()` should come before `repaint()`, why is the need to `repaint()` if nothing has changed. That's why we `revalidate()/validate()` first so the Layout Manager concern lays out the components, then we call `repaint()` to make those changes appear nicely :-) – nIcE cOw Aug 06 '13 at 18:10
  • 1
    @raffian : Please have a look at this [thread](http://stackoverflow.com/q/9510125/1057230) and this [answer](http://stackoverflow.com/q/18001087/1057230) – nIcE cOw Aug 06 '13 at 18:21