Results 1 to 10 of 10
  1. #1
    mmanu87 is offline Member
    Join Date
    Jul 2011
    Posts
    9
    Rep Power
    0

    Default Help!! getText() in action listener doesn't work!!!

    Hi!!!! The program I've created will generate a graph with a number of vertex choosed at the beginning. I've got this problem
    in my main function. All the other function works perfectly!!!
    Java Code:
    JFrame start = new JFrame("Jung2 - Example of Basical Function");
        		JDialog ask = new JDialog(start, "QUANTI VERTICI VUOI?");
        		JTextField answer = new JTextField("min 1 max 9");
        		JButton insert = new JButton("CREA GRAFICO");
        	    start.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        		start.setSize(Window.WIDTH,Window.HEIGHT);
       		    start.add(insert);
        		start.add(ask);
        		start.add(answer);
        		final String str ;
        		final int numV;
        		answer.addActionListener(new ActionListener()
        			{ public void actionPerformed(ActionEvent e)
        				{	}
        			});
        		insert.addActionListener(new ActionListener()
    		    	{ public void actionPerformed(ActionEvent e)
    		    		{	numV = Integer.parseInt(str = new String (answer.getText()));  }
    		    	                                                   // this answer.getText() doesn't work!!! 
    
    
                                                                     //if I set answer as final I have an error on numV below..
                            JPanel GuiPanel = makeGuiPanel( numV , graphMouse, ShearPlugin, PickPlugin, RotPlugin );
    			initFrame( GuiPanel, graphMouse, ShearPlugin, PickPlugin, RotPlugin);
    });
    I want to create a frame where is possible choose the number of vertex directly when you'll run the "program.jar" instead using console. What's the problem??Can you tell my the right code to do that??
    THANKS TO ALL!!!!

  2. #2
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    6

    Default Re: Help!! getText() in action listener doesn't work!!!

    you try to access a class variable inside an inner class defined in a different method. define the actionlistener in a different class. other point: you can't add a JDialog to a JFrame because a JDialog is a Window!
    Last edited by j2me64; 09-04-2011 at 12:51 PM.

  3. #3
    mmanu87 is offline Member
    Join Date
    Jul 2011
    Posts
    9
    Rep Power
    0

    Default Re: Help!! getText() in action listener doesn't work!!!

    Please, Can you write the right code for the class of the action listener?? thanks!!

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,452
    Rep Power
    20

    Default Re: Help!! getText() in action listener doesn't work!!!

    This is a forum, not a code factory. You've been given some good advice, now apply that to your code (it is your code, or is it someone else's?) and if you still need more help then ask a specific question.

    db

  5. #5
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    6

    Default Re: Help!! getText() in action listener doesn't work!!!

    Quote Originally Posted by mmanu87 View Post
    Please, Can you write the right code for the class of the action listener?? thanks!!

    No, this forum is for answering java questions and not a coding forum. but you could put your code in a class that implements an ActionListener and then you can put the listener with insert.addActionListener(this). Try and post your code!

  6. #6
    mmanu87 is offline Member
    Join Date
    Jul 2011
    Posts
    9
    Rep Power
    0

    Default Re: Help!! getText() in action listener doesn't work!!!

    ok thanks.. I've tried to do this. Now I don't have syntax error, but when I run the main I have this message:

    "Exception in thread "main" java.lang.IllegalArgumentException: adding a window to a container"

    The code I wrote is this:
    Java Code:
        		JFrame start = new JFrame("Jung2 - Example of Basical Function");
        		JDialog ask = new JDialog(start, "QUANTI VERTICI VUOI?");
        		JTextField answer = new JTextField("min 1 max 9");
        		JButton insert = new JButton("CREA GRAFICO");
        	    start.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        		start.setSize(Window.WIDTH,Window.HEIGHT);
       		    start.add(insert);
        		start.add(ask);
        		start.add(answer);
        		
        		
        		//NEW CODE ----------------------
        		int numero;
        	    answer.addActionListener(new ActionListener()
        			{ public void actionPerformed(ActionEvent e)
        				{	}
        			});
        	    Start prova = new Start(answer);
        	    numero= prova.getNumV();
        	    
                     //-----------------------------
        	    
        	    JPanel GuiPanel = makeGuiPanel( numero , graphMouse, ShearPlugin, PickPlugin, RotPlugin );
    			initFrame( GuiPanel, graphMouse, ShearPlugin, PickPlugin, RotPlugin);
    the class Start is in another "class file" of my package and its code is:
    Java Code:
    import javax.swing.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    public class Start implements ActionListener
    {
    	private JTextField textfield;
    	private String str;
    	private int numV;
    	public Start(JTextField b)
    		{
    			textfield = b;
    		}
    	public void actionPerformed(ActionEvent e)
    		{
    			str=textfield.getText();
    			numV = Integer.parseInt(str);
    		}
    	public int getNumV ()
    		{ return numV;}
    	
    };
    is it right??

  7. #7
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    6

    Default Re: Help!! getText() in action listener doesn't work!!!

    Quote Originally Posted by mmanu87 View Post
    is it right??
    great! an improvement could be to implement also the JFrame with

    public class Start extends JFrame implements ActionListener {

    }

    inside the same class.

  8. #8
    mmanu87 is offline Member
    Join Date
    Jul 2011
    Posts
    9
    Rep Power
    0

    Default Re: Help!! getText() in action listener doesn't work!!!

    thanks! Now I try to do that... But for the error message??

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

    Default Re: Help!! getText() in action listener doesn't work!!!

    thanks! Now I try to do that... But for the error message??
    If you need our help, please translate this into understandable English. What is your current problem? What is your current error message (or are we supposed to guess)?

  10. #10
    mmanu87 is offline Member
    Join Date
    Jul 2011
    Posts
    9
    Rep Power
    0

    Default Re: Help!! getText() in action listener doesn't work!!!

    Excuse me for my bad English!! I'll try to explain my problem. Now my program doesn't have syntax error but it can't read the text in my JTextflied.
    I just want to insert the number of vertex in my JTextfield and when I do that, when I'll click on my button I want to give at my "makeGuiPanel function" that number! I hope you'll understand everything!! I'll post my "main function" and my "Start class".

    MAIN:
    Java Code:
    int numero = 0;
        		insert = new JButton("CREATE GRAPH");
        		ask = new JLabel(" HOW MANY VERTEX DO YOU WANT FROM 1 TO 10?? ");
        		answer = new JTextField(5);
        		pan = new JPanel();
        		pan.setBackground(Color.ORANGE);	
        		start = new JFrame("JUNG2 - PROJECT");
        		start.getContentPane().add(pan);
        		start.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);	
        		pan.add(ask);
        		pan.add(answer);
        		pan.add(insert);
        		start.pack();
        	    start.setVisible(true); 
        	    
        	    answer.addActionListener(new ActionListener()
        			{ public void actionPerformed(ActionEvent e)
        				{ }
        			});
        	       Start prova = new Start(answer);
        		   numero = prova.getNumV();
    
        		   JPanel GuiPanel = makeGuiPanel( numero , graphMouse, ShearPlugin, PickPlugin, RotPlugin );
    ----------------------------------------
    Start class:
    Java Code:
    import javax.swing.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    public class Start extends JFrame implements ActionListener
    {
    	private JTextField textfield;
    	private String str;
    	private int numV;
    	public Start(JTextField b)
    		{
    			textfield = b;
    		}
    	public void actionPerformed(ActionEvent e)
    		{
    			do
    				{
    	   			System.out.println("ACTIONPERFORMED FUNZIONA!");
    
    				str=textfield.getText();
    			numV = Integer.parseInt(str);
    				}
    			while (numV == 0);
    		}
    	public int getNumV ()
    		{ return numV;}
    	
    };
    
    THANKS TO ALL!!

Similar Threads

  1. JPanel Listener doesn't work in a JFrame...
    By jiapei100 in forum AWT / Swing
    Replies: 2
    Last Post: 11-18-2010, 03:03 PM
  2. Problem in action listener
    By cool in forum AWT / Swing
    Replies: 4
    Last Post: 11-16-2010, 08:44 AM
  3. Simple Action/Listener Help GUI
    By aanders5 in forum New To Java
    Replies: 24
    Last Post: 10-18-2010, 07:43 PM
  4. Action Listener
    By greatmajestics in forum AWT / Swing
    Replies: 8
    Last Post: 03-25-2010, 06:39 PM
  5. Action Listener? how to use this?
    By jeffrey in forum New To Java
    Replies: 2
    Last Post: 10-12-2009, 09:51 AM

Posting Permissions

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