Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JComboBox not showing arrow

I have been searching this site and google for a solution to my problem, and I can't find anything. I think it's supposed to just work; however, it doesn't. The arrow icon for my JComboBox doesn't show up, and I can't find anywhere to set its visibility to true.

Here's my code:

public class Driver implements ActionListener {

private JTextField userIDField;
private JTextField[] documentIDField;
private JComboBox repository, environment;
private JButton close, clear, submit;
private JFrame window;

    public Driver()
    {
    window = makeWindow();
    makeContents(window);
    window.repaint();
    }

    private JFrame makeWindow()
    {
    JFrame window = new JFrame("");
    window.setSize(500,300);
    window.setLocation(50,50);
    window.getContentPane().setLayout(null);
    window.setResizable(false);
    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    window.setVisible(true);

    return window;
    }

    private void makeContents(JFrame w)
    {
    makeDropDowns(w);
    w.repaint();
    }

    private void makeDropDowns(JFrame w)
    {       
    String[] repositoryArray = {"Click to select", "NSA", "Finance", "Test"};
    repository = new JComboBox(repositoryArray);
    repository.setSelectedIndex(0);
    repository.addActionListener(this);
    repository.setSize(150,20);
    repository.setLocation(175,165);
    repository.setEditable(false);
    w.add(repository);

    String[] environmentArray = {"Click to select", "Dev", "Test", "Qual"};
    environment = new JComboBox(environmentArray);
    environment.setSelectedIndex(0);
    environment.addActionListener(this);
    environment.setSize(150,20);
    environment.setLocation(175,195);
    //environment.setEditable(false);
    w.add(environment,0);
}

    public void actionPerformed(ActionEvent e) 
    {

    String repositoryID = "null", environmentID = "null";

    if (e.getSource() == repository)
        {
        repositoryID = (String)repository.getSelectedItem();
        }

    if(e.getSource() == environment)
        {
        environmentID = (String)environment.getSelectedItem();
        }
    }
}

Here's a link to a picture of the problem:

image

If anyone could help that would be awesome.

like image 358
Deo Avatar asked May 30 '12 20:05

Deo


People also ask

What is the difference between JList and JComboBox?

A JComboBox is a component that displays a drop-down list and gives users options that we can select one and only one item at a time whereas a JList shows multiple items (rows) to the user and also gives an option to let the user select multiple items.

How do I make JComboBox not editable?

u can make ur jcombobox uneditable by calling its setEnabled(false). A JComboBox is unEditable by default. You can make it editable with the setEditable(boolean) command. If you are talking about enabled or disabled you can set that by the setEnabled(boolean) method.

Which is the superclass for JComboBox?

Other methods you are most likely to invoke on a JComboBox object are those it inherits from its superclasses, such as setPreferredSize . See The JComponent API for tables of commonly used inherited methods.


2 Answers

It doesn't appear to be the issue you were suffering from, but I found this post due to the same resulting issue of the arrow disappearing.

In my case it was due to me mistakenly using .removeAll() on the JComboBox rather than .removeAllItems() when I was attempting to empty and then reuse the JComboBox after a refresh of the data I was using. Just thought I'd include it as an answer in case someone else comes across this thread for similar reasons.

like image 69
DGolberg Avatar answered Sep 28 '22 10:09

DGolberg


The code you show works, but it looks like you're fighting the enclosing container's default layout. Here, ComboTest is a JPanel which defaults to FlowLayout.

Addendum: In general, do not use absolute positioning, as shown in your update. I've changed the example to use GridLayout; comment out the setLayout() call to see the default, FlowLayout.

enter image description here

import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;

/**
* @see https://stackoverflow.com/a/10824504/230513
*/
public class ComboTest extends JPanel {

    private JComboBox repository = createCombo(new String[]{
        "Click to select", "NSA", "Finance", "Test"});
    private JComboBox environment = createCombo(new String[]{
        "Click to select", "Dev", "Test", "Qual"});

    public ComboTest() {
        this.setLayout(new GridLayout(0, 1));
        this.add(repository);
        this.add(environment);
    }

    private JComboBox createCombo(String[] data) {
        final JComboBox combo = new JComboBox(data);
        combo.setSelectedIndex(1);
        combo.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                System.out.println(e.getActionCommand()
                    + ": " + combo.getSelectedItem().toString());
            }
        });
        return combo;
    }

    private void display() {
        JFrame f = new JFrame("ComboTest");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.add(this);
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

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

            @Override
            public void run() {
                new ComboTest().display();
            }
        });
    }
}
like image 33
trashgod Avatar answered Sep 28 '22 09:09

trashgod