Question

I am having a problem with jcombobox in linux java 1.6. The exact problem is the combo is listing the items when i click the combobox (down arrow) but it minimizes if the selection goes off. i.e I am unable to select the items in the list. The same code is working in windows(java1.5, 1.6) and linux(java 1.5). The prob is only in linux java 1.6.

Please help me to get out of this. Thanks in advance.

Below is the code, 

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class JComboBoxDemo extends JPanel {


    public JComboBoxDemo() {
        String[] comboTypes = { "Numbers", "Alphabets", "Symbols" };
        // Create the combo box, and set 2nd item as Default
        JComboBox comboTypesList = new JComboBox(comboTypes);
        comboTypesList.setSelectedIndex(2);
        comboTypesList.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                JComboBox jcmbType = (JComboBox) e.getSource();
                String cmbType = (String) jcmbType.getSelectedItem();
                System.out.println(cmbType);
            }
        });
        // Set up the picture

        // Layout the demo
        setLayout(new BorderLayout());
        add(comboTypesList, BorderLayout.NORTH);

        setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
    }
    public static void main(String s[]) {
        JFrame frame = new JFrame("JComboBox Usage Demo");
        frame.addWindowListener(new WindowAdapter() {

            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
        frame.setContentPane(new JComboBoxDemo());
        frame.pack();
        frame.setVisible(true);
    }
}
Was it helpful?

Solution 2

Thanks for all your help.

Finally i had found the solution after so much effort (Even its a simple solution).

The issue got fixed when i call frame.setUndecorated(true);

In my application we have customized OS, which will not support decorated frame.

So when i call this method it works fine.

OTHER TIPS

The following variation works correctly on Ubuntu 12, OpenJDK 6. The only significant change was starting on the event dispatch thread.

Why this difference from java 1.5 and java 1.6 in linux.

Java Swing has always required correct synchronization. Migrating to successive versions sometimes exposes a latent error.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class JComboBoxDemo extends JPanel {

    public JComboBoxDemo() {
        String[] comboTypes = {"Numbers", "Alphabets", "Symbols"};
        JComboBox comboTypesList = new JComboBox(comboTypes);
        comboTypesList.setSelectedIndex(2);
        comboTypesList.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                JComboBox jcmbType = (JComboBox) e.getSource();
                String cmbType = (String) jcmbType.getSelectedItem();
                System.out.println(cmbType);
            }
        });
        setLayout(new BorderLayout());
        add(comboTypesList, BorderLayout.NORTH);
        setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
    }

    public static void main(String s[]) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                JFrame frame = new JFrame("JComboBox Usage Demo");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setContentPane(new JComboBoxDemo());
                frame.pack();
                frame.setVisible(true);
            }
        });
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top