Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7551123
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T10:23:55+00:00 2026-05-30T10:23:55+00:00

So i have a Jframe with a JPanel in it, and there are some

  • 0

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);
}
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T10:23:56+00:00Added an answer on May 30, 2026 at 10:23 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I Have two files. One extends JFrame, and another Extends JPanel. Whenever I change
I have 1 JFrame and 10 JPanel components as seperate classes. There is also
I have a JFrame that contains a display JPanel with JTextField and a control
I have a JFrame with a menu bar and a canvas covering all the
I have a JFrame. This JFrame contains a JButton. I click the JButton and
I have a JFrame and I open a JDialog from it and another JDialog
This one's a tough one - I have a JFrame that generates JTextFields. When
I have a JFrame with an associated JPanel which fill the screen, both having
I've created a frame with a 3x3 grid of buttons using this code... JFrame
I have a JFrame which contains a JPanel . The frame can be resized

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.