Petr Safar
Petr Safar

Reputation: 107

JPanel content goes black after opening it from another Jframe

So i have a Jframe with a JPanel in it, and there are some buttons on there. when a button is pressed it opens up another Jframe with another Jpanel in it, but the content of this Jframe is all black. There are supposed to be 4 buttons, and when i open up this second Jframe individually, it shows and works just as it's supposed to. Could this ave anything to do with the fact that i used a thread.wait on the second one inside a while(frame.visible)? Any help would be greatly appreciated.

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

/**
 * Created by IntelliJ IDEA.
 * User: ThePetr
 * Date: 17/02/12
 * Time: 19:28
 * To change this template use File | Settings | File Templates.
 */
public class SelectKleurUI {
    private  JFrame frm = new JFrame("Kies kleur");
    private int gekozenKleur;

    public SelectKleurUI() {
        frm = new JFrame();
        Toolkit kit = frm.getToolkit();
        GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
        GraphicsDevice[] gs = ge.getScreenDevices();
        Insets in = kit.getScreenInsets(gs[0].getDefaultConfiguration());
        Dimension d = kit.getScreenSize();
        int max_width = (d.width - in.left - in.right);
        int max_height = (d.height - in.top - in.bottom);
        frm.setSize(Math.min(max_width, 400), Math.min(max_height, 64));//whatever size you want but smaller the insets
        frm.setLocation((max_width - frm.getWidth()) / 2, (max_height - frm.getHeight() ) / 2);
        //frm.setUndecorated(true);
        frm.setResizable(false);
        frm.setVisible(true);

        frm.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
        maakKnoppen();
    }

    public  int selectKleur(){

        while(frm.isVisible()){
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
            }
        }
        frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frm.dispose();
        return gekozenKleur;
    }

    public void setKleur(int kleur){
        gekozenKleur=kleur;
        frm.dispose();
    }
    private  void maakKnoppen(){
        JPanel knoppenFrame = new JPanel(new GridLayout(1,0));
        JButton geel = new JButton("Geel");
        geel.setBackground(Color.yellow);
        geel.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                setKleur(0);
                //To change body of implemented methods use File | Settings | File Templates.
            }
        });
        JButton groen = new JButton("Groen");
        groen.setBackground(Color.green);
        groen.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                setKleur(1);
                //To change body of implemented methods use File | Settings | File Templates.
            }
        });
        JButton blauw = new JButton("Blauw");
        blauw.setBackground(Color.blue);
        blauw.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                setKleur(2);
                //To change body of implemented methods use File | Settings | File Templates.
            }
        });
        JButton rood = new JButton("Rood");
        rood.setBackground(Color.red);
        rood.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                setKleur(3);
                //To change body of implemented methods use File | Settings | File Templates.
            }
        });
        knoppenFrame.add(geel);
        knoppenFrame.add(groen);
        knoppenFrame.add(blauw);
        knoppenFrame.add(rood);
        frm.add(knoppenFrame,BorderLayout.NORTH);

    }
}

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

/**
 * Created by IntelliJ IDEA.
 * User: ThePetr
 * Date: 17/02/12
 * Time: 14:55
 * To change this template use File | Settings | File Templates.
 */
public class Uno {
    private JFrame Hoofdvenster;
    private JPanel pnlOnder=new JPanel(new GridLayout(1,0));
    private JButton[] kaarten=new JButton[50];
    Uno(){
        Hoofdvenster = new JFrame();
        Hoofdvenster.setName("Uno");
        Toolkit kit = Hoofdvenster.getToolkit();
        GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
        GraphicsDevice[] gs = ge.getScreenDevices();
        Insets in = kit.getScreenInsets(gs[0].getDefaultConfiguration());
        Dimension d = kit.getScreenSize();
        int max_width = (d.width - in.left - in.right);
        int max_height = (d.height - in.top - in.bottom);
        Hoofdvenster.setSize(Math.min(max_width, 800), Math.min(max_height, 600));//whatever size you want but smaller the insets
        Hoofdvenster.setLocation((max_width - Hoofdvenster.getWidth()) / 2, (max_height - Hoofdvenster.getHeight() ) / 2);
        maakComponenten();
        Hoofdvenster.setVisible(true);
        Hoofdvenster.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    private void maakComponenten(){
        for(int i=0;i<7;i++){
            //kaarten[i]=new JButton("Kaart "+i);
            ImageIcon btnIcon = createImageIcon("Images/Naamloos.gif");//new ImageIcon("./Images/Naamloos.gif");

            kaarten[i]=new JButton("Kaart "+(i+1),btnIcon);
            kaarten[i].addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    //JOptionPane.showMessageDialog(null, "Ok");
                    Hoofdvenster.setVisible(false);
                    SelectKleurUI selectKleurUI = new SelectKleurUI();
                    JOptionPane.showMessageDialog(null, ""+ selectKleurUI.selectKleur());
                    Hoofdvenster.setVisible(true);

                }
            });
            pnlOnder.add(kaarten[i]);


        }
        Hoofdvenster.add(pnlOnder,BorderLayout.SOUTH);
    }
    private static ImageIcon createImageIcon(String path) {
    java.net.URL imgURL = Uno.class.getResource(path);

    return new ImageIcon(imgURL);
}
}

Upvotes: 1

Views: 841

Answers (1)

Gilbert Le Blanc
Gilbert Le Blanc

Reputation: 51543

All calls to Swing components have to be on the event dispatch thread. Swing components are not thread safe.

Your application should only have one JFrame. You can have as many JPanels as you want in the one and only JFrame.

Upvotes: 3

Related Questions