Tyrion
Tyrion

Reputation: 29

Add new label and textfield on button click Java

I have this code with three buttons. I want to add a label and a textfield after a buttonclick. So for the 'button' I want to add the 'label' and 'textfield.

For 'button2' I want to add 'label2' and 'textfield2'. And so on.

I already have this code, but it isn't working. I also don't understand how I can create three different ActionListeners.

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

public class Interface implements ActionListener{

    private JLabel label;
    private JLabel label2;
    private JLabel label3;
    private JTextField textfield;
    private JTextField textfield2;
    private JTextField textfield3;
    private JButton button;
    private JButton button2;
    private JButton button3;
    private JPanel panel;

    public static void main(String[] args){
        new Interface();
    }

    public Interface()
    {
        JFrame frame = new JFrame("Test");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(600,600);
        frame.setLayout(null);

        JPanel panel = new JPanel();
        panel.setBounds(10,10,600,600);
        panel.setLayout(null);

        label = new JLabel("Button 1");
        label.setBounds(5,5,600,20);

        label2 = new JLabel("Button 2");
        label2.setBounds(5,5,600,20);

        label3 = new JLabel("Button 3");
        label3.setBounds(5,5,600,20);

        textfield = new JTextField();
        textfield.setBounds(5,30,100,20);

        JButton button = new JButton("cirkel");
        button.setBounds(130,30,100,20);
        button.addActionListener(this);

        JButton button2 = new JButton("driehoek");
        button2.setBounds(250,30,100,20);
        button2.addActionListener(this);

        JButton button3 = new JButton("vierhoek");
        button3.setBounds(370,30,100,20);
        button3.addActionListener(this);

        panel.add(button);
        panel.add(button2);
        panel.add(button3);

        frame.add(panel);
        frame.setVisible(true);
    }

    public void actionPerformed(ActionEvent arg0)
    {
            panel.add(label);
            panel.add(textfield);
            panel.validate();
            panel.repaint();
            String text = textfield.getText();

            // Van de String 'text' een double maken
            double diameter = Double.parseDouble(text);

            cirkel C1 = new cirkel();
            C1.setDiam(diameter);
            label.setText("De diameter = " + C1.getDiam() + " cm \n\n");
            label.setText("De straal = " + C1.getRadius() + " cm");
            label.setText("De oppervlakte van de cirkel = " + C1.berekenOpp() + " cm2");

    }
}

Upvotes: 1

Views: 26521

Answers (2)

MadProgrammer
MadProgrammer

Reputation: 347314

You have a few choices:

You could...

Create a ActionListener for each button, this can be done using a outer, inner or anonymous class, this means that you provide a self contained unit of work for each button which is related to only that button.

You could also take advantage of the Action API for the same reasons. See How to Use Actions for more details

You could...

Check the ActionEvent#getSource property and compare it to an instance of the button, but your buttons are defined locally

You could...

Use the actionCommand property of the buttons, setting each button a unique "command" which you can use the ActionEvent#getActionCommand to compare with when the ActionListener is triggered

Regardless of which choice you use, have a closer look at How to Write an Action Listeners and How to Use Buttons, Check Boxes, and Radio Buttons for more details

Example

Now, having had a chance to run your code, you have a NullPointerException because panel is null in the context of the ActionListener. This is cased by the fact that you are shadowing the variable (declaring it twice)

// Declared here
private JPanel panel;

public Interface() {
    JFrame frame = new JFrame("Test");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(600, 600);
    frame.setLayout(null);

    // And here...
    JPanel panel = new JPanel();

So, instead, you could use a anonymous class for each button...

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class Interface {

    private JLabel label;
    private JLabel label2;
    private JLabel label3;
    private JTextField textfield;
    private JTextField textfield2;
    private JTextField textfield3;
    private JButton button;
    private JButton button2;
    private JButton button3;
    private JPanel panel;

    public static void main(String[] args) {
        new Interface();
    }

    public Interface() {
        JFrame frame = new JFrame("Test");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(600, 600);

        JPanel buttonPane = new JPanel();
        label = new JLabel("Button 1");
        label2 = new JLabel("Button 2");
        label3 = new JLabel("Button 3");

        textfield = new JTextField(5);

        JButton button = new JButton("cirkel");
        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                panel.removeAll();
                panel.add(label);
                panel.add(textfield);
                panel.revalidate();
                panel.repaint();
                String text = textfield.getText();

                // Van de String 'text' een double maken
                double diameter = Double.parseDouble(text);

//      cirkel C1 = new cirkel();
//      C1.setDiam(diameter);
                label.setText("De diameter = " + 1 + " cm \n\n");
                label.setText("De straal = " + 2 + " cm");
                label.setText("De oppervlakte van de cirkel = " + 3 + " cm2");
            }
        });

        JButton button2 = new JButton("driehoek");
        button2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                panel.removeAll();
                panel.add(label);
                panel.add(textfield);
                panel.revalidate();
                panel.repaint();
                // driehoek
            }
        });

        JButton button3 = new JButton("vierhoek");
        button3.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                panel.removeAll();
                panel.add(label);
                panel.add(textfield);
                panel.revalidate();
                panel.repaint();
                // vierhoek
            }
        });

        buttonPane.add(button);
        buttonPane.add(button2);
        buttonPane.add(button3);

        panel = new JPanel();

        frame.add(buttonPane, BorderLayout.NORTH);
        frame.add(panel);
        frame.setVisible(true);
    }
}

The next problem you'll have is a NumberFormatException, because you you're trying to convert a blank String to a double, because there is nothing in the text field yet...

Upvotes: 4

Kasper Due
Kasper Due

Reputation: 709

If you want to create three different ActionListeners, you can create three inner classes, and implement an ActionListener to each class. and put the instance of the class inside the addActionListener().

public class Example {

    ActionListen listen = new ActionListen();

    button.addActionListener(listen);

    private class ActionListen implements ActionListener {

        public void actionPerformed(ActionEvent e) {
            //code here to change...
        }
    }
}

Upvotes: 1

Related Questions