显示JComboBox项目中的按钮

时间:2012-06-16 16:44:49

标签: java swing jcombobox

我目前有一个JComboBox,我正在使用它作为音频播放列表 - 我想要实现的是每个项目右侧的一个“删除”按钮,我可以使用它从底层删除它模型,圆圈是:

实现这一目标的最佳方式是什么?

我希望JComboBox中的所有项目的按钮都相同。

demo screenshot

1 个答案:

答案 0 :(得分:8)

首先我要说这是一个有趣的问题(+1前一段时间)。

我必须快速尝试自己查看使用JComboBox获得所需结果的难度。我得到的结论(正如@trashgod在上面的评论中所说的那样)是这个对象从来没有被设计成有其他组件,或者至少对我来说这感觉就像这样。

以下是一个可以满足您需求的示例。您可以将它作为一个开始,但说实话,您应该忘记使用JComboBox解决此问题。

绝不是下面的示例提供了解决问题的正确方法。它只是显示了我尝试解决问题的结果。以下代码不保留良好实践规则,例如它将表示与功能混合(渲染器删除元素)。事实上这只是一个黑客而不是真正的解决方案。

import java.awt.*;
import java.awt.event.*;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;

public class ButtonCombo {

    private JPanel getContent() throws MalformedURLException {
        String[] ids = {"north", "west", "south", "east"};
        JComboBox combo = new JComboBox(ids);
        Icon removeIcon = new ImageIcon(new URL("http://filesharefreak.org/images/red_x.png"));
        combo.setRenderer(new ButtonComboRenderer(removeIcon, combo));
        JPanel panel = new JPanel();
        panel.add(combo);
        return panel;
    }

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

            @Override
            public void run() {
                try {
                    JFrame f = new JFrame();
                    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    JPanel panel = new JPanel();
                    panel.add(new ButtonCombo().getContent());
                    JButton button = new JButton("OKOKO");
                    panel.add(button);
                    f.setContentPane(panel);
                    f.setSize(300, 160);
                    f.setLocation(200, 200);
                    f.setVisible(true);
                } catch (MalformedURLException ex) {
                    Logger.getLogger(ButtonCombo.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        });
    }
}

class ButtonComboRenderer implements ListCellRenderer {
    Icon icon;
    JPanel panel;
    JLabel label;
    JButton button;

    public ButtonComboRenderer(Icon removeIcon, final JComboBox combo) {
        icon = removeIcon;
        label = new JLabel();
        button = new JButton(icon);
        button.setPreferredSize(new Dimension(icon.getIconWidth(), icon.getIconHeight()));
        panel = new JPanel(new BorderLayout());
        panel.add(label);
        panel.add(button, BorderLayout.EAST);
        panel.addMouseListener(new MouseAdapter() {

            @Override
            public void mousePressed(MouseEvent e) {
                if (button.getX() < e.getX()) {
                    System.out.println("button contains the click remove the item");
                    combo.removeItem(label.getText());
                }
            }
        });
    }
    //so we will install the mouse listener once
    boolean isFirst = true;

    @Override
    public Component getListCellRendererComponent(JList list,
            Object value,
            int index,
            boolean isSelected,
            boolean cellHasFocus) {
        if (isFirst) {
            isFirst = false;
            list.addMouseListener(new MouseAdapter() {

                @Override
                public void mousePressed(MouseEvent e) {
                    panel.dispatchEvent(e);
                    e.consume();
                }
            });
        }
        String text = (String) value;
        label.setText(text);
        if(text == null)
            button.setIcon(null);
        else if(button.getIcon() == null)
            button.setIcon(icon);
        panel.setBackground(isSelected ? Color.red : Color.white);
        panel.setForeground(isSelected ? Color.white : Color.black);
        return panel;
    }
}

我的最终建议和我的方式是: 构建您自己的组件。通过将其与触发器和演示文件分开来使其可扩展和可修改,其中两者都使用JComponent来反对使用渲染器。通过这种方式,您将能够捕获和提供组件上的事件,而不是在这种情况下,所有事件都由用于渲染的JList捕获。

以下是一个可以帮助您入手的示例。它不是最终解决方案,但它提出了制作此类组件所涉及的许多重要问题。您应该使用所提供的功能并将其全部包装在一个组件中:

import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import javax.swing.*;

public class MockJComboBox {

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

            @Override
            public void run() {
                final JPanel popupContent = new JPanel(new GridLayout(0, 1));
                popupContent.setBackground(Color.GREEN);
                popupContent.add(new JLabel("Content of popupContent panel"));
                popupContent.add(new JLabel("Content of popupContent panel"));
                popupContent.add(new JLabel("Content of popupContent panel"));
                popupContent.add(new JLabel("Content of popupContent panel"));
                popupContent.add(new JLabel("Content of popupContent panel"));
                popupContent.add(new JComboBox(new Object[]{"Content of popupContent panel"}));
                final JButton popupCloseButton = new JButton("X");
                popupContent.add(popupCloseButton);

                final JScrollPane s = new JScrollPane(popupContent);
                s.setPreferredSize(new Dimension(popupContent.getPreferredSize().width + s.getVerticalScrollBar().getPreferredSize().width
                        + s.getBorder().getBorderInsets(s).left
                        + s.getBorder().getBorderInsets(s).right, 100));

                JPanel panel = new JPanel();
                panel.setPreferredSize(new Dimension(200, 200));
                final JButton popupOpenButton = new JButton();
                panel.add(popupOpenButton);
                final JFrame f = new JFrame();
                f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                f.setContentPane(panel);
                final PopupFactory popupFactory = PopupFactory.getSharedInstance();
                popupOpenButton.setAction(new AbstractAction("Open") {
                    private Popup popup;
                    private boolean isShown = false;

                    @Override
                    public void actionPerformed(ActionEvent e) {
                        if (isShown) {
                            popup.hide();
                        } else {
                            popup = popupFactory.getPopup(popupOpenButton, s,
                                    popupOpenButton.getLocationOnScreen().x, popupOpenButton.getLocationOnScreen().y + popupOpenButton.getHeight());
                            popupCloseButton.setAction(new AbstractAction(popupCloseButton.getText()) {

                                @Override
                                public void actionPerformed(ActionEvent e) {
                                    isShown = false;
                                    popup.hide();
                                }
                            });
                            popup.show();
                        }
                        isShown = !isShown;
                    }
                });
                f.pack();
                f.setVisible(true);
            }
        });
    }
}