Question

Well, this is a very newie cuestion. Im stating to write by myself the code of my GUI applications with the help of window builder, i have decided to stop using netbeans couse ive read some peope in here that said that would be good. You may think i havent investigate, but trust me, i did my homework...

I tryed the way oracle says:

  1. Declare an event handler class and specify that the class either implements an ActionListener interface or extends a class that implements an ActionListener interface. For example:

    public class MyClass implements ActionListener {

  2. Register an instance of the event handler class as a listener on one or more components. For example:

    someComponent.addActionListener(instanceOfMyClass);

  3. Include code that implements the methods in listener interface. For example:

    public void actionPerformed(ActionEvent e) { ...//code that reacts to the action... }

and my own way (wrong, off course, but i dont know whats wrong)

package Todos;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class Main extends JFrame {

private JPanel contentPane;
protected JButton btnNewButton;

/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                Main frame = new Main();
                frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });


}

/**
 * Create the frame.
 */
public Main() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.getContentPane().setLayout(new BorderLayout());
    //setDefaultLookAndFeelDecorated(false);
    //setIconImage(Image imagen);
    setTitle("");
    setSize(java.awt.Toolkit.getDefaultToolkit().getScreenSize());
    setPreferredSize(java.awt.Toolkit.getDefaultToolkit().getScreenSize());
    setLocationRelativeTo(null);

    this.btnNewButton = new JButton("New button");
    this.btnNewButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            asd(arg0);
        }
    });
    this.getContentPane().add(this.btnNewButton, BorderLayout.NORTH);
}

public void asd(ActionEvent arg0) {
    this.getContentPane().add(new JButton("asd"));
}
}

The cuestion is, why this code doesnt work, the JButton im trying to add to the JFrame with the ActionPerformed event is not visible after i click.

This is an example code, may look silly, but i think it simplifies the cuestion, since my problem is in a few lines of code its not neccesary to show you the hole proyect.

Thank you in advance!

Was it helpful?

Solution

Your problem is here:

public void asd(ActionEvent arg0) {
    this.getContentPane().add(new JButton("asd"));
}

Form Container.add() javadoc:

This method changes layout-related information, and therefore, invalidates the component hierarchy. If the container has already been displayed, the hierarchy must be validated thereafter in order to display the added component.

You need to call validate() method to make added button visible:

public void asd(ActionEvent arg0) {
    this.getContentPane().add(new JButton("asd"));
    this.getContentPane().validate();
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top