Reputation: 139
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 .
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;
}
}
Upvotes: 1
Views: 20031
Reputation: 9
combobox.getEditor().getItem().toString
worked for me.
You can add documentListener
to listen to the changes of input.
Upvotes: 0
Reputation: 45
You can use KeyListener but instead of adding it directly, insert it using
comboBox.getEditor().getEditorComponent().addKeyListener();
This will make sure that listener is being added to Editor of JComboBox
To get text from it use:
comboBox.getEditor().getItem();
this will get the text directly from the editor, you can use it below keyReleased to get text from editor as you type
Upvotes: 0
Reputation: 11
The following method can be used to get the text from a ComboBox
.
String s = (String)comboOne.getSelectedItem();
where comboOne
is the variable name of ComboBox
.
Upvotes: 1
Reputation: 11
For reference, to answer the tittle of the page : How to get text from JComboBox
?
((JTextComponent)combo.getEditor().getEditorComponent()).getText()
It "works" but does not return any text... crap.
Upvotes: 0
Reputation: 347184
KeyListener
is not the appropriate event listener to use, it is triggered BEFORE the combobox has updated its state, better to use an 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.
Upvotes: 4