Question

I have a GUIPanel already created and I need to add it to a program I have, however; when I try to run the other program from within the main class the GUIPanel won't run, yet it will when I don't execute it(if that makes any sense).

This is what happens when I run the program This is what happens when I run the program

This is the GUIPanel I want the output to print to

I want the output to print to this GUIPanel(I had to remove the call to TourServer to get this to run), if you're thinking "Why not just leave it in the command prompt, you don't have anything in your GUIPanel other than the text area anyway?", well that would be because I plan to add more to it later :D

In summary: I need to run the TourServer Program from the GUIPanel class, and when I try to it doesn't open the GUI panel itself

Here is the main class:

import java.net.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.awt.*;
import javax.swing.*;
import java.io.*;


/**
 *
 * @author Dan
 */
public class GUIPanel extends JFrame {
    private PrintStream outStream;
    /**
     * Creates new form GUIPanel
     */
    public GUIPanel() {
        initComponents();
    }
    private void init() throws IOException {
        jTabbedPane1.add("Main", jPanel);
        jPanel.add(textArea1);
        setOutputStream();
        TourServer.init(cmd);//Calling the program, I'm assuming I might be doing this wrong.
    }
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    private void initComponents() {

        textArea1 = new java.awt.TextArea();
        jPanel = new javax.swing.JPanel();
        jTabbedPane1 = new javax.swing.JTabbedPane();



        textArea1.setPreferredSize(new java.awt.Dimension(432, 300));



        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Game Manager");
        setBounds(new java.awt.Rectangle(0, 0, 400, 450));
        setPreferredSize(new java.awt.Dimension(450, 420));
        getContentPane().setLayout(new java.awt.FlowLayout());

        try {
            init();
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }
        jTabbedPane1.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        jTabbedPane1.setFocusable(false);
        jTabbedPane1.setOpaque(true);
        getContentPane().add(jTabbedPane1);
        jTabbedPane1.getAccessibleContext().setAccessibleName("");

        pack();
    }




    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
    cmd = args;
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
    } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | javax.swing.UnsupportedLookAndFeelException ex) {  java.util.logging.Logger.getLogger(GUIPanel.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }


        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new GUIPanel().setVisible(true);
            }
        });
    }

    private static String[] cmd;
    private javax.swing.JPanel jPanel;
    private javax.swing.JTabbedPane jTabbedPane1;
    private java.awt.TextArea textArea1;

      //Written by MadProgrammer
        public class CapturePane extends JPanel implements Consumer {

        private TextArea output;

        public CapturePane() {
            jPanel.setLayout(new BorderLayout());
            jPanel.add(new JScrollPane(output));
        }

        @Override
        public void appendText(final String text) {
            if (EventQueue.isDispatchThread()) {
                output.append(text);
                output.setCaretPosition(output.getText().length());
            } else {

                EventQueue.invokeLater(new Runnable() {
                    @Override
                    public void run() {
                        appendText(text);
                    }
                });

            }        
    }
 }

    public interface Consumer {        
        public void appendText(String text);        
    }

    public class StreamCapturer extends OutputStream {

        private StringBuilder buffer;

        public StreamCapturer() {
            buffer = new StringBuilder(128);
            buffer.append("");
        }

        @Override
        public void write(int b) throws IOException {
            char c = (char) b;
            String value = Character.toString(c);
            buffer.append(value);
            if (value.equals("\n")) {
                textArea1.appendText(buffer.toString());
                buffer.delete(0, buffer.length());
            }
        }        
    }  

}

And here is the TourServer Class

         // TourServer.java
 /*
   The top-level Tour server, which waits for client connections
   and creates TourServerHandler threads to handle them.

   Details about each client are maintained in a TourGroup object
   which is referenced by each thread.

   Very similar to the multithreaded Chat server.
*/

import java.net.*;
import java.io.*;


public class TourServer
{
  static int PORT = 0;

  private TourGroup tg;
  static GameSession gameSession=new GameSession();
  public TourServer()
  // wait for a client connection, spawn a thread, repeat
  {
    tg = new TourGroup();
    try {
      ServerSocket serverSock = new ServerSocket(PORT);
      Socket clientSock;

      while (true) {
        new GUIPanel("Waiting for a client...");
        clientSock = serverSock.accept();
        new TourServerHandler(clientSock, tg).start();
      }
    }
    catch(Exception e)
    {  e.printStackTrace(); }
  }  // end of TourServer()


  // -----------------------------------

  public static void init(String args[]) {
      if (Integer.parseInt(args[0]) > 0) {
        PORT = Integer.parseInt(args[0]);
      } else {
          PORT = 5550;
      }
        new GUIPanel("Port set to: "+PORT);
        new TourServer();
    }
} // end of TourServer class
Was it helpful?

Solution

You have this constructor in your GUI class:

public GUIPanel() {
        initComponents();
    }

Add these 3 functions after your init function:

public GUIPanel() {
        initComponents();
        validate();
        pack();
        show();
    }

I'm sure the window will appear afterwards. Don't stop at this point, but try to improve this solution, because at least one of these functions are deprecated, if I memory serves. Sorry for this, but I haven't really used Swing since Java 1.4.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top