我对java很新,所以请原谅我犯的任何愚蠢错误......
我有一个2列的JTable,其中第2列可以有4种类型:
列类型是动态的,因为它们取决于所接收的数据。
除JCheckBox外,所有类型都可以正常工作。 当我用带有2个选项的JComboBox替换JCheckBox时,它工作正常。 所以我猜JCheckBox的代码存在问题。
问题是:
一个示例项目(尽可能小):
import java.awt.Component;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.table.*;
public class frmCheck extends JApplet
{
JTable mgrdData;
DefaultTableModel mtableModel;
public void init()
{
mtableModel = new DefaultTableModel();
mtableModel.setColumnCount(2);
mtableModel.setRowCount(4);
mgrdData = new JTable(mtableModel);
mgrdData.getSelectionModel().addListSelectionListener(new RowListener());
add(mgrdData);
for (int i=1;i<mtableModel.getRowCount();i++)
{
addCheck(1);
}
}
private class RowListener implements ListSelectionListener
{
public void valueChanged(ListSelectionEvent event)
{
if (event.getValueIsAdjusting()) return;
}
}
private void addCheck(int intCol)
{
mgrdData.getColumnModel().getColumn(intCol).setCellRenderer(new TableCellRenderer()
{
public Component getTableCellRendererComponent(JTable table,Object value,boolean isSelected,boolean isFocused,int row,int col)
{
JCheckBox rendererComponent = new JCheckBox();
String strVal="";
if (value!=null) strVal = value.toString();
if (strVal.equals("1"))
{
rendererComponent.setSelected(true);
} else
{
rendererComponent.setSelected(false);
}
return rendererComponent;
}
});
DefaultCellEditor cellEditor = new DefaultCellEditor(new JCheckBox());
cellEditor.setClickCountToStart(1);
cellEditor.addCellEditorListener(new CellEditorListener()
{
public void editingCanceled(ChangeEvent e) {}
public void editingStopped(ChangeEvent e)
{
JCheckBox checkBox = (JCheckBox)((DefaultCellEditor)e.getSource()).getComponent();
System.out.println("isSelected = " + checkBox.isSelected());
if (checkBox.isSelected())
{
System.out.println("Sent 0");
} else
{
System.out.println("Sent 1");
}
}
});
mgrdData.getColumnModel().getColumn(intCol).setCellEditor(cellEditor);
}
}
我用来查看Applet的html页面是:
<html>
<body>
<applet code="frmCheck.class" width="1016" height="822"></applet>
</body>
</html>
要使它成为可执行文件(应用程序),您可以向其添加以下main()函数(在init()函数之上,并在mtableModel的声明之下)
public static void main(String[] args) {
frmCheck myApplet = new frmCheck(); // define applet of interest
Frame myFrame = new Frame("Applet Holder"); // create frame with title
// Call applet's init method (since Java App does not
// call it as a browser automatically does)
myApplet.init();
// add applet to the frame
myFrame.add(myApplet, BorderLayout.CENTER);
myFrame.pack(); // set window to appropriate size (for its elements)
myFrame.setVisible(true); // usual step to make frame visible
}
当您单击未选中的复选框时,控制台会显示:
isSelected = true
Sent 0
之后,仍然取消选中该复选框 当我再次单击该复选框时(因此第二次),然后控制台显示:
isSelected = false
Sent 1
复选框会在检查一段时间后闪烁,然后再次取消选中 当我再次单击该复选框(因此第三次),然后它的行为就像第一次点击一样
我想要复选框: - 取消选中并单击后发送1 - 选中并点击后发送0
真正的项目有点复杂:通过检查JCheckBox,我向设备发送消息,该设备以其当前状态进行回答,该状态被处理并显示为已检查的JCheckBox。 处理工作正常,因为我通过另一个通道更改设备中的状态,然后JCheckBox完美地显示当前状态
我希望JCheckBox响应1次点击:
好的,这是我到目前为止的代码:
JTable创建如下:
JTable mgrdData;
DefaultTableModel mtableModel;
mtableModel = new DefaultTableModel(null,new String[0]);
mgrdData = new JTable(mtableModel);
mgrdData.getSelectionModel().addListSelectionListener(new RowListener());
mgrdData.setFillsViewportHeight(true);
String[] strHeader = {"Naam","Waarde"};
mtableModel.setColumnIdentifiers(strHeader);
列配置如下:
addReadOnly(0);
// addCombo(1,new String[]{"UIT","AAN"}); //this works fine
addCheck(1);
第0列是只读的,第1列有2个可能的值 其中addCombo(1,new String [] {“UIT”,“AAN”});完美的作品
addReadOnly和addCombo以及addCheck函数如下:
private void addReadOnly(int intCol)
{
JTextField txtField = new JTextField();
txtField.setEditable(false);
DefaultCellEditor cellEditor = new DefaultCellEditor(txtField);
mgrdData.getColumnModel().getColumn(intCol).setCellEditor(cellEditor);
}
private void addCombo(int intCol,final String[] strItems)
{
//add combobox
mgrdData.getColumnModel().getColumn(intCol).setCellRenderer(new TableCellRenderer()
{
public Component getTableCellRendererComponent(JTable table,Object value,boolean isSelected,boolean isFocused,int row,int col)
{
JComboBox rendererComponent = new JComboBox(strItems);
if (value!=null) rendererComponent.setSelectedItem(value.toString());
return rendererComponent;
}
});
DefaultCellEditor cellEditor = new DefaultCellEditor(new JComboBox(strItems));
cellEditor.setClickCountToStart(1);
cellEditor.addCellEditorListener(new CellEditorListener()
{
private boolean blnChanged=false;
public void editingCanceled(ChangeEvent e) {}
public void editingStopped(ChangeEvent e)
{
if (blnChanged==true)
{
JComboBox comboBox = (JComboBox)((DefaultCellEditor)e.getSource()).getComponent();
sendVal(String.valueOf(comboBox.getSelectedIndex()));
blnChanged = false;
} else
{
blnChanged = true;
}
}
});
mgrdData.getColumnModel().getColumn(intCol).setCellEditor(cellEditor);
}
private void addCheck(int intCol)
{
//add checkbox
mgrdData.getColumnModel().getColumn(intCol).setCellRenderer(new TableCellRenderer()
{
public Component getTableCellRendererComponent(JTable table,Object value,boolean isSelected,boolean isFocused,int row,int col)
{
JCheckBox rendererComponent = new JCheckBox();
String strVal="";
if (value!=null) strVal = value.toString();
// if (strVal.equals("AAN"))
if (strVal.equals("1"))
{
rendererComponent.setSelected(true);
} else
{
rendererComponent.setSelected(false);
}
return rendererComponent;
}
});
DefaultCellEditor cellEditor = new DefaultCellEditor(new JCheckBox());
cellEditor.setClickCountToStart(1);
cellEditor.addCellEditorListener(new CellEditorListener()
{
public void editingCanceled(ChangeEvent e) {}
public void editingStopped(ChangeEvent e)
{
JCheckBox checkBox = (JCheckBox)((DefaultCellEditor)e.getSource()).getComponent();
System.out.println("isSelected = " + checkBox.isSelected());
if (checkBox.isSelected())
{
sendVal("0");
System.out.println("Sent 0");
} else
{
sendVal("1");
System.out.println("Sent 1");
}
}
});
mgrdData.getColumnModel().getColumn(intCol).setCellEditor(cellEditor);
}
点击3次后测试结果:
检查JCheckBox,点击一下后得到结果:
isSelected = true
Sent : REG SCH 4 = 0
Sent 0
此后,JCheckBox未经检查
取消选中JCheckBox,点击一下后显示结果:
isSelected = true
Sent : REG SCH 4 = 0
Sent 0
此后JCheckBox仍然未经检查
JCheckBox未经检查但已点击一次,结果在第二次点击后显示:
isSelected = false
Sent : REG SCH 4 = 1
Sent 1
此后检查JCheckBox
我搜索并找到解决方案,以便在您总是需要2次点击时,或者使用固定复选框而不是动态的解决方案,而不是像我这样的情况....
有人可以请光明吗?
答案 0 :(得分:2)
问题在于,当它是布尔值时,你将复选框值视为字符串。
工作代码如下:
Boolean val = new Boolean(false);
if (value != null){
val = (Boolean) value;
}
if (val.booleanValue()) {
rendererComponent.setSelected(true);
}else{
rendererComponent.setSelected(false);
}
return rendererComponent;
我仍在关注它,但我认为这个问题可能是因为JCheckBox
被选中或取决于以下事实:
if (strVal.equals("AAN"))
{
rendererComponent.setSelected(true);
} else
{
rendererComponent.setSelected(false);
}
Dropbox的值不是"AAN"
而不是checkbutton的值吗?
另外,您每次都在创建一个新的JCheckBox。
也许SSCCE可能有用。
答案 1 :(得分:1)
代码的问题在于getTableCellRendererComponent
方法。在这里,您始终使用JCheckBox
创建新组件。因此每次采用新的JCheckBox
并渲染列时都会这样做。
除此之外,无需将列呈现给Boolean
。 JTable
它会自我渲染。您只需要将列类型指定为Boolean.class.
覆盖模型中的getColumnClass
方法(DefaultTableModel,就像我已经显示的那样),并说第1列是布尔值。
import javax.swing.DefaultCellEditor;
import javax.swing.JApplet;
import javax.swing.JCheckBox;
import javax.swing.JTable;
import javax.swing.event.CellEditorListener;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableModel;
public class frmCheck extends JApplet
{
JTable mgrdData;
DefaultTableModel mtableModel;
public void init()
{
mtableModel = new DefaultTableModel() {
@Override
public Class<?> getColumnClass(int arg0) {
if(arg0 == 1) {
return Boolean.class;
} else {
return super.getColumnClass(arg0);
}
}
};
mtableModel.setColumnCount(2);
mtableModel.setRowCount(4);
mgrdData = new JTable(mtableModel);
// mgrdData.getSelectionModel().addListSelectionListener(new RowListener());
add(mgrdData);
for (int i=1;i<mtableModel.getRowCount();i++)
{
addCheck(1);
}
}
private class RowListener implements ListSelectionListener
{
public void valueChanged(ListSelectionEvent event)
{
if (event.getValueIsAdjusting()) return;
}
}
private void addCheck(int intCol)
{
DefaultCellEditor cellEditor = new DefaultCellEditor(new JCheckBox());
mgrdData.getColumnModel().getColumn(1).getCellEditor();
cellEditor.setClickCountToStart(1);
cellEditor.addCellEditorListener(new CellEditorListener()
{
public void editingCanceled(ChangeEvent e) {}
public void editingStopped(ChangeEvent e)
{
JCheckBox checkBox = (JCheckBox)((DefaultCellEditor)e.getSource()).getComponent();
System.out.println("isSelected = " + checkBox.isSelected());
if (checkBox.isSelected())
{
System.out.println("Sent 0");
} else
{
System.out.println("Sent 1");
}
}
});
mgrdData.getColumnModel().getColumn(intCol).setCellEditor(cellEditor);
}
}
P.S:我删除了未使用的代码。