gpt4 book ai didi

java - JComboBox 从所选项目中的多个变量中获取变量

转载 作者:行者123 更新时间:2023-12-01 12:43:59 28 4
gpt4 key购买 nike

在我的代码中,我有一个 JComboBox,在其中放置数据库中的多个项目,所选项目使用操作监听器打印出来,但我想要获取的是从中选择的变量“code”的值comboBox.addItem("代码:"+code+"标题:"+name);

final JComboBox comboBox = new JComboBox();
comboBox.setBounds(139, 40, 337, 20);
contentPanel.add(comboBox);
Connection con;
try {
con = DriverManager.getConnection(Main.URL);
Statement statement = con.createStatement();
ResultSet resultSet = statement.executeQuery("select * from APPDATABASE.SUBJECT");
while (resultSet.next()){
String code = resultSet.getString(1);
String name = resultSet.getString(2);
comboBox.addItem("Κωδ: "+code+" Τίτλος: "+name);
}
ActionListener actionListener = new ActionListener() {
public void actionPerformed(ActionEvent actionEvent) {
System.out.println("Selected: " + comboBox.getSelectedItem());
}
};
comboBox.addActionListener(actionListener);

resultSet.close();
statement.close();
con.close();
} catch (SQLException e1) {
e1.printStackTrace();
}

最佳答案

这是一个简单的示例代码。

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import swingdemoapp.table.model.ComboBoxItem;

public class GetItemsFromJComboBoxPanel extends JPanel implements ActionListener {

private final JComboBox<ComboBoxItem> items;
private final DefaultComboBoxModel model;
private final ComboBoxItem[] itemList = new ComboBoxItem[100];

private final JLabel codeLabel;

public GetItemsFromJComboBoxPanel() {

JPanel jPanel = new JPanel(new BorderLayout());
initList();
model = new DefaultComboBoxModel(itemList);
items = new JComboBox<>(model);
items.addActionListener(this);
jPanel.add(items, BorderLayout.NORTH);
codeLabel = new JLabel();
jPanel.add(codeLabel, BorderLayout.CENTER);
this.add(jPanel);
}

private ComboBoxItem[] initList() {
for(int i = 0; i < 100; i++) {
itemList[i] = new ComboBoxItem("Code" + i, "My Item " + i);
}

return itemList;
}

@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource().equals(items)) {
ComboBoxItem selectedItem = (ComboBoxItem) items.getModel().getSelectedItem();
codeLabel.setText(selectedItem.getCode());
}
}


}

这是域类ComboBoxItem:

public class ComboBoxItem {

private String code;
private String title;

public ComboBoxItem() {
}

public ComboBoxItem(String code, String title) {
this.code = code;
this.title = title;
}

/**
* @return the code
*/
public String getCode() {
return code;
}

/**
* @param code the code to set
*/
public void setCode(String code) {
this.code = code;
}

/**
* @return the title
*/
public String getTitle() {
return title;
}

/**
* @param title the title to set
*/
public void setTitle(String title) {
this.title = title;
}

@Override
public String toString() {
return "Code: " + code + " Title: " + title;
}


}

如果您不想使用域类,则必须将 Text 从 selectedItem 中分离出来,只打印出 Code 部分。

帕特里克

关于java - JComboBox 从所选项目中的多个变量中获取变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24851129/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com