![](/img/trans.png)
[英]How to change a value in the Jtextfield or JComboBox using the data structure linkedlist?
[英]Return value of filled JComboBox and using the values to fill JTextField
我正在编写一个JFrame,它通过JMenubar打开JOptionPane。 JOptionPane确实有一个带有许多不同字符串值的组合框。 在JComboBox之后,有一个JTextfield。
我希望每次在JComboBox中选择新的字符串值时,将在JComboBox中选择的文本插入下一个JTextField中并进行更新。
请帮忙!
class DateiAdapter implements ActionListener {
public void actionPerformed(ActionEvent event) {
JMenuItem change = (JMenuItem) event.getSource();
JComboBox alleQuest = new JComboBox(ah.getLine(1)); //this ComboBox gets a lot of different values from a different class
// Actionlistener
ActionListener cbActionListener = new ActionListener() {
public void actionPerformed(ActionEvent e) {
String s = (String) allQuest.getSelectedItem(); //gets the selected string of the JComboBox
System.out.println("\n" + s); // I want to use String s outside of here, too
}
};
allQuest.addActionListener(cbActionListener);
JTextField questions = new JTextField(); //THIS is the TextField I want to insert the different values of the ComboBox above
JTextField a2 = new JTextField();
JTextField b2 = new JTextField();
JTextField c2 = new JTextField();
JTextField answ2 = new JTextField();
if (change == itemChange) {
final JComponent[] input = new JComponent[] {
new JLabel("You change your question here:"),
allQuest,
quest2,
a2,
b2,
c2,
answ2, };
JOptionPane.showMessageDialog(null, input, "Change Question", JOptionPane.PLAIN_MESSAGE);
您可以使用
questions.setText(alleQuest.getSelectedItem().toString());
在ActionListener中。 但是问题textfileld应该在actionPerformed方法上方定义
您有2个关于
public void actionPerformed(ActionEvent e) {
String s = (String) allQuest.getSelectedItem(); //gets the selected string of the JComboBox
System.out.println("\n" + s); // I want to use String s outside of here, too
}
};
s
作为封闭类的字段(在您的情况下为DateiAdapter类) actionPerformed
方法中的给定s
值更新所需的每个GUI组件。 老实说,这是执行此操作的确切位置,因为EDT调用actionPerformed
,所有GUI更改都应在该处进行。 DateiAdapter
类之外使用s
,请遵循第1点,然后选择2,并为该字段添加public getter。 在你的情况下把questions.setText(alleQuest.getSelectedItem().toString());
在actionPerformed
中将是最直接的选择。
因此,这似乎基本上是一个上下文问题, questions
字段需要在cbActionListener
可以引用它的上下文中定义。
最简单的方法之一就是将questions
设为类的实例字段,例如...
class DateiAdapter implements ActionListener {
private JTextField questions = new JTextField(); //THIS is the TextField I want to insert the different values of the ComboBox above
public void actionPerformed(ActionEvent event) {
JMenuItem change = (JMenuItem) event.getSource();
JComboBox alleQuest = new JComboBox(ah.getLine(1)); //this ComboBox gets a lot of different values from a different class
// Actionlistener
ActionListener cbActionListener = new ActionListener() {
public void actionPerformed(ActionEvent e) {
String s = (String) allQuest.getSelectedItem(); //gets the selected string of the JComboBox
questions.setText(s);
}
};
allQuest.addActionListener(cbActionListener);
JTextField a2 = new JTextField();
JTextField b2 = new JTextField();
JTextField c2 = new JTextField();
JTextField answ2 = new JTextField();
if (change == itemChange) {
final JComponent[] input = new JComponent[]{
new JLabel("You change your question here:"),
allQuest,
quest2,
a2,
b2,
c2,
answ2,};
JOptionPane.showMessageDialog(null, input, "Change Question", JOptionPane.PLAIN_MESSAGE);
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.