Search code examples
javaswingjframejpaneljbutton

JButton and JPanel not being shown on JFrame


I have made a program where there is a "login" button at the bottom, this was working fine for sometime before it started not showing background of the JPanel on which the JButton is placed. After trying for sometime I had discovered that the JButton gets displayed only when I hover over it.

I tried excluding specific areas of the code and I realized this glitch occurred when I initialized the variable JButton, even excluding the addAll(); method (from the code given below) makes the JPanel visible. The code is very basic and simple

public Panel() {
        this.setBackground(Color.darkGray);
        this.setSize(600,400);
        this.setVisible(true);
        addAll();
    }

    private void addAll() {
        String app = "res/files/debugging.jar";
        JButton b = new JButton();
        
        b.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent evt) {
                 if (app!=null) {
                     File file = new File(app);
                     try {
                        Desktop.getDesktop().open(file);
                        
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                 }
                }});
        b.setText("Login");
        Dimension size = b.getPreferredSize();
        b.setBounds(250,290,(int)size.getWidth(),(int)size.getHeight());
        this.setLayout(null);
        this.add(b);
    }

Panel extends the class JPanel. The JFrame being used is provided below

public static void main(String[] args) {
        JFrame frame = new JFrame();
        
        
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setResizable(false);
        frame.setTitle("Login to your Account");
        //frame.setIconImage(frameIcon);
        frame.setSize(600,400);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
        frame.add(new Panel());
    }

Solution

  • Try this.

    • I added a private class that implements the actionListener.
    • Moved file and button declarations as instance fields.
    • Returns the preferred size when getPreferredSize() is called.
    • added JFrame.pack() to size the components.

    Also, check out The Java Tutorials for many examples of using Swing as well as how to paint.

    import java.awt.Color;
    import java.awt.Desktop;
    import java.awt.Dimension;
    import java.awt.FlowLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.io.File;
    import java.io.IOException;
    
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.SwingUtilities;
    
    public class FrameDemo extends JPanel {
        JFrame frame = new JFrame();
        JButton b = new JButton();
        String app = "res/files/debugging.jar";
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(() -> new FrameDemo().start());
        }
    
        public void start() {
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            b.setText("Login");
            b.addActionListener(new MyActionListener());
            Dimension size = b.getPreferredSize();
            this.setLayout(new FlowLayout());
            add(b);
            this.add(b);
        }
        public Dimension getPreferredSize() {
            return new Dimension(600,400);
        }
        public FrameDemo() {
            frame.add(this);
            this.setBackground(Color.darkGray);
            frame.setVisible(true);
            frame.pack();
        }
    
        private class MyActionListener implements ActionListener {
            public void actionPerformed(ActionEvent evt) {
                if (app != null) {
                    File file = new File(app);
                    try {
                        Desktop.getDesktop().open(file);
    
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }