Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. Java
  4. Swing

Swing

Scheduled Pinned Locked Moved Java
helpjavasysadmindata-structures
15 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    Joshua Waring
    wrote on last edited by
    #1

    First lets give some context, I've just created a client and server but they don't work without using console So I've decided to create a GUI on top they'll communicate though the Stack, but that's all for a later date, right now I get a error on the Even listener which says that the JTextField is empty upon .getText() even though it's not. I've looked though it, but since I'm quite new to the whole Swing field, it's a challenge for me to fix the problem. It really is a simple GUI.

    import java.awt.Button;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.util.Stack;

    import javax.swing.*;
    public class GUI extends JFrame implements ActionListener{
    Stack incoming = null;
    Stack outgoing = null;
    JTextField input;
    JTextArea output;
    Button button;
    public GUI(){

    	setTitle("Satire v1.0.1");
    	setSize(320,360);
    	setLocationRelativeTo(null);
    	setDefaultCloseOperation(EXIT\_ON\_CLOSE);
    	
    	JPanel panel = new JPanel();
    	panel.setLayout(null);
    	
    	output = new JTextArea();
    	output.setBounds(0, 0, 300, 300);
    	output.setEditable(false);
    	
    	input = new JTextField();
    	input.setBounds(0, 300, 260, 20);
    	
    	button = new Button("SEND");
    	button.setBounds(250, 300, 50, 20);
    	button.addActionListener(this);
    	
    	getContentPane().add(panel);
    	panel.add(button);
    	panel.add(input);
    	panel.add(output);
    }
    public void actionPerformed(ActionEvent e) { 
    	GUI gui = new GUI();
    	try{
    		String out = gui.input.getText();
    		gui.input.selectAll();
    		outgoing.add(out);
    	}catch(NullPointerException e3){}
    	gui.output.append(outgoing.pop());
    }
    
    public static void main(String\[\] args){
    	 SwingUtilities.invokeLater(new Runnable() {
    		 public void run(){
    			 GUI cGUI = new GUI();
    			 cGUI.setVisible(true);
    		 }
    	 });
    }
    

    }

    P G 4 Replies Last reply
    0
    • J Joshua Waring

      First lets give some context, I've just created a client and server but they don't work without using console So I've decided to create a GUI on top they'll communicate though the Stack, but that's all for a later date, right now I get a error on the Even listener which says that the JTextField is empty upon .getText() even though it's not. I've looked though it, but since I'm quite new to the whole Swing field, it's a challenge for me to fix the problem. It really is a simple GUI.

      import java.awt.Button;
      import java.awt.event.ActionEvent;
      import java.awt.event.ActionListener;
      import java.util.Stack;

      import javax.swing.*;
      public class GUI extends JFrame implements ActionListener{
      Stack incoming = null;
      Stack outgoing = null;
      JTextField input;
      JTextArea output;
      Button button;
      public GUI(){

      	setTitle("Satire v1.0.1");
      	setSize(320,360);
      	setLocationRelativeTo(null);
      	setDefaultCloseOperation(EXIT\_ON\_CLOSE);
      	
      	JPanel panel = new JPanel();
      	panel.setLayout(null);
      	
      	output = new JTextArea();
      	output.setBounds(0, 0, 300, 300);
      	output.setEditable(false);
      	
      	input = new JTextField();
      	input.setBounds(0, 300, 260, 20);
      	
      	button = new Button("SEND");
      	button.setBounds(250, 300, 50, 20);
      	button.addActionListener(this);
      	
      	getContentPane().add(panel);
      	panel.add(button);
      	panel.add(input);
      	panel.add(output);
      }
      public void actionPerformed(ActionEvent e) { 
      	GUI gui = new GUI();
      	try{
      		String out = gui.input.getText();
      		gui.input.selectAll();
      		outgoing.add(out);
      	}catch(NullPointerException e3){}
      	gui.output.append(outgoing.pop());
      }
      
      public static void main(String\[\] args){
      	 SwingUtilities.invokeLater(new Runnable() {
      		 public void run(){
      			 GUI cGUI = new GUI();
      			 cGUI.setVisible(true);
      		 }
      	 });
      }
      

      }

      P Offline
      P Offline
      pasztorpisti
      wrote on last edited by
      #2

      You are new to object oriented programming as well, arent you? :-) Instead of this:

      public void actionPerformed(ActionEvent e) { 
      	GUI gui = new GUI();
      	try{
      		String out = gui.input.getText();
      		gui.input.selectAll();
      		outgoing.add(out);
      	}catch(NullPointerException e3){}
      	gui.output.append(outgoing.pop());
      }
      

      Use this:

      public void actionPerformed(ActionEvent e) { 
      	try{
      		String out = input.getText();
      		input.selectAll();
      		outgoing.add(out);
      	}catch(NullPointerException e3){}
      	output.append(outgoing.pop());
      }
      
      J 1 Reply Last reply
      0
      • P pasztorpisti

        You are new to object oriented programming as well, arent you? :-) Instead of this:

        public void actionPerformed(ActionEvent e) { 
        	GUI gui = new GUI();
        	try{
        		String out = gui.input.getText();
        		gui.input.selectAll();
        		outgoing.add(out);
        	}catch(NullPointerException e3){}
        	gui.output.append(outgoing.pop());
        }
        

        Use this:

        public void actionPerformed(ActionEvent e) { 
        	try{
        		String out = input.getText();
        		input.selectAll();
        		outgoing.add(out);
        	}catch(NullPointerException e3){}
        	output.append(outgoing.pop());
        }
        
        J Offline
        J Offline
        Joshua Waring
        wrote on last edited by
        #3

        I have made the edit, yes I'm new, but learning fast, a lot faster than expected. though the edit has been made, there was no change to the error. What I don't understand is that the field isn't empty D:

        1 Reply Last reply
        0
        • J Joshua Waring

          First lets give some context, I've just created a client and server but they don't work without using console So I've decided to create a GUI on top they'll communicate though the Stack, but that's all for a later date, right now I get a error on the Even listener which says that the JTextField is empty upon .getText() even though it's not. I've looked though it, but since I'm quite new to the whole Swing field, it's a challenge for me to fix the problem. It really is a simple GUI.

          import java.awt.Button;
          import java.awt.event.ActionEvent;
          import java.awt.event.ActionListener;
          import java.util.Stack;

          import javax.swing.*;
          public class GUI extends JFrame implements ActionListener{
          Stack incoming = null;
          Stack outgoing = null;
          JTextField input;
          JTextArea output;
          Button button;
          public GUI(){

          	setTitle("Satire v1.0.1");
          	setSize(320,360);
          	setLocationRelativeTo(null);
          	setDefaultCloseOperation(EXIT\_ON\_CLOSE);
          	
          	JPanel panel = new JPanel();
          	panel.setLayout(null);
          	
          	output = new JTextArea();
          	output.setBounds(0, 0, 300, 300);
          	output.setEditable(false);
          	
          	input = new JTextField();
          	input.setBounds(0, 300, 260, 20);
          	
          	button = new Button("SEND");
          	button.setBounds(250, 300, 50, 20);
          	button.addActionListener(this);
          	
          	getContentPane().add(panel);
          	panel.add(button);
          	panel.add(input);
          	panel.add(output);
          }
          public void actionPerformed(ActionEvent e) { 
          	GUI gui = new GUI();
          	try{
          		String out = gui.input.getText();
          		gui.input.selectAll();
          		outgoing.add(out);
          	}catch(NullPointerException e3){}
          	gui.output.append(outgoing.pop());
          }
          
          public static void main(String\[\] args){
          	 SwingUtilities.invokeLater(new Runnable() {
          		 public void run(){
          			 GUI cGUI = new GUI();
          			 cGUI.setVisible(true);
          		 }
          	 });
          }
          

          }

          P Offline
          P Offline
          pasztorpisti
          wrote on last edited by
          #4

          There you go. This time I compiled/tested it, contained another big mistake. Why have you used those stacks? You should check the error messages as well in the console output!!! You program threw exceptions!

          import java.awt.Button;
          import java.awt.event.ActionEvent;
          import java.awt.event.ActionListener;
          import java.util.Stack;

          import javax.swing.*;
          public class GUI extends JFrame implements ActionListener{
          JTextField input;
          JTextArea output;
          Button button;
          public GUI(){

          	setTitle("Satire v1.0.1");
          	setSize(320,360);
          	setLocationRelativeTo(null);
          	setDefaultCloseOperation(EXIT\_ON\_CLOSE);
          	
          	JPanel panel = new JPanel();
          	panel.setLayout(null);
          	
          	output = new JTextArea();
          	output.setBounds(0, 0, 300, 300);
          	output.setEditable(false);
          	
          	input = new JTextField();
          	input.setBounds(0, 300, 260, 20);
          	
          	button = new Button("SEND");
          	button.setBounds(250, 300, 50, 20);
          	button.addActionListener(this);
          	
          	getContentPane().add(panel);
          	panel.add(button);
          	panel.add(input);
          	panel.add(output);
          }
          public void actionPerformed(ActionEvent e) { 
          	try{
          		String out = input.getText();
          		output.append(input.getText());
          		input.selectAll();
          		input.requestFocusInWindow();
          	}catch(NullPointerException e3){}
          }
          
          public static void main(String\[\] args){
          	 SwingUtilities.invokeLater(new Runnable() {
          		 public void run(){
          			 GUI cGUI = new GUI();
          			 cGUI.setVisible(true);
          		 }
          	 });
          }
          

          }

          J 1 Reply Last reply
          0
          • J Joshua Waring

            First lets give some context, I've just created a client and server but they don't work without using console So I've decided to create a GUI on top they'll communicate though the Stack, but that's all for a later date, right now I get a error on the Even listener which says that the JTextField is empty upon .getText() even though it's not. I've looked though it, but since I'm quite new to the whole Swing field, it's a challenge for me to fix the problem. It really is a simple GUI.

            import java.awt.Button;
            import java.awt.event.ActionEvent;
            import java.awt.event.ActionListener;
            import java.util.Stack;

            import javax.swing.*;
            public class GUI extends JFrame implements ActionListener{
            Stack incoming = null;
            Stack outgoing = null;
            JTextField input;
            JTextArea output;
            Button button;
            public GUI(){

            	setTitle("Satire v1.0.1");
            	setSize(320,360);
            	setLocationRelativeTo(null);
            	setDefaultCloseOperation(EXIT\_ON\_CLOSE);
            	
            	JPanel panel = new JPanel();
            	panel.setLayout(null);
            	
            	output = new JTextArea();
            	output.setBounds(0, 0, 300, 300);
            	output.setEditable(false);
            	
            	input = new JTextField();
            	input.setBounds(0, 300, 260, 20);
            	
            	button = new Button("SEND");
            	button.setBounds(250, 300, 50, 20);
            	button.addActionListener(this);
            	
            	getContentPane().add(panel);
            	panel.add(button);
            	panel.add(input);
            	panel.add(output);
            }
            public void actionPerformed(ActionEvent e) { 
            	GUI gui = new GUI();
            	try{
            		String out = gui.input.getText();
            		gui.input.selectAll();
            		outgoing.add(out);
            	}catch(NullPointerException e3){}
            	gui.output.append(outgoing.pop());
            }
            
            public static void main(String\[\] args){
            	 SwingUtilities.invokeLater(new Runnable() {
            		 public void run(){
            			 GUI cGUI = new GUI();
            			 cGUI.setVisible(true);
            		 }
            	 });
            }
            

            }

            P Offline
            P Offline
            pasztorpisti
            wrote on last edited by
            #5

            Start using a good java IDE. Eclipse is one of the best free IDEs out there, choosing that you can only win. Download it and learn its basic features (how to set up a project and how to debug in it). Advice regarding your program: Hook the input of your textbox and perform the "send text" action when the user presses enter in the textfield. When sending the text it might be a better idea to clear the textfield instead of selecting its contents.

            1 Reply Last reply
            0
            • P pasztorpisti

              There you go. This time I compiled/tested it, contained another big mistake. Why have you used those stacks? You should check the error messages as well in the console output!!! You program threw exceptions!

              import java.awt.Button;
              import java.awt.event.ActionEvent;
              import java.awt.event.ActionListener;
              import java.util.Stack;

              import javax.swing.*;
              public class GUI extends JFrame implements ActionListener{
              JTextField input;
              JTextArea output;
              Button button;
              public GUI(){

              	setTitle("Satire v1.0.1");
              	setSize(320,360);
              	setLocationRelativeTo(null);
              	setDefaultCloseOperation(EXIT\_ON\_CLOSE);
              	
              	JPanel panel = new JPanel();
              	panel.setLayout(null);
              	
              	output = new JTextArea();
              	output.setBounds(0, 0, 300, 300);
              	output.setEditable(false);
              	
              	input = new JTextField();
              	input.setBounds(0, 300, 260, 20);
              	
              	button = new Button("SEND");
              	button.setBounds(250, 300, 50, 20);
              	button.addActionListener(this);
              	
              	getContentPane().add(panel);
              	panel.add(button);
              	panel.add(input);
              	panel.add(output);
              }
              public void actionPerformed(ActionEvent e) { 
              	try{
              		String out = input.getText();
              		output.append(input.getText());
              		input.selectAll();
              		input.requestFocusInWindow();
              	}catch(NullPointerException e3){}
              }
              
              public static void main(String\[\] args){
              	 SwingUtilities.invokeLater(new Runnable() {
              		 public void run(){
              			 GUI cGUI = new GUI();
              			 cGUI.setVisible(true);
              		 }
              	 });
              }
              

              }

              J Offline
              J Offline
              Joshua Waring
              wrote on last edited by
              #6

              the purpose of the stack was for when I implements it into my chat client I'll have something to send it by, without changing the code around too much. I personally don't see how it caused an error.

              P 1 Reply Last reply
              0
              • J Joshua Waring

                the purpose of the stack was for when I implements it into my chat client I'll have something to send it by, without changing the code around too much. I personally don't see how it caused an error.

                P Offline
                P Offline
                pasztorpisti
                wrote on last edited by
                #7

                It caused an error because in java every object is used by reference. You had there a null reference to a stack object, actually you haven't created your stack object and when you tried to use it that was a NullPointerException! :-)

                J 1 Reply Last reply
                0
                • P pasztorpisti

                  It caused an error because in java every object is used by reference. You had there a null reference to a stack object, actually you haven't created your stack object and when you tried to use it that was a NullPointerException! :-)

                  J Offline
                  J Offline
                  Joshua Waring
                  wrote on last edited by
                  #8

                  Meaning i should of put Stack incoming = new Stack();

                  P J 2 Replies Last reply
                  0
                  • J Joshua Waring

                    Meaning i should of put Stack incoming = new Stack();

                    P Offline
                    P Offline
                    pasztorpisti
                    wrote on last edited by
                    #9

                    Yep. Common mistake when starting java. In java only the primitive types are self contained, everything else is just a null or uninitialized reference without initialization.

                    J 1 Reply Last reply
                    0
                    • J Joshua Waring

                      Meaning i should of put Stack incoming = new Stack();

                      J Offline
                      J Offline
                      Joshua Waring
                      wrote on last edited by
                      #10

                      Any special features and tricks to Eclipse Debugger, I've hit an error while fusing it to the client. the server gets the connection but all input is just added to the JTextArea without being sent to the server even though it's told to send. I'd prefer to fix this one so I don't have to spam these forums.

                      1 Reply Last reply
                      0
                      • P pasztorpisti

                        Yep. Common mistake when starting java. In java only the primitive types are self contained, everything else is just a null or uninitialized reference without initialization.

                        J Offline
                        J Offline
                        Joshua Waring
                        wrote on last edited by
                        #11

                        Hopefully you could help me with this one, I've found the problem (Thanks to the breakpoints) My main created a thread1 and then thread1 created thread 2, both of which contain a while(true) (Then just sit and wait for input and output) but when thread1 creates thread2 it doesn't go past the line which creates thread2 which Isn't what I thought would happen... I thought Thread1 would create thread2 then proceeds to the loop for user input.

                        P 1 Reply Last reply
                        0
                        • J Joshua Waring

                          Hopefully you could help me with this one, I've found the problem (Thanks to the breakpoints) My main created a thread1 and then thread1 created thread 2, both of which contain a while(true) (Then just sit and wait for input and output) but when thread1 creates thread2 it doesn't go past the line which creates thread2 which Isn't what I thought would happen... I thought Thread1 would create thread2 then proceeds to the loop for user input.

                          P Offline
                          P Offline
                          pasztorpisti
                          wrote on last edited by
                          #12

                          You are right that should happen.

                          J 1 Reply Last reply
                          0
                          • P pasztorpisti

                            You are right that should happen.

                            J Offline
                            J Offline
                            Joshua Waring
                            wrote on last edited by
                            #13

                            I see no error but to call a implements Runnable I use the line,

                            (new Thread(new accept(br))).start();

                            if that helps

                            P 1 Reply Last reply
                            0
                            • J Joshua Waring

                              I see no error but to call a implements Runnable I use the line,

                              (new Thread(new accept(br))).start();

                              if that helps

                              P Offline
                              P Offline
                              pasztorpisti
                              wrote on last edited by
                              #14

                              Not to be rude but maybe you should learn the basics first, correct threading is more or less an advanced topic. Actually the java tutorials put together by sun is exceptionally good: http://docs.oracle.com/javase/tutorial/[^] There is a paragraph on that page with "Trails Covering the Basics" header, you should read through that carefully, you will find topics about threading in the "Essential Java Classes" part, but read it from the beginning to the end without skipping. After this basic tutorial you won't ask questions like this.

                              1 Reply Last reply
                              0
                              • J Joshua Waring

                                First lets give some context, I've just created a client and server but they don't work without using console So I've decided to create a GUI on top they'll communicate though the Stack, but that's all for a later date, right now I get a error on the Even listener which says that the JTextField is empty upon .getText() even though it's not. I've looked though it, but since I'm quite new to the whole Swing field, it's a challenge for me to fix the problem. It really is a simple GUI.

                                import java.awt.Button;
                                import java.awt.event.ActionEvent;
                                import java.awt.event.ActionListener;
                                import java.util.Stack;

                                import javax.swing.*;
                                public class GUI extends JFrame implements ActionListener{
                                Stack incoming = null;
                                Stack outgoing = null;
                                JTextField input;
                                JTextArea output;
                                Button button;
                                public GUI(){

                                	setTitle("Satire v1.0.1");
                                	setSize(320,360);
                                	setLocationRelativeTo(null);
                                	setDefaultCloseOperation(EXIT\_ON\_CLOSE);
                                	
                                	JPanel panel = new JPanel();
                                	panel.setLayout(null);
                                	
                                	output = new JTextArea();
                                	output.setBounds(0, 0, 300, 300);
                                	output.setEditable(false);
                                	
                                	input = new JTextField();
                                	input.setBounds(0, 300, 260, 20);
                                	
                                	button = new Button("SEND");
                                	button.setBounds(250, 300, 50, 20);
                                	button.addActionListener(this);
                                	
                                	getContentPane().add(panel);
                                	panel.add(button);
                                	panel.add(input);
                                	panel.add(output);
                                }
                                public void actionPerformed(ActionEvent e) { 
                                	GUI gui = new GUI();
                                	try{
                                		String out = gui.input.getText();
                                		gui.input.selectAll();
                                		outgoing.add(out);
                                	}catch(NullPointerException e3){}
                                	gui.output.append(outgoing.pop());
                                }
                                
                                public static void main(String\[\] args){
                                	 SwingUtilities.invokeLater(new Runnable() {
                                		 public void run(){
                                			 GUI cGUI = new GUI();
                                			 cGUI.setVisible(true);
                                		 }
                                	 });
                                }
                                

                                }

                                G Offline
                                G Offline
                                Gowtham Gutha
                                wrote on last edited by
                                #15

                                First of all you didn't set text in the JTextField and also you've not even enabled the user to write into the JTextField. Because, setEditable(false); means the user cannot edit the text in the text field and also there was no text in the Textfield. And also you do not need to create object for the class GUI in actionPerformed() because this method was written in the same class only. This means that you are performing action on the object 'g' which you've created. You can directly access them, there is no need of creating the object.

                                Gowtham Gutha (Java-Demos.Blogspot.Com)

                                1 Reply Last reply
                                0
                                Reply
                                • Reply as topic
                                Log in to reply
                                • Oldest to Newest
                                • Newest to Oldest
                                • Most Votes


                                • Login

                                • Don't have an account? Register

                                • Login or register to search.
                                • First post
                                  Last post
                                0
                                • Categories
                                • Recent
                                • Tags
                                • Popular
                                • World
                                • Users
                                • Groups