给小妹看看这是什么问题? 当我选择选项后,确认RadioButton中的数据添加到一个树节点上 现在问题是:当你选好,确认后 每次最后一个数据添加不上 但是当你选完后,在JTable的空白处单击一下 全部就能添加上 ,这是怎么回事呢? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 用得数组来接收你选中的值吗??看你讲的情况,我觉得是不是你在循环时让他的长度-1了啊,所以少了一个,或者是list,但size()减了一个 应该不是数组方面的问题,就是在选择后,必须在JTable的空白处单击一下,然后按“确认生成”,我选择的 都能添加上 否则最后一个就添加不上 逻辑问题吧 这种问题DEBUG一下就好了 如果你指点1个选项,然后点确认,是否会出现同样问题?在点击Radio的时候,你是怎么判断选了哪个?找到那段代码,调试 循环都好着呢 感觉最后一个那个状态 在JTable 空白处单击一下 全部就添加上了 应该是RadioButton事件用的不对,你可以换个别的类似事件再试试. 你是怎么遍历的呢?看你的描述,好像是失去焦点的时候才把失去焦点的radio的值保存到一个集合中,然后确认的时候,把集合的元素追加到树中(因为可能是如果是用选择事件处理的话,可能没法判断多次选择,所以可能你用失去焦点事件来处理的)你应该是在确认事件中遍历jtable,把选中的radio值保存到树中 class RadioButtonPanel extends JPanel { JRadioButton[] buttons; RadioButtonPanel(String[] str) { setLayout(new BoxLayout(this, BoxLayout.X_AXIS)); this.setBackground(Color.WHITE); buttons = new JRadioButton[str.length]; for (int i = 0; i < str.length; i++) { buttons[i] = new JRadioButton(str[i]); buttons[i].setFocusPainted(false); buttons[i].setBackground(Color.white); add(buttons[i]); } } public void setSelectedIndex(int index) { for (int i = 0; i < buttons.length; i++) { buttons[i].setSelected(i == index); } } public int getSelectedIndex() { for (int i = 0; i < buttons.length; i++) { if (buttons[i].isSelected()) { return i; } } return -1; } public JRadioButton[] getButtons() { return buttons; } } class RadioButtonRenderer extends RadioButtonPanel implements TableCellRenderer { RadioButtonRenderer(String[] strs) { super(strs); } public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) { if (value instanceof Integer) { //setSelectedIndex(((Integer) value).intValue()); /*val=Integer.parseInt(s)*/this.setSelectedIndex(((Integer)value).intValue()); } return this; } } class RadioButtonEditor extends DefaultCellEditor implements ItemListener { RadioButtonPanel panel; public RadioButtonEditor(JCheckBox checkBox, RadioButtonPanel panel) { super(checkBox); this.panel = panel; ButtonGroup buttonGroup = new ButtonGroup(); JRadioButton[] buttons = panel.getButtons(); for (int i = 0; i < buttons.length; i++) { buttonGroup.add(buttons[i]); buttons[i].addItemListener(this); } } public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) { if (value instanceof Integer) { panel.setSelectedIndex(((Integer) value).intValue()); } return panel; } public Object getCellEditorValue() { return new Integer(panel.getSelectedIndex()); } public void itemStateChanged(ItemEvent e) { super.fireEditingStopped(); } } 应该是在确认事件时没有停止表格编辑器,应该在确认事件中先进行下面的判断再到表格中取数据.if(table.isEditing()){ table.getCellEditor().stopCellEditing();} 我很费解 英语四级 一直争论不休的话题 高分求一个算法 乱吗的问题????????? JTree与本地文件系统同步问题 想阅读LUCENE的源代码 则么把这个项目添加到ECLIPSE项目中 急!!!!!! 哪位老大能帮忙找找 getCustomDatum 的替代方法。 哪里有比较好的gif集成工具可以用来做button上的图片? 线程启动等待问题 date如何获取年月日 请教如何用正则表达式表示这个字符串
循环都好着呢 感觉最后一个那个状态 在JTable 空白处单击一下 全部就添加上了
看你的描述,好像是失去焦点的时候才把失去焦点的radio的值保存到一个集合中,然后确认的时候,把集合的元素追加到树中(因为可能是如果是用选择事件处理的话,可能没法判断多次选择,所以可能你用失去焦点事件来处理的)
你应该是在确认事件中遍历jtable,把选中的radio值保存到树中
class RadioButtonPanel extends JPanel {
JRadioButton[] buttons; RadioButtonPanel(String[] str) {
setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
this.setBackground(Color.WHITE);
buttons = new JRadioButton[str.length];
for (int i = 0; i < str.length; i++) {
buttons[i] = new JRadioButton(str[i]);
buttons[i].setFocusPainted(false);
buttons[i].setBackground(Color.white);
add(buttons[i]);
}
} public void setSelectedIndex(int index) {
for (int i = 0; i < buttons.length; i++) {
buttons[i].setSelected(i == index);
}
} public int getSelectedIndex() {
for (int i = 0; i < buttons.length; i++) {
if (buttons[i].isSelected()) {
return i;
}
}
return -1;
} public JRadioButton[] getButtons() {
return buttons;
}
} class RadioButtonRenderer extends RadioButtonPanel implements
TableCellRenderer {
RadioButtonRenderer(String[] strs) {
super(strs);
}
public Component getTableCellRendererComponent(JTable table,
Object value, boolean isSelected, boolean hasFocus, int row,
int column) {
if (value instanceof Integer) {
//setSelectedIndex(((Integer) value).intValue());
/*val=Integer.parseInt(s)*/this.setSelectedIndex(((Integer)value).intValue());
}
return this;
}
} class RadioButtonEditor extends DefaultCellEditor implements ItemListener {
RadioButtonPanel panel; public RadioButtonEditor(JCheckBox checkBox, RadioButtonPanel panel) {
super(checkBox);
this.panel = panel;
ButtonGroup buttonGroup = new ButtonGroup();
JRadioButton[] buttons = panel.getButtons();
for (int i = 0; i < buttons.length; i++) {
buttonGroup.add(buttons[i]);
buttons[i].addItemListener(this);
}
} public Component getTableCellEditorComponent(JTable table,
Object value, boolean isSelected, int row, int column) {
if (value instanceof Integer) {
panel.setSelectedIndex(((Integer) value).intValue());
}
return panel;
} public Object getCellEditorValue() {
return new Integer(panel.getSelectedIndex());
} public void itemStateChanged(ItemEvent e) {
super.fireEditingStopped();
}
}
if(table.isEditing()){
table.getCellEditor().stopCellEditing();
}