Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

JTextField and Choice Problem  RSS feed

 
Peter Phung
Ranch Hand
Posts: 138
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i have a choice button and a JTextField in a GUI. When an item in the choice is selected, it is meant to appear in the textfield but doesn't.
Here is my code :

As far as i can tell my code is correct. This is very confusing
 
Rajendar Goud
Ranch Hand
Posts: 220
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi pete,
yes ur code seems to be correct as far as event hndling is concerned.well i too wrote a simple example and it worked fine for me.
please compare ur code and check ,if theres some
thing went wrong some where in other parts of code related to setting the layout and the contenPane.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class choicetext extends JFrame
{
JTextField tf;
Choice ch;
Container cont;
choicetext()
{
tf = new JTextField(10);
ch = new Choice();
ch.add("One");
ch.add("Two");
cont = getContentPane();
cont.setLayout(new FlowLayout());
cont.add(ch);
cont.add(tf);
ch.addItemListener(new itemclass());
setSize(300,250);
setVisible(true);
}
class itemclass implements ItemListener
{
public void itemStateChanged(ItemEvent ie)
{
tf.setText(ch.getSelectedItem());
}
}
public static void main(String a[])
{
new choicetext();
}
}
cheers,
Raj
 
Ali Ashraf
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Peter Phung:
i have a choice button and a JTextField in a GUI. When an item in the choice is selected, it is meant to appear in the textfield but doesn't.
Here is my code :

As far as i can tell my code is correct. This is very confusing
 
Ali Ashraf
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear peter
u can access only final methods and variables in an anonymous class.
change yur code and try again!!!
 
Peter Phung
Ranch Hand
Posts: 138
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Muhammad,
Explain what u mean
 
Tris Rabar
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Peter, what he means is that you can't access components just like that from an anonymous inner class when they are defined out of the class scope.
You need to declare your textfield as 'final' like
[CODE]
final JTextField textfield = new JTextField();
[CODE]
Or, you can use a named inner class like Rajendar did.
Good luck!
 
Peter Phung
Ranch Hand
Posts: 138
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually i can because the textfield is defined globally. The code that i've posted is part of a larger application which uses 9 custom classes and has about 3-4 thousand lines of code
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!