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]
# 1

It works fine for me when I hardcoded multiple addItem(...) statements in the event routine instead of reading the data from a database.

If you need further help then you need to create a [url http://homepage1.nifty.com/algafield/sscce.html]Short, Self Contained, Compilable and Executable, Example Program[/url] (SSCCE) that demonstrates the incorrect behaviour, because I can't guess exactly what you are doing based on the information provided.

Don't forget to use the [url http://forum.java.sun.com/help.jspa?sec=formatting]Code Formatting Tags[/url] so the posted code retains its original formatting.

camickra at 2007-7-12 10:11:48 > top of Java-index,Desktop,Core GUI APIs...
# 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;

}

oliverwga at 2007-7-12 10:11:48 > top of Java-index,Desktop,Core GUI APIs...