开发者

Get input text without scanner

开发者 https://www.devze.com 2022-12-21 07:34 出处:网络
I work on a school project and I am now wondering if it is possible to get text from JTextField with get?

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 :)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号