不可编辑的JComboBox中所选项目的背景颜色是一种蓝色:

有没有办法让它变成不同的颜色,例如白色?
我有一个JComboBox的子类.我尝试使用以下代码添加一个密钥监听器.
addKeyListener(new KeyAdapter()
{
public void keyPressed(KeyEvent evt)
{
if(evt.getKeyCode() == KeyEvent.VK_ENTER)
{
System.out.println("Pressed");
}
}
});
然而,这无法正确检测用户何时按下按键.它实际上根本没有被调用.我添加这个监听器错了吗?还有其他方法可以添加吗?
嗨,如果您将JComboBox放在JTable和String []数组中给JComboBox一切正常.Buf如果您将自己的数据类型放到JComboBox中,则选择同一列中的值会变得复杂.这是官方的例子.尝试更改以下部分:
JComboBox comboBox = new JComboBox();
comboBox.addItem("Snowboarding");
comboBox.addItem("Rowing");
comboBox.addItem("Knitting");
comboBox.addItem("Speed reading");
comboBox.addItem("Pool");
comboBox.addItem("None of the above");
sportColumn.setCellEditor(new DefaultCellEditor(comboBox));
Run Code Online (Sandbox Code Playgroud)
成:
JComboBox comboBox = new JComboBox();
comboBox.addItem(new Test("Snowboarding"));
comboBox.addItem(new Test("Rowing"));
comboBox.addItem(new Test("Knitting"));
comboBox.addItem(new Test("Speed reading"));
comboBox.addItem(new Test("Pool"));
comboBox.addItem(new Test("None of the above"));
sportColumn.setCellEditor(new DefaultCellEditor(comboBox));
Run Code Online (Sandbox Code Playgroud)
并创建新的数据类型:
public class Test {
private String name;
public Test(String name) {
this.name = name;
}
@Override
public String toString() {
return name;
}
}
Run Code Online (Sandbox Code Playgroud)
你会看到,当你点击女巫中的表格单元格时,会有一个带有自定义数据类型的JComboBox.自动选择第一列单元格的值.如何解决这个问题?
编辑1:我添加了SSCCE.
主类:
import java.awt.BorderLayout;
public class windw extends …Run Code Online (Sandbox Code Playgroud) Java中的组合框是否有一种方法可以将组合框中的项目居中?我尝试了这个,但它不起作用:
myCombobox.setAlignmentY(CENTER_ALIGNMENT);
Run Code Online (Sandbox Code Playgroud)
谢谢!
每当从JComboBox中选择一个项目时,我都试图从图像文件夹中将图标设置为JLabel.JComboBox中的项目名称和文件夹中图像的名称相同.因此,无论何时从JComboBox中选择一个项目,都应将具有相同名称的相应图像设置为JLabel的图标.我想做这样的事情.
private void cmb_movieselectPopupMenuWillBecomeInvisible(javax.swing.event.PopupMenuEvent evt){
updateLabel(cmb_moviename.getSelectedItem().toString());
}
protected void updateLabel(String name) {
ImageIcon icon = createImageIcon("C:\\Users\\xerof_000\\Pictures\\tmspictures\\" + name + ".jpg");
if(icon != null){
Image img = icon.getImage();
Image newimg = img.getScaledInstance(lbl_pic.getWidth(), lbl_pic.getHeight(), java.awt.Image.SCALE_SMOOTH);
icon = new ImageIcon(newimg);
lbl_pic.setIcon(icon);
lbl_pic.setText(null);
}
else{
lbl_pic.setText("Image not found");
lbl_pic.setIcon(null);
}
}
protected static ImageIcon createImageIcon(String path) {
URL imgURL;
imgURL = NowShowing.class.getResource(path);
if (imgURL != null) {
return new ImageIcon(imgURL);
} else {
return null;
}
}
Run Code Online (Sandbox Code Playgroud)
我认为问题出现在"C:\ Users\xerof_000\Pictures\tmspictures \"中我尝试使用"C:/ Users/xerof_000/Pictures/tmspictures /",但即使这样也行不通.无论我做什么,它只在JLabel上显示"Image not found".
当我在Windows 7上使用JComboBox时,四个角的每个角都有一个与父组件的背景颜色不匹配的像素.
在Windows 8中,这个问题不会发生(尽管这可能是因为在Windows 8中,JComboBox被渲染为完美的矩形).它也不会发生在OS X上.
我该怎么做才能使角落像素让父组件的背景颜色通过?
这是显示问题的图像:

这是我正在使用的一个独立的代码示例:
import com.sun.java.swing.plaf.windows.WindowsLookAndFeel;
import javax.swing.*;
import java.awt.*;
public class Main {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
try {
UIManager.setLookAndFeel(new WindowsLookAndFeel());
} catch (Exception e) {
e.printStackTrace();
}
JPanel contentPane = new JPanel();
contentPane.setBackground(Color.WHITE);
JComboBox<String> comboBox = new JComboBox<String>(new String[]{"One", "Two"});
contentPane.add(comboBox);
JFrame frame = new JFrame("JComboBox Test");
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setContentPane(contentPane);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
}
Run Code Online (Sandbox Code Playgroud) 我正在寻找一个只有当用户(正在使用该程序的用户)选择一个项目时才会触发的监听器JComboBox.我不想使用ActionListener或ItemListener因为当我通过程序选择项目时也会触发.我也不能使用MouseListener它,因为它只在我点击时才会触发JComboBox,而不是在我选择一个项目时触发.
我想知道最简单的方法是什么?目前,我的解决方案很混乱.当我通过代码更改jcombobox的选定项目时,我将标志设置为true.在我的动作监听器中,只有在标志为false时才会执行.
我发现,当一个人希望用户从中选择jcombobox时,一种有用的方法是让它在通常在用户完成前一项时获得焦点时下拉.怎么能在Java中完成?

我想将jcombobox添加到jtable请有人解释我怎么能在netbeans中做到这一点
我使用以下代码成功将JTextField绑定到JTable的选定行:
public static final void BindTableToFields(JTable Table, Object[] Fields) {
for (Object Field : Fields) {
BeanProperty<JTable, Object> tableBeanProperty;
String DesignCallID;
String PropertyName;
if (Field instanceof JTextField) {
BeanProperty<JTextField, String> BindingFieldProperty;
Binding<JTable, Object, JTextField, String> binding;
JTextField jTextField = (JTextField) Field;
DesignCallID = jTextField.getText();
PropertyName = "selectedElement." + GetCorrespondingColumn(Table, DesignCallID);
jTextField.setText("");
System.out.println("property name =" + PropertyName.trim());
tableBeanProperty = BeanProperty.create(PropertyName);
BindingFieldProperty = BeanProperty.create("text");
binding = Bindings.createAutoBinding(UpdateStrategy.READ_WRITE,
Table,
tableBeanProperty,
jTextField,
BindingFieldProperty);
Converter<Object, String> old = binding.getConverter();
Converter<Object, String> converter = new Converter<Object, …Run Code Online (Sandbox Code Playgroud)