我JComboBox有一个自定义inputVerifyer设置,以限制MaxLength设置为可编辑时.
验证方法似乎永远不会被调用.
在罚款时调用相同的verifyer JTextField.
我可能做错了什么?
我有一个JComboBox对象,JFrame其行为完全符合预期.但是,我想让它可编辑,以便用户可以根据需要输入替代值.不幸的是,使用setEditable(true)启用编辑会导致其宽度显着增加.如何允许编辑JComboBox同时保持其宽度仅与最大可选项目所需的宽度相同?
该JComboBox处于JPanel同一个FlowLayout,但我不认为这是相关的,因为改变的宽度JPanel不影响的宽度JComboBox.

我想以这样的方式修改(不可编辑的)显示JComboBox,使得当前选择的条目在编辑字段中具有一些额外的文本(但不是下拉列表).
像这样的东西:

我的第一个猜测是覆盖ComboBox的模型,以便getSelectedItem返回修改显示的包装器对象:
petList.setModel(new ComboBoxModel() {
private Object selected;
public void setSelectedItem(Object anItem) {
selected = anItem;
}
public Object getSelectedItem() {
return new ActiveComboItem(selected);
}
// … The rest of the methods are straightforward.
});
Run Code Online (Sandbox Code Playgroud)
凡ActiveComboItem如下所示:
static class ActiveComboItem {
private final Object item;
public ActiveComboItem(Object item) { this.item = item; }
@Override
public boolean equals(Object other) {
return item == null ? other == null : item.equals(other);
}
@Override
public …Run Code Online (Sandbox Code Playgroud) 我有一个JComboBox包含一些员工ID号(它是整数值).
我想将"选择员工"设置为JComboBox作为默认值.由于这个值是字符串格式,它会抛出一个异常,比如"java.lang.NumberFormatException:For input string:"选择Employee"".我该怎么做呢?
我的代码是:
public void clear()
{
cmb_emp_id.setSelectedItem("Select Employee");
txt_emp_name.setText("");
txt_department.setText("");
txt_designation.setText("");
joining_date.setDate(new Date());
resign_date.setDate(new Date());
txt_description.setText("");
}
Run Code Online (Sandbox Code Playgroud)
我怎么能做到这一点?
当我选择JCombobox我想要处理事件时,当它被选中并显示下拉列表以及当下拉消失并且JCombobox取消选择时处理事件.
注意,我不打算听取项目选择更改,但是当用户选择JCombobox并且UI弹出Dropdown时.
将JComboBox组件放入一个内部时,我遇到了一种奇怪的行为GroupLayout.我已经减少了代码到下面小例子,正好特色的一个JComboBox奠定了通过GroupLayout.
观察到的行为如下:
我已经发现了什么:
这是示例代码 - 欢迎提出意见:
import java.awt.BorderLayout;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JRootPane;
public class DummyUI_cbdiagnosis extends javax.swing.JPanel {
private javax.swing.JComboBox cbCategory;
public DummyUI_cbdiagnosis() {
initComponents();
}
private void initComponents() {
cbCategory = new JComboBox();
cbCategory.setModel(new javax.swing.DefaultComboBoxModel(new String[] {
"a", "b", "c" }));
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
this.setLayout(layout);
layout.setHorizontalGroup(layout.createParallelGroup(
javax.swing.GroupLayout.Alignment.LEADING).addGroup(
layout.createSequentialGroup().addComponent(cbCategory,
javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE)));
layout.setVerticalGroup(layout.createParallelGroup(
javax.swing.GroupLayout.Alignment.LEADING).addGroup(
layout.createSequentialGroup().addComponent(cbCategory)
));
}
public static void main(String[] args) …Run Code Online (Sandbox Code Playgroud) 我有一个JComboBox,我有一个听众附加它.
现在,每当用户从下拉列表中"选择"事件时,即使他们刚刚重新选择之前选择的值,事件也会触发.
如果组合框的选定值与选择之前的值不同,有没有办法只触发事件?
我想我可以将组合框的值存储在不同的字段中,并在每次事件触发时进行比较,这看起来有点矫枉过正.我有20个左右这样的组合框.我宁愿不再有20个变量来存储值,所以事件不会触发.
一定有更好的方法.
谢谢您的帮助!
我尝试制作一个没有箭头按钮的可编辑JComboBox.(它将显示其下拉列表,具体取决于用户在其编辑器中输入的内容)
到目前为止,箭头按钮不可见,但仍然可以点击!它仍然会在点击时显示列表.
public class MyComboBox<E> extends JComboBox<E> {
public MyComboBox(E[] list) {
super(list);
this.setEditable(true);
setUI(new BasicComboBoxUI() {
@Override
protected JButton createArrowButton() {
return new JButton() {
@Override
public int getWidth() {
return 0;
}
};
}
});
}
}
Run Code Online (Sandbox Code Playgroud)
有没有办法禁用它?
我有一个Jtable,我在其中添加了JComobox.
TableColumn sportColumn = jTable1.getColumnModel().getColumn(2);
comboBox = new JComboBox();
comboBox.addItem("Snowboarding");
comboBox.addItem("Rowing");
comboBox.addItem("Chasing toddlers");
comboBox.addItem("Speed reading");
comboBox.addItem("Teaching high school");
comboBox.addItem("None");
sportColumn.setCellEditor(new DefaultCellEditor(comboBox));
Run Code Online (Sandbox Code Playgroud)
我添加了一个像这样的jtable鼠标事件.
private void jTable1MouseClicked(java.awt.event.MouseEvent evt) {
// TODO add your handling code here:
try {
int row = jTable1.rowAtPoint(evt.getPoint());
int col = jTable1.columnAtPoint(evt.getPoint());
System.out.println("Row" + row + "Column" + col);
} catch (Exception e) {
e.printStackTrace();
}
}
Run Code Online (Sandbox Code Playgroud)
我正在把行和列放到正确的位置.
但当我点击我添加Jcombobox的单元格时,它没有给出该行和列的放置.仍然我在组合框点击事件中调用了表格的clickevent但是它给了所有时间第0行甚至第0列这里的屏幕截图.

所以我怎么解决它所以我可以有那个行和列?