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 8478831
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T18:51:22+00:00 2026-06-10T18:51:22+00:00

I have a main window with 4 JButtons and an action listener, and three

  • 0

I have a main window with 4 JButtons and an action listener, and three of them call another window and the fourth exits. Two of my windows work fine, but for some reason my window that waits for a client to connect opens and you can see the boundary of the window, but the inside of the window is transparent.
I tried saying new HostWindow() from my main class, and that worked fine; it’s just when I call it from my StartWindow class that it doesn’t work.
code:

StartWindow:

public class StartWindow extends JFrame{
private JPanel pane;
private JButton host;
private JButton join;
private JButton comp;
private JButton exit;

public StartWindow()
{
    this.setSize(220, 110);
    this.setTitle("Closed Arena");
    pane = new JPanel();
    this.add(pane);
    host = new JButton("Host Match");
    host.addActionListener(new myButtonListener());
    join = new JButton("Join Match");
    join.addActionListener(new myButtonListener());
    comp = new JButton("Play Computer");
    comp.addActionListener(new myButtonListener());
    exit = new JButton("Exit");
    exit.addActionListener(new myButtonListener());
    pane.add(host);
    pane.add(join);
    pane.add(comp);
    pane.add(exit);
    this.setResizable(false);
    pane.setVisible(true);
    this.setVisible(true);
    this.setDefaultCloseOperation(EXIT_ON_CLOSE);   

    //If I make a new Hostwindow here it displays properly
    //new HostWindow();
}

private class myButtonListener implements ActionListener
{

    @Override
    public void actionPerformed(ActionEvent e) 
    {
        Object source = e.getSource();
        if(source.equals(host))
        {
            close();
            //But here it displays improperly.
            new HostWindow();
        }
        if(source.equals(join))
        {
            close();
            new JoinWindow();
        }
        if(source.equals(comp))
        {
            close();
            new Arena();
        }
        if(source.equals(exit))
        {
            close();
        }
    }   
}
public void close()
{
    this.dispose();
}
}

HostWindow:

private JPanel panel;
private JLabel text;
private JButton stop;
private LabelEditor edit;
private Thread editThread;
private ServerSocket server;
private Socket mySocket;

public HostWindow()
{
    panel = new JPanel();

    text = new JLabel("Waiting for client");
    stop = new JButton("Stop");
    stop.addActionListener(new buttonList());

    panel.add(text);
    panel.add(stop);
    this.add(panel);

    this.setResizable(false);   
    this.setSize(160, 90);
    this.setTitle("Server");

    this.setVisible(true);
    this.setDefaultCloseOperation(EXIT_ON_CLOSE);

    edit = new LabelEditor(text, "Waiting for client", 700);
    editThread = new Thread(edit);
    editThread.start();

    try 
    {
        server = new ServerSocket(4011);
        mySocket = server.accept();
        server.close();
        new Arena(mySocket, true);
    } 
    catch (IOException e) {
        System.out.print("Failed to set up server!");
    }
    editThread.interrupt();
    this.dispose();
}

Edit: HostWindow does extend JFrame, I jsut didn’t copy-paste the heading, but it looks like this:
public class HostWindow extends JFrame {

Edit2: Thank you answerers, it was fixed when I made it so that the serer starts in a separate thread:

if(source.equals(host))
        {
            close();
            HostWindow hoster = new HostWindow();
            Thread hosterThread = new Thread(hoster);
            hosterThread.start();
        }

and in hostwindow:
I moved the server stuff into the run.

public void run() {
    try 
    {
        server = new ServerSocket(4011);
        mySocket = server.accept();
        server.close();
        new Arena(mySocket, true);
    } 
    catch (IOException e) {
        System.out.print("Failed to set up server!");
    }
    editThread.interrupt();
    this.dispose();     
}
  • 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-06-10T18:51:23+00:00Added an answer on June 10, 2026 at 6:51 pm

    The problem here is that you’re performing a long lasting blocking operation inside the UIThread.

    Threads are simultanous sequences of commands that run inside a single program (I recommend reading up on Java concurrency ). There is a thread usually refered to as the UIThread that performs drawing of the UI elements and works with their code.

    Creating a socket – a connection to a remote host means you’re starting a process that can take several seconds to perform. And this is happening inside the constructor of the UI class, inside the UIThread. Until the connection is established the rest of the code in the constructor cannot run. This is because the connection process is a blocking operation – the code after the socket creation won’t run until the socket creation is finishsed.

    So you should move the socket creation to a different thread.

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

Sidebar

Related Questions

I have a main window, that opens another window. I want to close this
I have a main window where all the work is done. When i open
I have a main window which hosts two views (user controls). One is UserControl1
I have main window which has inner grid components. When I press a button
I have a main window with a Gtk Button named openDialog. If I click
I have a main menu window. On clicking any menu item it opens a
I have a window(say main window) with a frame which has a page in
I have a page viewer as main window showing an external page as we
i have a mvvm app that the main window is a tab control. i
I have an application with a main window that loads in text file(s) and

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.