c语言中null的arrow是无效的吗,JComboBox中的空值停止箭頭鍵使用

The code below has a bug. Upon loading the JFrame, press tab to focus on the JComboBox, and try pressing the down key. It doesn't do anything.

下面的代碼有一個錯誤。加載JFrame后,按Tab鍵聚焦JComboBox,然后嘗試按下向下鍵。它沒有做任何事情。

Inserting Null at position 0 causes this. However, I would still like to be able to select Null. I don't want to force the user to pick an option.

在位置0處插入Null會導致這種情況。但是,我仍然希望能夠選擇Null。我不想強迫用戶選擇一個選項。

package kobalt.test.colin;

import java.awt.*;

import javax.swing.*;

public class ColinTest extends JFrame {

private JTextField mTextField;

private JComboBox mComboBox;

public ColinTest(){

setLayout(new BorderLayout());

mTextField = new JTextField("Something");

mComboBox = new JComboBox(new String[]{"One", "Two"});

mComboBox.insertItemAt(null, 0); //this line causes the bug

add(mTextField, "North");

add(mComboBox, "South");

pack();

setVisible(true);

}

public static void main(String[] argv) {

new ColinTest();

}

}

Is there something I can override in JComboBox to fix this behaviour?

我可以在JComboBox中覆蓋一些東西來修復這種行為嗎?

I don't really fancy inserting an empty String at position 0 as I'll have to deal with that everywhere.

我真的不想在0號位置插入一個空字符串,因為我必須在任何地方處理它。

Using a Wrapping object may be an option, but I'd rather extend and then override something in JComboBox.

使用Wrapping對象可能是一個選項,但我寧願擴展然后覆蓋JComboBox中的某些東西。

2 个解决方案

#1

2

Null objects do not play nicely in a JComboBox. For example, the combo box's getSelectedIndex method, which is fired when you select an item, will return -1 if the object is null. There may also be other methods which perform null checks and may return incorrect results.

空對象在JComboBox中不能很好地播放。例如,組合框的getSelectedIndex方法(在選擇項時觸發)將返回-1(如果對象為null)。可能還有其他方法執行空檢查並可能返回不正確的結果。

But you can try overriding the getSelectedIndex so that it returns 0 instead of -1 if the object is null. Also override selectedItemChanged so that it doesn't check for nulls. The following seems to work, but there may be other methods which need to be overridden too:

但您可以嘗試重寫getSelectedIndex,以便在對象為null時返回0而不是-1。還要覆蓋selectedItemChanged,以便它不檢查空值。以下似乎有效,但可能還有其他方法需要重寫:

JComboBox mComboBox = new JComboBox(new String[]{"One", "Two"}){

@Override

public int getSelectedIndex() {

Object sObject = dataModel.getSelectedItem();

int i,c;

Object obj;

if(sObject==null){

return 0;

}

for ( i=0,c=dataModel.getSize();i

obj = dataModel.getElementAt(i);

if ( obj != null && obj.equals(sObject) )

return i;

}

return -1;

}

@Override

protected void selectedItemChanged() {

fireItemStateChanged(new ItemEvent(this, ItemEvent.ITEM_STATE_CHANGED,

selectedItemReminder,

ItemEvent.DESELECTED));

selectedItemReminder = dataModel.getSelectedItem();

fireItemStateChanged(new ItemEvent(this, ItemEvent.ITEM_STATE_CHANGED,

selectedItemReminder,

ItemEvent.SELECTED));

}

};

However, instead of doing the above, I would recommend using a wrapper object. For example:

但是,我建議使用包裝器對象,而不是執行上述操作。例如:

class StringWrapper{

final String s;

public StringWrapper(String s){

this.s=s;

}

@Override

public String toString() {

return s;

}

}

JComboBox cb = new JComboBox(new StringWrapper[]{

new StringWrapper("one"),

new StringWrapper("two"),

new StringWrapper("three")});

cb.insertItemAt(new StringWrapper(null), 0);

#2

1

Creation of combobox in such a way creates DefaultComboBoxModel which is based on Vector. So nulls can't be inserted. You can try to implement your ComboBoxModel with nulls support.

以這種方式創建組合框會創建基於Vector的DefaultComboBoxModel。因此無法插入空值。您可以嘗試使用nulls支持實現ComboBoxModel。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值