2

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);
    }
}
shan
  • 21
  • 5
  • 1
    Trying wrapping the contents of the `main` in `EventQueue.invokeLater(new Runnable() { public void run() {...}});` as outlined in [Initial Threads](http://docs.oracle.com/javase/tutorial/uiswing/concurrency/initial.html) – MadProgrammer Feb 05 '14 at 04:54
  • 1
    `jlbPicture.setPreferredSize(new Dimension(177, 122 + 10));` See [Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?](http://stackoverflow.com/q/7229226/418556) (Yes.) – Andrew Thompson Feb 05 '14 at 04:54
  • hi thanks for your reply. I didn't use the jlbPicture and even now the same issue occurs. – shan Feb 05 '14 at 06:34
  • Tips: 1) Add @MadProgrammer (or whoever - the @ is important) to *notify* them of a new comment. 2) Noting you took my advice, read carefully. *The only reason I did not **also** tell you to create the GUI on the EDT was that it had already been said. **Don't ignore advice.*** – Andrew Thompson Feb 05 '14 at 08:25
  • @Andrew Thompson I didn't understand – shan Feb 05 '14 at 09:26
  • *"I didn't understand"* OK. How do you expect us to know that if you don't mention it? But more importantly, what *specifically* do you not understand? Pretend we are *not* mind readers for a few moments. – Andrew Thompson Feb 05 '14 at 09:30
  • in the code i have just added the jpanel class into the frame. the jpanel class contains the combobox which is listing the contents and unable to select in linux java 1.6 specifically. May i know the exact reason why this is specific to only java 1.6 in linux? – shan Feb 05 '14 at 10:27
  • My actual question is why this difference from java 1.5 and java 1.6 in linux. There was no code change in both versions but why this is behaving differently? – shan Feb 05 '14 at 10:36
  • @shan You don't seem to be understanding much of anything I write, given you are still failing to add notifications to comments. Since I'm obviously failing in my communications, I think it is best I abandon this. Good luck with it. – Andrew Thompson Feb 05 '14 at 23:26

2 Answers2

3

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);
            }
        });
    }
}
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • This also didn't display the dropdown when i unhold the jcombobox(down arrow). – shan Feb 05 '14 at 11:16
  • 1
    I am unable to reproduce this on either Ubuntu 12 or Mac OS X 10.9 using JDK 6. What platform and version are you using? – trashgod Feb 05 '14 at 13:29
  • I am using Linux x86_64 – shan Feb 06 '14 at 04:06
  • You might try updating your system and edit your question to include the exact name and version of your Linux x86_64 distribution and Java development kit; someone may be able to reproduce the problem. – trashgod Feb 06 '14 at 08:57
  • In linux if I do Ctrl + Alt + l then i can able to select the items normally. May i know what exactly happens in the background when i click Ctrl + Alt + l – shan Feb 14 '14 at 03:54
  • I don't know; I'd guess that it causes the host platform's window manager to generate an update event; you might try the changing window manager. – trashgod Feb 14 '14 at 05:40
0

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.

shan
  • 21
  • 5