如何将JButton放在JComboBox内 [英] How to put a JButton inside a JComboBox

查看:65
本文介绍了如何将JButton放在JComboBox内的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将JButton放在JComboBox内.用户可以使用此按钮浏览文件.用户选择的文件将添加到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.

阅读有关ListCellRenderer的更多信息后,我尝试了以下代码:

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;
    }
}

上述问题是浏览"按钮将被添加3次,并且我希望它仅显示一次,并在其下方显示以将Item1和Item2显示为常规/常规组合框选择对象.

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.

推荐答案

我会避免使用JButton.完全有可能在组合框中获取JButton的图像,但它本身不会像按钮一样.您无法单击它,它永远不会在视觉上被按下"或释放",....简而言之,您的组合框将包含一个用户不熟悉的项目.

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.

这样做的原因是,在JCombobox中不包含您在getListCellRendererComponent方法中返回的组件.它们仅用作邮票.这也解释了为什么您可以(并且应该)重用在该方法中返回的Component,而不是始终都没有创建新组件.关于渲染器的部分,在JTable教程中对此进行了全部解释.和编辑器(对JTable进行了解释,但对于使用渲染器和编辑器的所有其他Swing组件有效.)

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).

如果您确实想要组合框中的一个项目来显示文件选择器,那么我会选择类似于以下SSCCE的项目:

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();
      }
    } );
  }
}

这篇关于如何将JButton放在JComboBox内的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆