Results 1 to 10 of 10
- 09-04-2011, 11:03 AM #1
Member
- Join Date
- Jul 2011
- Posts
- 9
- Rep Power
- 0
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!!!
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??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); });
THANKS TO ALL!!!!
- 09-04-2011, 11:31 AM #2
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 11:51 AM.
- 09-04-2011, 11:48 AM #3
Member
- Join Date
- Jul 2011
- Posts
- 9
- Rep Power
- 0
Re: Help!! getText() in action listener doesn't work!!!
Please, Can you write the right code for the class of the action listener??
thanks!!
- 09-04-2011, 11:55 AM #4
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
- 09-04-2011, 11:56 AM #5
Re: Help!! getText() in action listener doesn't work!!!
- 09-04-2011, 12:24 PM #6
Member
- Join Date
- Jul 2011
- Posts
- 9
- Rep Power
- 0
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:
the class Start is in another "class file" of my package and its code is: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);
is it right??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;} };
- 09-04-2011, 12:32 PM #7
Re: Help!! getText() in action listener doesn't work!!!
- 09-04-2011, 12:45 PM #8
Member
- Join Date
- Jul 2011
- Posts
- 9
- Rep Power
- 0
Re: Help!! getText() in action listener doesn't work!!!
thanks! Now I try to do that... But for the error message??
-
Re: Help!! getText() in action listener doesn't work!!!
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)?thanks! Now I try to do that... But for the error message??
- 09-04-2011, 03:19 PM #10
Member
- Join Date
- Jul 2011
- Posts
- 9
- Rep Power
- 0
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
-
JPanel Listener doesn't work in a JFrame...
By jiapei100 in forum AWT / SwingReplies: 2Last Post: 11-18-2010, 02:03 PM -
Problem in action listener
By cool in forum AWT / SwingReplies: 4Last Post: 11-16-2010, 07:44 AM -
Simple Action/Listener Help GUI
By aanders5 in forum New To JavaReplies: 24Last Post: 10-18-2010, 06:43 PM -
Action Listener
By greatmajestics in forum AWT / SwingReplies: 8Last Post: 03-25-2010, 05:39 PM -
Action Listener? how to use this?
By jeffrey in forum New To JavaReplies: 2Last Post: 10-12-2009, 08:51 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks