java 如何从 JComboBox 获取文本?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12985861/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get text from JComboBox?
提问by mathewM
I have a problem with getting text from selected position of JComboBox. I tried to use getSelectedItem method in class which extends Kodowanie.java, but i get null value and i can't find out why. Method (getSelectedItem) works in Kodowanie class I can easily get text form JComboBox .
我在从 JComboBox 的选定位置获取文本时遇到问题。我试图在扩展 Kodowanie.java 的类中使用 getSelectedItem 方法,但我得到空值,我找不到原因。方法 (getSelectedItem) 在 Kodowanie 类中有效,我可以轻松地从 JComboBox 获取文本形式。
Kodowanie.java
Kodowanie.java
public class Kodowanie {
//Skladowe:
ArrayList <String> qweqwe;
JComboBox inputCode = new JComboBox(); //HERE IS MY INPUT COMBOBOX
JComboBox outputCode = new JComboBox();
JTextArea input;
JTextArea output;
public void createGUI(){
JFrame frame = new JFrame("Code");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
input = new JTextArea();
output = new JTextArea();
qweqwe = new ArrayList<>();
napelnijTalbiceCharsetami(qweqwe); //METHOD WHICH ADD ALL CHARSETS TO LIST
inputCode = new JComboBox(qweqwe.toArray());
outputCode = new JComboBox(qweqwe.toArray());
JScrollPane scrollPaneInput = new JScrollPane(input, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
JScrollPane scrollPaneOutput = new JScrollPane(output, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
scrollPaneInput.setBorder(BorderFactory.createTitledBorder("Input Path"));
scrollPaneOutput.setBorder(BorderFactory.createTitledBorder("Output Path"));
inputCode.setPreferredSize(new Dimension(400,50));
outputCode.setPreferredSize(new Dimension(400,50));
scrollPaneInput.setPreferredSize(new Dimension(400, 100));
scrollPaneOutput.setPreferredSize(new Dimension(400, 100));
input.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent evt){
Strumieniowanie tmp;
if(evt.getKeyCode() == KeyEvent.VK_ENTER)
{
try {
tmp = new Strumieniowanie(input.getText(), output.getText());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
});
frame.getContentPane().add(scrollPaneInput);
frame.getContentPane().add(scrollPaneOutput);
frame.getContentPane().add(inputCode);
frame.getContentPane().add(outputCode);
frame.setLayout(new FlowLayout());
frame.setPreferredSize(new Dimension(850, 220));
frame.setVisible(true);
frame.pack();
}
private ArrayList napelnijTalbiceCharsetami(ArrayList tmp){
Map charSets = Charset.availableCharsets();
Iterator iterator = charSets.keySet().iterator();
while (iterator.hasNext()) {
System.out.println(iterator.next().toString());
tmp.add(iterator.next().toString());
}
return tmp;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Kodowanie l = new Kodowanie();
l.createGUI();
}
}
Strumieniowanie.java
Strumieniowanie.java
public class Strumieniowanie extends Kodowanie {
protected boolean pathInputOk = false;
protected boolean pathOutputOk = false;
public Strumieniowanie(String tmpInpute, String tmpOutput) throws IOException {
File plikInput = new File(tmpInpute);
File plikOutput = new File(tmpOutput);
String inputText;
inputText = (String) inputCode.getSelectedItem(); //HERE I TRY TO GET STRING FROM JCOMBOBOX BUT IT IS ALWAYS NULL !
System.out.println(inputText);
pathInputOk = plikInput.isFile();
pathOutputOk = plikOutput.isFile();
System.out.println(pathInputOk);
System.out.println(pathOutputOk);
if (pathInputOk && pathOutputOk) {
File nowyPlik = new File(tmpInpute);
FileInputStream fis = new FileInputStream(nowyPlik);
fis.close();
}
}
protected boolean isItaPath(File plik) {
boolean tmp =
false;
tmp = plik.isFile();
return tmp;
}
}
回答by MadProgrammer
KeyListener
is not the appropriate event listener to use, it is triggered BEFORE the combobox has updated its state, better to use an ActionListener
.
KeyListener
不是要使用的适当事件侦听器,它在组合框更新其状态之前触发,最好使用ActionListener
.
The actionPerformed
event will be fired AFTER the combo box has updated, which will ensure that the getSelectedItem
method will actually return the currently selected value.
该actionPerformed
事件将在组合框更新后触发,这将确保该getSelectedItem
方法实际返回当前选定的值。
回答by Kapil Garg
The following method can be used to get the text from a ComboBox
.
以下方法可用于从ComboBox
.
String s = (String)comboOne.getSelectedItem();
where comboOne
is the variable name of ComboBox
.
comboOne
的变量名在哪里ComboBox
。
回答by Alexandre Fradkin
For reference, to answer the tittle of the page : How to get text from JComboBox
?
作为参考,回答页面标题:How to get text from JComboBox
?
((JTextComponent)combo.getEditor().getEditorComponent()).getText()
It "works" but does not return any text... crap.
它“有效”但不返回任何文本......废话。