1

I am using Java 11 on Debian 4. I am trying to build a very basic Java GUI. To start with I have the following code:

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

public class BasicSwing extends JFrame {
    JPanel p = new JPanel();
    JButton b = new JButton("Hello");

    public static void main (String[] args) {
       new BasicSwing();
    }
    
    public BasicSwing() {
        super("Basic Swing");
        setSize(400,300);
        setResizable(true);
        setDefaultCloseOperation(EXIT_ON_CLOSE);

        p.add(b);
        add(p);
        setVisible(true);
    }
}

I have the X11 server running. The code does not fail but the GUI does not show up. I am not using Netbeans and I compile and run the code just as I would run and compile any other java code, ie with javac and java commands.The code does not stop and does not throw any error. Am I missing something very basic? I have seen a lot of discussion on the GUI not showing up but I am unable to find a solution to this problem given my specific development environment.

Naruto
  • 63
  • 9
  • `JPanel p = new JPanel();` please edit this and re run the code again – MK.Whridoy Jul 11 '22 at 17:26
  • I am sorry, the "-" was a typo because I could not copy paste the code. It is an = in the code, so I do not have any compilation error – Naruto Jul 12 '22 at 09:16
  • worksforme - probably because this here is different from your real code: _was a typo because I could not copy paste the code_ why not? It's always possible to write a [mcve] demonstrating what's wrong and c&p that example .. – kleopatra Jul 12 '22 at 09:41

1 Answers1

0

Instead of calling the setVisible method inside of your JFrame extended class's constructor, You should make a call on it in your main function.

Do it this way:

public static void main (String[] args) {
       EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    BasicSwing mySwingApp = new BasicSwing();
                    mySwingApp.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
}

Please read more about why I use a java.awt.EventQueue here.

Update:

It's not good practice to directly create a class that extends JFrame. Also, please read this too, for more clarification.

  • 1
    Using EventQueue is correct, but your first sentence has nothing to do with the problem. – VGR Jul 11 '22 at 21:53
  • @VGR Displaying from a constructor is generally a bad idea... But in this particular case, yes, it has something to do with the problem. When you extend 'JFrame,' you won't be able to call 'setVisisble' inside the constructor. Read [this Stackoverflow answer](https://stackoverflow.com/a/14603910/11121568) for more info. – Soroush Shemshadi Jul 12 '22 at 04:05
  • 1
    you mis-interpreted the QA you cited: in the cited https://stackoverflow.com/a/14603910/11121568 there are __two__ frames: the extended and an internal (which is a bit on the weird side) - it's the internal that's populated in the constructor. Naturally, showing the outer is just showing an empty window :) _When you extend 'JFrame,' you won't be able to call 'setVisisble' inside the constructor_ is plain wrong (as you should notice on running the code) and @VGR is correct: all you state in the answer is fine, but unrelated to the problem :) – kleopatra Jul 12 '22 at 09:52
  • The solution given by @SoroushShemshadi is not yet working on my system. I will have to debug and update here. – Naruto Jul 12 '22 at 14:01
  • The solution was not working because of specific permission issues in the platform I was using. – Naruto Jul 15 '22 at 17:05