- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我目前有一个 JComboBox,我将其用作音频播放列表 - 我想要实现的是每个项目右侧的一个小“删除”按钮,我可以使用它从底层删除它模型,其中圆圈是:
实现此目标的最佳方法是什么?
我希望 JComboBox 中所有项目的按钮都相同。
最佳答案
首先让我说这是一个有趣的问题(不久前 +1)。
我必须快速尝试并亲眼看看使用 JComboBox
实现想要的结果有多困难。我得到的结论(正如@trashgod 在上面的评论中所说)是这个对象从未被设计为具有其他组件,或者至少对我来说是这样的。
下面是一个示例,可以执行您想要的操作。您可以将它作为一个开始,但老实说,您应该忘记使用 JComboBox
来解决这个问题。
下面的示例绝不代表解决问题的正确方法。它只是显示了我尝试解决问题的结果。下面的代码不保留良好的实践规则,例如它将表示与功能混合在一起(渲染器删除了元素)。这实际上只是一个 hack 而不是真正的解决方案。
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);
}
});
}
}
关于java - 在 JComboBox 项中显示按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11065282/
我正在尝试组合 2 个 jcomboboxes。 1 组合框用于显示费用类别。第二个组合框正在从文本文件中读取文件以显示产品类型。如果我更改第一个组合框,我希望第二个组合框会根据用户在第一个组合框中选
我希望一个 JComboBox 的值根据另一个 JComboBox 的值进行更改。在一个 JComboBox 中,我显示了从数据库检索的医生名称。根据该名称,我想在另一个 JComboBox 中显示医
我有课 public class Speciality { String specName; String eduProgram; } 和 TreeMap Map> treeM
我有一个 JComboBox。我希望它能够工作,以便如果选择某个项目(“其他”),立即在同一个组合框中显示更多项目(类似于子菜单,但在组合框内)。我很难让这个工作发挥作用。 有人对如何做到这一点有任何
如何从 JComboBox 中进行选择,删除或添加选择到另一个 JComboBox? 更具体地说,我有一个 JComboBox,其中包含一年中的月份作为选择。我还有另一个,里面有这个月的日子。虽然 3
我有一个用例(在 Java Swing 中),其中某个 JTable 列必须可以通过 JComboBox 编辑器进行编辑,但可选择的值列表取决于该行的域对象(我使用自定义 TableModel)。 C
我们可以在执行 jFrame 后根据点击另一个 jcomboBox 的项目来启用 jcomboBox 吗?这可能吗? 最佳答案 JComboBox comboBox_1 = new JComboBox
我有 7 个 jcombobox,它们都以相同的默认选择开头。如果在前面的 jcombobox 中选择了默认值以外的内容,如何启用下一个? if ( ! (custData1.equa
我有 2 个 JComboBox,在第一个 JComboBox 上选择一个项目后,第二个会从数据库中填充。问题是,每次我输入一个字母时,第二个 jcombobox 都会填充。我想让第二个 jcombo
您好,我正在尝试将一个对象从一个组合框传输到另一个组合框,反之亦然。为了实现这一目标,我使用 actionListeners 或 ItemListeners,不幸的是它们没有回答我的问题,或者可能只是
当我从 JComboBox A 中选择项目时,我尝试在 JComboBox B 中显示值。到目前为止,当我从 JComboBox A 中选择一个值时,没有任何反应。这是我的数据和代码。例如,如果我从
我写了这个简单的程序: import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.tab
我正在制作一个医疗药物软件程序,我希望用户能够从由类别数组填充的 jcombo 框中选择疾病类别(呼吸系统疾病、心血管疾病等)。 选择此类别后,我希望另一个 jcombobox 仅填充与该类别相关的药
这段代码很草率,我也欢迎一些反馈。 我正在尝试根据另一个 JComboBox 的值更改 JComboBox 的值。还有一个额外的复杂性,因为我使用一个额外的类来确定要返回的字符串数组(请参阅我之前的问
我已经加载了对象的 JComboBox,这些对象有自己的“toString”方法。该组合框用于选择并通过该组合框上的“ActionListener”返回该对象。一切工作正常,直到我决定添加通过在此组合
所以我的问题是 JComboBoxs 和 ActionListeners。我将制作一个新的简化代码来尝试代表我的原始代码中的问题。我想要一个 JComboBox 添加一个 JComboBox,然后 J
我的转换器程序中有 3 个 JCombobox,其中一个用于类别,两个用于选择要执行的功能;我希望程序做的是,当用户在一个 JCombobox 中选择一个类别时,两个 JCombobox 将自动更改与
所以,这就是问题所在:我需要 3 个或更多 jComboBox(都具有相同的项目)相互连接,这样当我在任何 jComboBox 中选择一个项目时,该项目将在所有其他 jComboBox 中消失。具体例
我用这样的东西制作了一个图形用户界面: String[] days29 = {"1",....."29"}; String[] days30 = {"1",....."30"}; String[] d
我有一个小应用程序,它通过 JPA 从 MySQL 数据库生成统计图表。为了选择要包含在统计信息中的数据库组件,我安装了 2 个 JComboBoxes。第一个 JComboBox 填充了 Categ
我是一名优秀的程序员,十分优秀!