JComboBox show single item
I use a JComboBox to let the user select the status.
And I click the combobox, it is only show one item, and the right hand side show the scroll button (like a JList).
But when i click it again, the combobox becomes normal. It can show a few items.
So, how can i make it to normal when my first click.
Thank you!
My logic is that,
1. user clicks combobox
2. add data from sql and add it to combobox by additem
3. wait for user select the item
the following is my coding
Status.setMaximumRowCount(4);
Status.addPopupMenuListener(new javax.swing.event.PopupMenuListener(){
publicvoid popupMenuWillBecomeVisible(javax.swing.event.PopupMenuEvent evt){
LUCustomerStatus(evt);
}
privatevoid LUCustomerStatus(javax.swing.event.PopupMenuEvent evt){
JComboBox obj=(JComboBox) evt.getSource();
obj.removeAllItems();
try{
String sql="SELECT * FROM CUSTOMER_STATUS";// it will return 20 rows
PreparedStatement pstat=dbcon.prepareStatement(sql);
ResultSet rs=pstat.executeQuery();
while (rs.next()){
obj.addItem(rs.getString("DESCRIPTION"));
}
}
catch (SQLException e){}
}
[1884 byte] By [
oliverwga] at [2007-11-27 4:56:35]

# 2
I find a some hint, but i don't know why it can!
if i omit this statement, it cannot show 6 items for my first click, when the statement was added, it is work fine.
jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "", "", "", "", "", "" }));
the following code is generated by NetBeans IDE 5.5
import java.sql.*;
import java.util.*;
import java.util.Date;
import javax.swing.*;
import javax.swing.text.MaskFormatter;
import javax.swing.UIManager;
public class TestCombo extends javax.swing.JFrame {
public TestCombo() {
initComponents();
DBConnect objConnection=new DBConnect();
dbcon=objConnection.getDBConnect();
}
private void initComponents() {
jComboBox1 = new javax.swing.JComboBox();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jComboBox1.setMaximumRowCount(6);
jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "", "", "", "", "", "" }));
jComboBox1.addPopupMenuListener(new javax.swing.event.PopupMenuListener() {
public void popupMenuCanceled(javax.swing.event.PopupMenuEvent evt) {
}
public void popupMenuWillBecomeInvisible(javax.swing.event.PopupMenuEvent evt) {
}
public void popupMenuWillBecomeVisible(javax.swing.event.PopupMenuEvent evt) {
jComboBox1PopupMenuWillBecomeVisible(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(80, 80, 80)
.addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, 252, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(68, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(52, 52, 52)
.addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(227, Short.MAX_VALUE))
);
pack();
}
private void jComboBox1PopupMenuWillBecomeVisible(javax.swing.event.PopupMenuEvent evt) {
jComboBox1.removeAllItems();
try {
String sql="SELECT * FROM PAYMENT_TERM";
PreparedStatement pstat=dbcon.prepareStatement(sql);
ResultSet rs=pstat.executeQuery();
while (rs.next()) {
jComboBox1.addItem(rs.getString("PAYMENT_TERM"));
}
}
catch (SQLException e) {}
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new TestCombo().setVisible(true);
}
});
}
private javax.swing.JComboBox jComboBox1;
private Connection dbcon=null;
}