0

I am trying to make a panel showing a printed statement "Hello World!" and an OK button. Neither will show up on the panel and I have no idea why. I started with a block of code that was supposed to create just a blank popup. The blank popup worked great. I can't add the string or button and see them. I have tried calling paintComponent. I have tried adding the content to the panel. Does anyone know what I am missing?

Here is my code


package painting;

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

public class SwingPaintDemo1 {
        
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    createAndShowGUI();
                }
            });
        }
        
        private static class SwingPaintDemo extends JPanel{
            public void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.drawString("Hello World!", 20,30);
        }
        }
        
        private static void createAndShowGUI() {
            System.out.println("Created GUI on EDT? "+
                    SwingUtilities.isEventDispatchThread());
            JFrame f = new JFrame("Swing Paint Demo");
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.setSize(250,250);
            f.setVisible(true);
            
            JButton okbutton = new JButton("OK");
            ButtonHandler listener = new ButtonHandler();
            okbutton.addActionListener(listener);
            
            SwingPaintDemo displayPanel = new SwingPaintDemo();
            JPanel content = new JPanel();
            content.setLayout(new BorderLayout());
            content.add(displayPanel, BorderLayout.CENTER);
            content.add(okbutton, BorderLayout.SOUTH);
        }
        
        private static class ButtonHandler implements ActionListener{
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        }
    }
camickr
  • 321,443
  • 19
  • 166
  • 288
user-kk-
  • 11
  • 2
  • If [one of the answers](https://stackoverflow.com/questions/63419277/what-am-i-generating-when-i-generate-a-java-calendar-object) resolved your issue, you can help the community by marking that as accepted. An accepted answer helps future visitors use the solution confidently. – Arvind Kumar Avinash Oct 23 '20 at 21:09

1 Answers1

2

You forgot to add the JPanel to the JFrame. Just add the following line at the bottom of your createAndShowGUI() method:

f.add(content);

I would also recommend moving your f.setVisible(true); line to the bottom of the method just to be safe. When you make the frame visible, the component tree is set up to take into account all the components added to the JFrame. If you add more components after that, you will need to do either manually revalidate the tree or do something that triggers an automatic revalidation. I'm assuming you're not revalidating your tree anywhere, so you should move f.setVisible(true); to after all the components are added.

Charlie Armstrong
  • 2,332
  • 3
  • 13
  • 25
  • 1
    *just to be safe.* The "content" panel needs to be added to the frame BEFORE the frame is made visible otherwise the size of the panel will be (0, 0) since the layout manager will not be invoked. . – camickr Jul 10 '20 at 02:28
  • Thanks, just realized that myself. Made an edit. It is also possible that they revalidate somewhere, though. I'll explain a little more in another edit. – Charlie Armstrong Jul 10 '20 at 02:29