Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to put a JButton inside a JComboBox

I would like to put a JButton inside a JComboBox. This button lets users browse for files. The file the user selects gets added to the JComboBox list. How do I do this? Do I use some kind of a Renderer? Thank you.

EDIT: After reading more about ListCellRenderer I tried the following code:

JComboBox comboBox = new JComboBox(new String[]{"", "Item1", "Item2"});
ComboBoxRenderer renderer = new ComboBoxRenderer();
comboBox.setRenderer(renderer);

class ComboBoxRenderer implements ListCellRenderer {

    public Component getListCellRendererComponent(
            JList list,
            Object value,
            int index,
            boolean isSelected,
            boolean cellHasFocus) {

        JButton jbutton = new JButton("Browse");

        return jbutton;
    }
}

The problem with the above is that the Button "Browse" will be added 3 times and I want it to display only once and below it to display Item1 and Item2 as normal/regular combobox selection objects.

like image 248
jadrijan Avatar asked Jan 16 '23 18:01

jadrijan


1 Answers

I would avoid the JButton. It is perfectly possible to get the image of a JButton inside your combobox, but it will not behave itself as a button. You cannot click it, it never gets visually 'pressed' nor 'released', ... . In short, your combobox will contain an item which behaves unfamiliar to your users.

The reason for this is that the components you return in the getListCellRendererComponent method are not contained in the JCombobox. They are only used as a stamp. That also explains why you can (and should) reuse the Component you return in that method, and not create new components the whole time. This is all explained in the JTable tutorial in the part about Renderers and Editors (explained for a JTable but valid for all other Swing components which use renderers and editors).

If you really want an item in the combobox that allows to show a file chooser, I would opt for something similar to the following SSCCE:

import javax.swing.JComboBox;
import javax.swing.JFrame;
import java.awt.EventQueue;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

public class ComboboxTest {

  public static void main( String[] args ) {
    EventQueue.invokeLater( new Runnable() {
      @Override
      public void run() {
        JFrame frame = new JFrame( "TestFrame" );
        JComboBox<String> comboBox = new JComboBox<>(new String[]{"Item1", "Item2"});
        final String browse = "<<BROWSE>>";
        comboBox.addItem( browse );
        comboBox.addItemListener( new ItemListener() {
          @Override
          public void itemStateChanged( ItemEvent e ) {
            if ( e.getStateChange() == ItemEvent.SELECTED && 
                browse.equals( e.getItem() ) ){
              System.out.println("Show filechooser");
            }
          }
        } );
        frame.add( comboBox );
        frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
        frame.setVisible( true );
        frame.pack();
      }
    } );
  }
}
like image 173
Robin Avatar answered Jan 18 '23 08:01

Robin