Get input text without scanner
I work on a school project and I am now wondering if it is possible to get text from JTextField with get?
// Textrutor
JTextField textTitel = new JTextField(null, 20);
textTitel.setToolTipT开发者_StackOverflowext("ex. Flickan som lekte med elden");
JTextField textSort = new JTextField(null, 10);
textSort.setToolTipText("ex. Skräck, Action");
JTextField textDesc = new JTextField(null, 15);
textDesc.setToolTipText("ex. Stieg Larsson");
// Knappar
JButton addButton = new JButton("Lägg till");
// Combobox
JComboBox comboBox = new JComboBox();
comboBox.addItem("Film");
comboBox.addItem("CD");
comboBox.addItem("Bok");
comboBox.addItem("Annat");
I am trying to get the text and adding it to my array like this:
public String getTitelText() {
return titelText;
}
public String getDescText() {
return descText;
}
public String getSortText() {
return sortText;
}
public void actionPerformed(ActionEvent e) {
DatabaseTable dt = new DatabaseTable();
dt.add(titelText, sortText, descText, descText);
But I think that this way is wrong, but dont know how to solve it. Another question is there any easy way to know what is selected on JComboBox?
For JTextField use myTextField.getText()
For tooltip in JTextField use myTextField.getToolTipText()
For JComboBox use myComboBox.getSelectedIndex()
or myComboBox.getSelectedItem()
First gives you the index of the selected item and he second gives you the actual item.
comboBox.getSelectedItem();
public String getSortText() {
return sortText.getText();
}
All this can easily be found in the Java DOCs from SUN.
--edit-- updated my answer to really make sure you understand :)
精彩评论