0

i make simple game , and it consist of 2 files , first file is "Alibaba.java" which is extended from JFrame , i used it to display general contents of the game !,

and the second file is "intro.java" which is extended from JPanel , i used it to show intro of the game which include (title & background & person) ,

my problem occured when i tried to add a simple button in the intro ! , i did a function to create the button , but the problem is when i run the game , the button which i added it don't appear !! , but when i tried add it from the first file which extended from JFrame , its appeared ! , so what is the problem in my code ? is JPanel don't accept JButtons ! or i must create the buttons from the JFrame file ?!

so i need to know how to add Jbutton inside Jpanel instead of add Jbutton in JFrame Direct !!,

this is my samples of my codes which contain the problem :

1st file (Alibaba.java)

package alibaba;

import java.awt.Color;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Toolkit;
import javax.swing.JFrame;


public class Alibaba extends JFrame {

    public Alibaba(){
        super("Alibaba");    


        Intro intro =   new Intro();
        this.add(intro);


        GraphicsEnvironment environment =   GraphicsEnvironment.getLocalGraphicsEnvironment();
        GraphicsDevice  device  =   environment.getDefaultScreenDevice();

        device.setFullScreenWindow(this);    
    }

    public static void main(String[] args) { 

        Alibaba alibaba =   new Alibaba();
    }
}

2nd file(Intro.java) :

package alibaba;


import javax.swing.JButton;




public class Intro extends javax.swing.JPanel implements Runnable{

    Thread _intro_run;

    public Intro() {
        _intro_run  =   new Thread(this);
        _intro_run.start();
    }


    @Override
    public void run() {

    // Here i tried to add a button to the Intro !!! 

        this.add(this.createbutton("Exit"));
    }


    public JButton createbutton(String text){

        JButton _button  = new JButton(text);

        return _button;
    }

}

So please tell me what is the problem and how to solve it , sorry but iam new to java , new to programming games world ! ,, thank you :)

3 Answers 3

3

You have to add the JButton inside the main thread, cross thread Component manipulation is bad.

For example:

public Intro() {
    JButton exitButton = new JButton("Exit");
    this.add(exitButton);
} 

Alternatively, use SwingUtilities.invokeLater(Runnable). For example, in your run method:

@Override
public void run() {
    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            add(createbutton("Exit"));
        }
    }
} 

Based on discussion it seems that you are overriding the paint or paintComponent methods. You need to call super in them, like:

void paint(Graphics g) {
    super.paint(g);
    // do other stuff to g
}
12
  • i add this but th same problem !! , no buttons appears !!
    – Jason4Ever
    Dec 2, 2011 at 18:04
  • @Jason4Ever Are you sure this is the code you run? I copied it on my machine and it shows a button.
    – Howard
    Dec 2, 2011 at 18:13
  • hello , it's samples of my original code , my original code have additional lines to draw a background image& text image and code to set color of background
    – Jason4Ever
    Dec 2, 2011 at 18:15
  • Oh if you are overriding the paint method, you NEED to call super first. Thats probably the issue. I'll update my answer.
    – jli
    Dec 2, 2011 at 18:17
  • @Jason4Ever Then I suppose the error is somewhere else in your code. Did you maybe override the paint method?
    – Howard
    Dec 2, 2011 at 18:18
3

In addition, it is a bad sign that you have a JPanel that implements Runnable. In Java, all UI work (here you are using Swing components) is done from the Event Dispatching Thread - having an actual Swing component (your Intro class is a JPanel) Runnable flies in the face of that.

0
3

Suggestions:

  • Anytime you add a new component to a container, you have to tell the layout managers to layout the new and existing components. This is done by calling revalidate() on the JPanel receiving the button. You also should call repaint() on the JPanel after this.
  • You shouldn't do any of this in a background thread.
  • Most important read the Swing tutorials as they will tell you all of this and more.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.