我从JComboBox的选定位置获取文本时遇到问题。我试图在类中使用getSelectedItem方法扩展Kodowanie.java,但我得到null值,我找不到原因。方法(getSelectedItem)在Kodowanie类中工作我可以轻松地从JComboBox获取文本。
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
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;
}
}
答案 0 :(得分:4)
KeyListener
不是合适的事件监听器,它在组合框更新状态之前触发,最好使用ActionListener
。
组合框更新后将触发actionPerformed
事件,这将确保getSelectedItem
方法实际返回当前选定的值。
答案 1 :(得分:1)
以下方法可用于从ComboBox
获取文本。
String s = (String)comboOne.getSelectedItem();
其中comboOne
是ComboBox
的变量名。
答案 2 :(得分:0)
供参考,要回答页面标题:如何从JComboBox
获取文字?
((JTextComponent)combo.getEditor().getEditorComponent()).getText()
它“有效”,但不会返回任何文字......废话。