JAVA,怎么获取JTable上加的JRadioButton状态??
就是右边的上和下,我想获取各选了哪个学期的课程。。String[]Term={"上","下"};table.getColumn("学期").setCellRenderer...
就是右边的上和下,我想获取各选了哪个学期的课程。。
String[] Term={"上","下"};
table.getColumn("学期").setCellRenderer(new RadioButtonRenderer(Term));
table.getColumn("学期").setCellEditor(new RadioButtonEditor(new JCheckBox(), new RadioButtonPanel(Term)));
public class RadioButtonEditor extends DefaultCellEditor {
RadioButtonPanel panel;
JRadioButton[] buttons;
public RadioButtonEditor(JCheckBox checkBox, RadioButtonPanel panel) {
super(checkBox);
this.panel = panel;
ButtonGroup buttonGroup = new ButtonGroup();
buttons = panel.getButtons();
for (int i = 0; i < buttons.length; i++) {
buttonGroup.add(buttons[i]);
}
}
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());
}
}
表格下方有个“提交”按钮,我想在最后选好后按提交的时候 先获取课程的学期 展开
String[] Term={"上","下"};
table.getColumn("学期").setCellRenderer(new RadioButtonRenderer(Term));
table.getColumn("学期").setCellEditor(new RadioButtonEditor(new JCheckBox(), new RadioButtonPanel(Term)));
public class RadioButtonEditor extends DefaultCellEditor {
RadioButtonPanel panel;
JRadioButton[] buttons;
public RadioButtonEditor(JCheckBox checkBox, RadioButtonPanel panel) {
super(checkBox);
this.panel = panel;
ButtonGroup buttonGroup = new ButtonGroup();
buttons = panel.getButtons();
for (int i = 0; i < buttons.length; i++) {
buttonGroup.add(buttons[i]);
}
}
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());
}
}
表格下方有个“提交”按钮,我想在最后选好后按提交的时候 先获取课程的学期 展开
展开全部
额。。jtable里面放cellrenderer没用过。。
你可以试一下直接访问renderer render的那个object,就是那个panel还是什么的
你可以试一下直接访问renderer render的那个object,就是那个panel还是什么的
追答
查了一下,你在表里面放的应该是boolean然后再用renderer显示那个boolean
A JTable isn't supposed to contain components. It's supposed to contain data (Strings, Integers, Booleans, Dates, etc.). The renderer's goal is to use a component (the same instance for multiple cells) to transform this data into a visual thing (a label, a radio button, an icon, ...). The editor's goal is to be able to display the data in an editable component, to accept a new value, and to change the value of the data with the data entered by the end user.
Don't store components in a JTable. Configure renderers and/or editors to render/edit the data if the default doesn't suit your needs.
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询