-2

I built a Java-Swing GUI using the WindowBuilder in Eclipse. But when I try to add new components using the .add() and .revalidate() nothing happens.

If someone could help to fix this issue I realy would apreciate it.

package Frame;

import java.awt.EventQueue;

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

public class TestFrame {

    private JFrame frame;

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

    /**
     * Create the application.
     */
    public TestFrame() {
        initialize();
    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        frame = new JFrame();
        frame.setBounds(100, 100, 450, 300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


        JButton btnSampleButton = new JButton("Sample Button");
        btnSampleButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                frame.add(new JButton("BTN"));
                frame.revalidate();
                frame.repaint();
            }
        });
        frame.getContentPane().setLayout(null);
        btnSampleButton.setBounds(110, 126, 185, 112);
        frame.getContentPane().add(btnSampleButton);
    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
T.Furholzer
  • 199
  • 1
  • 8
  • Please post your code in form of [SSCCE](http://sscce.org), so we can see what's wrong. – Sergiy Medvynskyy Feb 06 '17 at 15:22
  • Ok posted my code – T.Furholzer Feb 06 '17 at 15:36
  • 1) Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). 2) Adding components after a GUI is visible requires some tricks. Rather than learrn those tricks, better to use a [`CardLayout`](http://download.oracle.com/javase/8/docs/api/java/awt/CardLayout.html) .. – Andrew Thompson Feb 06 '17 at 21:24
  • .. as shown in [this answer](http://stackoverflow.com/a/5786005/418556). That way, the size of the new component is used to calculate the preferred size of the GUI. In one of the cards, use a blank panel. In the other, the button. – Andrew Thompson Feb 06 '17 at 21:25

1 Answers1

1

Try following:

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;

public class TestFrame {

    private JFrame frame;

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

    /**
     * Create the application.
     */
    public TestFrame() {
        initialize();
    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        frame = new JFrame();
        frame.setBounds(100, 100, 450, 300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


        JButton btnSampleButton = new JButton("Sample Button");
        btnSampleButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent arg0) {
                JButton btn = new JButton("BTN");
                btn.setSize(btn.getPreferredSize());
                btn.setLocation(new Point(1, 1));
                frame.add(btn);
                frame.revalidate();
                frame.repaint();
            }
        });
        frame.getContentPane().setLayout(null);
        btnSampleButton.setBounds(110, 126, 185, 112);
        frame.getContentPane().add(btnSampleButton);
    }
}

Try to learn Layout Managers. When you use an appropriate layout manager you shouldn't set the component's size/location.

Sergiy Medvynskyy
  • 11,160
  • 1
  • 32
  • 48