点击JTable中JButton事件以及showConfirmDialog的问题
public class ButtonDemo extends JFrame{private static Label labname;
private static TextField tename;
private static Button okBut;
private static JTable table;
public ButtonDemo()
{
Object[][] tableData =
{
new Object[]{"李清照" , 29 , "确定"},
new Object[]{"苏格拉底", 56 , "确定"},
new Object[]{"李白", 35 , "确定"},
new Object[]{"弄玉", 18 , "确定"},
new Object[]{"虎头" , 2 , "确定"}
};
Object[] columnTitle = {"案件ID" , "案件名称" , "选择"};
table=new JTable(tableData,columnTitle);
table.setCellSelectionEnabled(true);
ButtclassonColumn buttonColumn=new ButtclassonColumn(table,2);
labname=new Label("请输入您需要检索的案件,如20130326湖北省武汉市盗窃案");
tename=new TextField(50);
okBut=new Button("确定");
}
class ButtclassonColumn extends AbstractCellEditor
implements TableCellRenderer
{
JTable table;
JButton sureButton;
String text;
public ButtclassonColumn(JTable table,int column)
{
super();
this.table=table;
sureButton=new JButton();
//sureButton.setEditable(false);
TableColumnModel columnModel = table.getColumnModel();
columnModel.getColumn(column).setCellRenderer( this );
}
public Component getTableCellRendererComponent(
JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column)
{
sureButton.setText( (value == null) ? "" : value.toString() );
if(isSelected){
int choose = JOptionPane.showConfirmDialog(null,"确定吗","Test",JOptionPane.YES_NO_OPTION);
// this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
if(choose==JOptionPane.YES_OPTION)
{
System.out.println ("Result=Yes");
}
}
return sureButton;
}
public Object getCellEditorValue()
{
return text;
}
}
public static void main(String[] args)
{
ButtonDemo frame=new ButtonDemo();
frame.add(labname);
frame.add(tename);
frame.add(okBut);
frame.add(new JScrollPane(table));
frame.setBounds(300,100,600,500);
frame.setLayout(new FlowLayout());
frame.setVisible(true);
}
}
有几个问题:1.我想让每个单元格都是不可编辑的
2.点击确定之后,程序抛出异常不知道怎么改
补充:Java , Java相关