我正在尝试从SqlServer中的数据填充JComboBox。我正在使用WindowBuilder,如果这有所作为。这是我到目前为止的代码。我是Java的新手,所以我不知道该怎么做。
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTable;
public class Customer extends JFrame{
private JFrame frame;
private JTable table;
private JTable tblInformation;
private JTable tblHistory;
/**
* Launch the application.
*/
public static void NewScreen()
{
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Customer window = new Customer();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public Customer() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 819, 656);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JComboBox comboBox = new JComboBox();
comboBox.setBounds(372, 28, 152, 22);
frame.getContentPane().add(comboBox);
JLabel lblCustomer = new JLabel("Select customer");
lblCustomer.setFont(new Font("Tahoma", Font.PLAIN, 16));
lblCustomer.setBounds(245, 30, 121, 16);
frame.getContentPane().add(lblCustomer);
JLabel lblCustomerInformation = new JLabel("Information");
lblCustomerInformation.setFont(new Font("Tahoma", Font.PLAIN, 16));
lblCustomerInformation.setBounds(12, 74, 92, 16);
frame.getContentPane().add(lblCustomerInformation);
JLabel lblHistory = new JLabel("History");
lblHistory.setFont(new Font("Tahoma", Font.PLAIN, 16));
lblHistory.setBounds(12, 334, 56, 16);
frame.getContentPane().add(lblHistory);
JButton btnEdit = new JButton("Edit");
btnEdit.setBounds(12, 569, 97, 25);
frame.getContentPane().add(btnEdit);
JButton btnExit = new JButton("Exit");
btnExit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Welcome.main(null);
}
});
btnExit.setBounds(690, 569, 97, 25);
frame.getContentPane().add(btnExit);
JButton btnAdd = new JButton("Add");
btnAdd.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0)
{
String custIDString, firstName, lastName, companyName, phoneNumber, street, city, state, zip;
int custID;
custIDString = JOptionPane.showInputDialog(null,"Enter customer ID", "Customer ID", JOptionPane.INFORMATION_MESSAGE);
custID = Integer.parseInt(custIDString);
firstName = JOptionPane.showInputDialog(null,"Enter first name", "First Name", JOptionPane.INFORMATION_MESSAGE);
lastName = JOptionPane.showInputDialog(null,"Enter last name", "Last Name", JOptionPane.INFORMATION_MESSAGE);
companyName = JOptionPane.showInputDialog(null,"Enter company name", "Company Name", JOptionPane.INFORMATION_MESSAGE);
phoneNumber = JOptionPane.showInputDialog(null,"Enter phone number (xxx-xxx-xxx)", "Phone Number", JOptionPane.INFORMATION_MESSAGE);
street = JOptionPane.showInputDialog(null,"Enter street", "Street", JOptionPane.INFORMATION_MESSAGE);
city = JOptionPane.showInputDialog(null,"Enter city", "City", JOptionPane.INFORMATION_MESSAGE);
state = JOptionPane.showInputDialog(null,"Enter state", "State", JOptionPane.INFORMATION_MESSAGE);
zip = JOptionPane.showInputDialog(null,"Enter zip", "Zip", JOptionPane.INFORMATION_MESSAGE);
}
});
btnAdd.setBounds(235, 569, 97, 25);
frame.getContentPane().add(btnAdd);
table = new JTable();
table.setBounds(257, 168, 137, -66);
frame.getContentPane().add(table);
tblInformation = new JTable();
tblInformation.setBounds(12, 96, 422, 156);
frame.getContentPane().add(tblInformation);
tblHistory = new JTable();
tblHistory.setBounds(12, 357, 422, 177);
frame.getContentPane().add(tblHistory);
JButton btnDelete = new JButton("Delete");
btnDelete.setBounds(474, 569, 97, 25);
frame.getContentPane().add(btnDelete);
}
public class TestComboBox extends JComboBox
{
private Connection sqlCon;
private Statement st;
public TestComboBox()
{
super();
initComponents();
}
private void initComponents()
{
try
{
st = sqlCon.createStatement();
loadComboBox();
}
catch (SQLException sqle)
{
System.out.println(sqle);
}
}
public void loadComboBox()
{
this.removeAllItems();
this.addItem("Please select");
try
{
ResultSet rs = st.executeQuery("select FirstName, LastName from Customer");
while (rs.next())
{
this.addItem(rs.getString("FirstName") + "" + rs.getString("LastName"));
}
}
catch (SQLException sqle)
{
System.out.println(sqle);
}
}
}
}
答案 0 :(得分:0)
OBJ_FILES := $(addprefix Objects/,$(notdir $(OBJ_FILES))
这里有一个例子,我在这里做的是从public void loadCombo(){
Vector vec = new Vector();
try {
resultset = //"resultst from database";
while (resultset.next()) {
vec.add(resultset.getString("table column"));
}
yourCombo.setModel(new DefaultComboBoxModel(vec));
} catch (Exception ex) {
ex.printStackTrace();
}
}
获取values
将值放入resultset
然后在{{1}中添加vector
试试这个。
答案 1 :(得分:0)
您是否听说过在课程中重写toString方法?我试着解释一下。
如果你有一个名为Customers的类,这个类的属性是firstName和lastName(带有setter和getters),那么你可以覆盖Customers类中的toString方法,如下所示:
@Override
public String toString() {
return firstName;
}
然后你可以创建一个返回DefaultComboBoxModel的类Example:
public static DefaultComboBoxModel getCustomerModel() {
DefaultComboBoxModel model = new DefaultComboBoxModel();
ResultSet rs = st.executeQuery("select FirstName, LastName from Customer");
while (rs.next()) {
Customer c = new Customer();
c.setFirstName(rs.getString("FirstName"));
c.setLastName(rs.getString("LastName"));
model.addElement(c);
}
return model;
}
最后设置你的组合框模型:
myComboBox.setModel(Example.getCustomerModel());
因此,在myComboBox中,您将只看到客户的firstName,如果您想从myComboBox对象获取Customer,则:
Customer c = (Customer) myComboBox.getSelectedItem();
问候。