Search code examples
javaswingjframewindowawt

JButton only appears after i hover over it


The second button isnt visible before i hover over it.

I know there has been answers on this question for JPanels, but they dont seem to work for me.

I have the following code:

Main class

public class Main {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            Window w = new Window();
        });
    }
}

My custom Window

public class Window implements ActionListener {

    JFrame f;
    JButton b1, b2;
    JRootPane jRootPane;

    public Window() {
        f = new JFrame("Ceaser Verschluesselung");
        f.setPreferredSize(new Dimension(480, 150));
        f.setResizable(false);
        f.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
        f.setLocation(720, 300);
        f.setLayout(null);
        jRootPane = f.getRootPane();

        b1 = new JButton("Verschlüsseln");
        b2 = new JButton("Entschlüsseln");
        b1.setSize(new Dimension(220, 100));
        b1.setLocation(7, 5);
        b1.setFont(new Font("1", Font.BOLD, 25));
        b1.addActionListener(this);
        b2.setSize(new Dimension(220, 100));
        b2.setLocation(237, 5);
        b2.setFont(new Font("1", Font.BOLD, 25));
        b2.addActionListener(this);
        jRootPane.add(b1);
        jRootPane.add(b2);

        f.pack();
        f.setVisible(true);
    }

    public void setVisibility(boolean b) {
        f.setVisible(b);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getActionCommand().equals("Verschlüsseln")) {
            new Encoder(this);
        } else if (e.getActionCommand().equals("Entschlüsseln")) {
            new Decoder();
        }
    }
}

I had this issue on other projects before but the SwingUtilities.invokeLater() to run the Window fixed it. On the other thread with JPanels i figuered out that the Buttons disappear when they collaps but i tried it with more narrow buttons which didnt change anything at all.

I dont add the Encoder and Decoder classes, i dont think its necessary until someone proves me wrong :D


Solution

  • Read How to Use RootPanes. You should not add components into RootPane. Instead, you should add them to content pane:

    Container contentPane;
    //....
    contentPane = f.getContentPane();
    

    The whole class:

    public class Window implements ActionListener {
    
        JFrame f;
        JButton b1, b2;
        Container contentPane;
    
        public Window() {
            f = new JFrame("Ceaser Verschluesselung");
            f.setPreferredSize(new Dimension(480, 150));
            f.setResizable(false);
            f.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
            f.setLocation(720, 300);
            f.setLayout(null);
            contentPane = f.getContentPane();
    
            b1 = new JButton("Verschlüsseln");
            b2 = new JButton("Entschlüsseln");
            b1.setSize(new Dimension(220, 100));
            b1.setLocation(7, 5);
            b1.setFont(new Font("1", Font.BOLD, 25));
            b1.addActionListener(this);
            b2.setSize(new Dimension(220, 100));
            b2.setLocation(237, 5);
            b2.setFont(new Font("1", Font.BOLD, 25));
            b2.addActionListener(this);
            contentPane.add(b1);
            contentPane.add(b2);
    
            f.pack();
            f.setVisible(true);
        }
    
        public void setVisibility(boolean b) {
            f.setVisible(b);
        }
    
        @Override
        public void actionPerformed(ActionEvent e) {
            if (e.getActionCommand().equals("Verschlüsseln")) {
    
            } else if (e.getActionCommand().equals("Entschlüsseln")) {
            }
        }
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(() -> new Window());
        }
    }
    

    Also, using setLayout(null) is bad (bad) practice. Use layout managers instead. It will make your life easier, plus your GUI will be more user-friendly since it will support resizing.