Search code examples
javajcomboboxswingx

JComboBox DropDown list is not displayed


this might be a duplicate of JComboBox popup menu not appearing , but as it is a rather old question and not been active for quite some time, plus all the answers were not solutions, that helped with my problem. Thus I decided to create a new question.

The Problem is as follows: I got an application of a prior colleque, that does not work at my company anymore. Now I tried adding a JComboBox to a JPanel. The JCombobox is displayed as expected, but it behaves in the same way as described by Seth in his question:

1) The first click on the expand button does nothing. The second click highlights the contents of the box, but the popup still doesn't appear.

2) Once I've clicked the button and given it focus, up/down keystrokes cycle through the entries correctly.

I have broken down the code to what I think is the minimum of needed programming, to have the problem occur. (As one comment in the mentioned question mentioned to provide SSCCE, which never happened).

Now here is the code I can provide:

public static class CreateProjectDialog extends JFrame {

private Dimension size = Toolkit.getDefaultToolkit().getScreenSize();

public CreateProjectDialog() {

    setDefaultCloseOperation(EXIT_ON_CLOSE);
    int SZ_INCR = 1;

    // Passe Fontgröße an Resolution an:
    if (size.width > 1920) {
        SZ_INCR = 2;
    }

    // Initialize Glass Layer
    final JPanel panelGlass = (JPanel) getGlassPane();
    panelGlass.setLayout(null);
    panelGlass.setVisible(true);

    private static JPanel licBorrowPanel = null;

    licBorrowPanel = new JPanel();
    licBorrowPanel.setBounds(0, 20, 1000, 500);
    licBorrowPanel.setVisible(false);
    licBorrowPanel.setBackground(Color.WHITE);
    panelGlass.add(licBorrowPanel);
}

public static void main(String[] args) {
    hauptFrame = new CreateProjectDialog();
}

public static void licenceBorrowDialog() {

    int mainWidth = hauptFrame.getSize().width;
    int mainHeight = hauptFrame.getSize().height;

    // pick a Date
    JComboBox dayList = new JComboBox();
    DateFormat df = new SimpleDateFormat("dd/MM/yyyy");
    Calendar calToday = Calendar.getInstance();
    Date dayToday = calToday.getTime();
    int weekDay = calToday.get(Calendar.DAY_OF_WEEK);
    String weekDayName = "";
    for (int i = 1; i <= 22; i++){
        dayToday.setDate(dayToday.getDate()+1);
        weekDay = dayToday.getDay();
        weekDayName = translateWeekDay(weekDay);
        dayList.addItem(i + " day(s) until " + weekDayName + " " + df.format(dayToday));
    }
    dayList.setOpaque(true);
    dayList.setSelectedIndex(2);
    dayList.setBounds(mainWidth / 2 - (125*SZ_INCR), (165*SZ_INCR), (250*SZ_INCR), (100*SZ_INCR));
    licBorrowPanel.add(dayList);

    dayList.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e) {
            int numberOfDays;
            JComboBox dl = (JComboBox)e.getSource();
            numberOfDays = dl.getSelectedIndex()+1;
            labelSelectedDate.setText("<HTML><BODY><b>Count of days: </b>" + numberOfDays + "</HTML></BODY>");
        }
    });
}

//Translate weekday int to name
public static String translateWeekDay(int day){
    String retDay;
    switch (day) {
        case 0:  retDay = "Monday";
            break;
        case 1:  retDay = "Truesday";
            break;
        case 2:  retDay = "Wednesday";
            break;
        case 3:  retDay = "Thursday";
            break;
        case 4:  retDay = "Friday";
            break;
        case 5:  retDay = "Saturday";
            break;
        case 6:  retDay = "Sunday";
            break;
        default: retDay = "Invalid day";
            break;
    }
    return retDay;
}
}

I tried popoulating with more items (as proposed by jluzwick) to see, if the DropDown is simply hidden behind anything, but no.

I definitely have never used getRootPane() instead of getContentPane(), as suspected by Sehtim.

There is also JCombobox is not displayed , where the accepted answer is to set the setVisible(true) to the end of the constructor. I tried that and it did not change any behaviour in my case.

The question I need an answer to, is: How do I make the DropDown list visible, to enable the user to easily choose an entry?


Solution

  • Thanks MadProgrammer for the hint regarding the code not compiling - I found a solution and will provide it here for anyone having a similar issue.

    The problem was a result of mixing heavy weight and light weight components (awt / swing).

    This resulted in the light weight popup being used, which was then probably occluded by other components and thus not visible.

    The solution ( if the mix of both heavy and light weight has to stay ) is to disable the light weight popup forcing the application to use a backup popup. This is done by replaceing the following line:

    dayList.setSelectedIndex(2);
    

    With this line:

    dayList.setLightWeightPopupEnabled (false);
    

    I found the solution here: http://de.comp.lang.java.narkive.com/t2GPS9vy/jcombobox-poppt-nicht-auf