Results 1 to 6 of 6
  1. #1
    IAmMe is offline Member
    Join Date
    Nov 2012
    Posts
    3
    Rep Power
    0

    Question Java/AWT doing appenf from another class does not work.

    The title says it all, I'm working on this for a while and I cant solve it.
    I have 2 classes, clsMain and clsListener. The jtextarea is in the clsmain using this void:

    Java Code:
    public void consoletext(String consolestring)
        {
            textArea1.append(consolestring);
        }
    I've built the GUI using netbeans.
    Last edited by IAmMe; 11-02-2012 at 05:52 PM.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Java/AWT doing appenf from another class does not work.

    I don't think that we have enough information to allow us to solve this problem. Are you seeing any exceptions when you run your program? Could you be calling this method on the wrong instance of the clsMain class?

  3. #3
    IAmMe is offline Member
    Join Date
    Nov 2012
    Posts
    3
    Rep Power
    0

    Default Re: Java/AWT doing appenf from another class does not work.

    There are no exceptions. Here's a more accurate explanation:

    I'm creating a game server for a little multiplayer game. I've made a server GUI with netbeans and put a jTextArea in the frame.
    When the server is started, via a button, I append a "Server is starting!" message from the clsMain class, where the jtextarea is located.
    the other appends I want to show in the textarea are located in another class, clslistener, that sends a string to the consoletext function in clsmain.
    but these appends are not shown on the screen. I hope this helps.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Java/AWT doing appenf from another class does not work.

    I still think that you need to show us some code, else we're relegated to trying to read your mind and guess what is in your code not shown, and this will lead to nothing but frustration for you and us. Why don't you instead show us some code? In particular where you try to call the method you've shown above in your listener class, and most important, the listener gets a reference to the visualized GUI object. I wonder (my guess) if you're passing in a valid reference into the handler (good) or are you creating a new instance of the GUI in the handler (bad). But again, please don't make me guess as I'm horrible at this and really don't like doing it.

  5. #5
    IAmMe is offline Member
    Join Date
    Nov 2012
    Posts
    3
    Rep Power
    0

    Default Re: Java/AWT doing appenf from another class does not work.

    The GuI:

    Java Code:
    public class frmServer extends javax.swing.JFrame 
    {
    
        public frmServer() 
        {
            initComponents();
        }
    
        @SuppressWarnings("unchecked")
        // <editor-fold defaultstate="collapsed" desc="Generated Code">
        private void initComponents() {
    
            //
    
            /* Create and display the form */
            java.awt.EventQueue.invokeLater(new Runnable() 
            {
                public void run() {
                    new frmServer().setVisible(true);
                }
            });
            
                private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
            
            String input;
            input = jTextPane2.getText().toString();
            textArea1.append("\n" + input);
        }                                        
    
        private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {                                         
            
            clsListener listener = new clsListener();
            try
            {
                Thread.sleep(500);
                consoletext("Starting server..");
                new clsListener().start();
               // new clsListener().start();
                consoletext("\nServer started!");
            }
            catch(InterruptedException a)
            {
                a.printStackTrace();
                consoletext("Threaderror!");
            }
        }                                        
    
        private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {                                         
            // TODO add your handling code here:
            clsListener listener = new clsListener();
            listener.shutdown();
            consoletext("\nThe server shut down.");
            
        }                                        
    
        private void jButton7ActionPerformed(java.awt.event.ActionEvent evt) {                                         
           
            System.exit(1);
        }                                        
    
        public void consoletext(String consolestring)
        {
            textArea1.append(consolestring);
            textArea1.update(textArea1.getGraphics());
        }
        }
        // Variables declaration - do not modify
        private javax.swing.JButton jButton1;
        private javax.swing.JButton jButton2;
        private javax.swing.JButton jButton3;
        private javax.swing.JButton jButton4;
        private javax.swing.JButton jButton5;
        private javax.swing.JButton jButton6;
        private javax.swing.JButton jButton7;
        private javax.swing.JList jList1;
        private javax.swing.JPanel jPanel1;
        private javax.swing.JPanel jPanel2;
        private javax.swing.JPanel jPanel4;
        private javax.swing.JPanel jPanel5;
        private javax.swing.JScrollPane jScrollPane2;
        private javax.swing.JScrollPane jScrollPane3;
        private javax.swing.JTabbedPane jTabbedPane1;
        private javax.swing.JTabbedPane jTabbedPane4;
        private javax.swing.JTextPane jTextPane2;
        private java.awt.TextArea textArea1;
        // End of variables declaration
    }
    The listener:

    Java Code:
    import java.io.IOException;
    import java.net.ServerSocket;
    import java.lang.Thread;
    
    public class clsListener extends Thread
    {
        private ServerSocket serverSocket = null;
        private  boolean listening = true;
        
        
        public clsListener()
        {
            
        }
        
        @Override
        public final void run()
        {
            // Declarations
            frmServer frmserver = new frmServer();
            clsDatabase database = new clsDatabase();
    
            try 
            {
                serverSocket = new ServerSocket(6666);
                System.err.println("Socket created!");
                System.err.println("Socket port: " + serverSocket);
            } 
            catch (IOException e) 
            {
                frmserver.consoletext("Could not listen on port: 6666.");
            }
             
            database.connect();
    
            try
            {
                while (listening)
                {
                    new clsThreadpool(serverSocket.accept()).start();
                    Thread.sleep(20);
                }
                serverSocket.close();
            }
            catch(IOException e)
            {
                e.printStackTrace();
            }
            catch(InterruptedException a)
            {
                frmserver.consoletext("Threaderror!");
            }
        }
        
        public void shutdown()
        {
            listening = false;
            try{serverSocket.close();}
            catch(IOException e)
            {
                
            }
        }
    }

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Java/AWT doing appenf from another class does not work.

    Actually surprisingly enough, my guess was in fact correct: you're creating a new GUI object inside of your handler class that is completely unrelated to the displayed GUI:

    Java Code:
     
    public class clsListener extends Thread
    {
        // ....
         
        public clsListener()
        {
             
        }
         
        @Override
        public final void run()
        {
            // Declarations
            frmServer frmserver = new frmServer();  // *** here *** !
    What you're doing is this:

    1. First you create the GUI and display it.
    2. Then in a JButton's ActionListener you create a clsListener object.
    3. But in this clsListener's run method you create a new GUI object, one that is completely unrelated to the one that is being displayed. So calling methods on this object will have no effect on the displayed GUI. You're also doing other things wrong:
    4. You've got a Thread.sleep in your Swing GUI code that is being called on the event thread. Never do this as this does nothing but puts the entire application to sleep and makes it unresponsive -- not what you want.
    5. You're making Swing calls to the GUI from within a background thread. You don't want to do this but rather want to make these calls on the Swing event thread, the EDT.


    To solve your problem, give your clsListener class a frmServer field that is initially set to null. Give the clsListener constructor a frmServer parameter that you use to set the frmServer field. When you call the clsListener constructor from within frmServer, pass in a reference to the current frmServer object, this. Then use this reference from within the clsListener to call GUI methods on the active visible GUI object.

    Java Code:
     
    public class clsListener extends Thread
    {
        // ....
        frmServer frmserver = null;
         
        public clsListener(frmServer frmserver)
        {
           // assign the reference
           this.frmserver = frmserver    
        }
         
        @Override
        public final void run()
        {
            // Declarations
            //  frmServer frmserver = new frmServer();  // *** get rid of this ***

    Also, you'll want to read up on threading and Swing. I suggest the tutorial Concurrency in Swing.

Similar Threads

  1. How does the ColorModel class work?
    By TacoManStan in forum Java 2D
    Replies: 6
    Last Post: 02-03-2012, 10:10 PM
  2. Replies: 6
    Last Post: 02-24-2011, 11:35 PM
  3. How can I make this Java class work
    By ruchir123 in forum New To Java
    Replies: 2
    Last Post: 12-11-2010, 12:53 AM
  4. class that wont work out properly.
    By vampire-elf in forum New To Java
    Replies: 7
    Last Post: 09-07-2010, 02:39 AM
  5. repaint class doesnt work anymore... dunno why..
    By Addez in forum New To Java
    Replies: 9
    Last Post: 11-07-2009, 10:10 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •