Java的JTable中添加JCheckBox,点击事件处理问题,求高手指点迷津
classProfileTableCellRendererextendsJCheckBoximplementsTableCellRenderer{@Overridepub...
class ProfileTableCellRenderer extends JCheckBox implements TableCellRenderer{
@Override
public Component getTableCellRendererComponent(JTable table,Object value, boolean isSelected, boolean hasFocus, int row,int column)
{
Boolean b = (Boolean) value;
this.setSelected(b.booleanValue());
if(hasFocus==true)
{
if(b == false && isSelected == false)
{
//某事件处理,
}
else if(b == true && isSelected == true)
{
//重点,此时也有事件处理,但是每次点击都会执行该段代码,有什么方法做到只有执行一次
} 展开
@Override
public Component getTableCellRendererComponent(JTable table,Object value, boolean isSelected, boolean hasFocus, int row,int column)
{
Boolean b = (Boolean) value;
this.setSelected(b.booleanValue());
if(hasFocus==true)
{
if(b == false && isSelected == false)
{
//某事件处理,
}
else if(b == true && isSelected == true)
{
//重点,此时也有事件处理,但是每次点击都会执行该段代码,有什么方法做到只有执行一次
} 展开
2个回答
展开全部
package p1;
import java.awt.Dimension;
import java.awt.GridLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
public class TableDemo extends JPanel
{
private boolean DEBUG = true;
public TableDemo ()
{
super (new GridLayout (1, 0));
JTable table = new JTable (new MyTableModel ());
table.setPreferredScrollableViewportSize (new Dimension (500, 70));
table.setFillsViewportHeight (true);
JScrollPane scrollPane = new JScrollPane (table);
add (scrollPane);
}
class MyTableModel extends AbstractTableModel
{
private String[] columnNames = { "First Name", "Last Name", "Sport", "# of Years", "Vegetarian" };
private Object[][] data = { { "Kathy", "Smith", "Snowboarding", new Integer (5), new Boolean (false) },
{ "John", "Doe", "Rowing", new Integer (3), new Boolean (true) },
{ "Sue", "Black", "Knitting", new Integer (2), new Boolean (false) },
{ "Jane", "White", "Speed reading", new Integer (20), new Boolean (true) },
{ "Joe", "Brown", "Pool", new Integer (10), new Boolean (false) } };
public int getColumnCount ()
{
return columnNames.length;
}
public int getRowCount ()
{
return data.length;
}
public String getColumnName ( int col )
{
return columnNames[col];
}
public Object getValueAt ( int row, int col )
{
return data[row][col];
}
public Class getColumnClass ( int c )
{
return getValueAt (0, c).getClass ();
}
public boolean isCellEditable ( int row, int col )
{
if (col < 2)
{
return false;
}
else
{
return true;
}
}
public void setValueAt ( Object value, int row, int col )
{
if (DEBUG)
{
System.out.println ("Setting value at " + row + "," + col + " to " + value + " (an instance of "
+ value.getClass () + ")");
}
data[row][col] = value;
fireTableCellUpdated (row, col);
if (DEBUG)
{
System.out.println ("New value of data:");
printDebugData ();
}
}
private void printDebugData ()
{
int numRows = getRowCount ();
int numCols = getColumnCount ();
for ( int i = 0; i < numRows; i++ )
{
System.out.print (" row " + i + ":");
for ( int j = 0; j < numCols; j++ )
{
System.out.print (" " + data[i][j]);
}
System.out.println ();
}
System.out.println ("--------------------------");
}
}
private static void createAndShowGUI ()
{
JFrame frame = new JFrame ("TableDemo");
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
TableDemo newContentPane = new TableDemo ();
newContentPane.setOpaque (true);
frame.setContentPane (newContentPane);
frame.pack ();
frame.setVisible (true);
}
public static void main ( String[] args )
{
javax.swing.SwingUtilities.invokeLater (new Runnable ()
{
public void run ()
{
createAndShowGUI ();
}
});
}
}
2014-11-17
展开全部
你这里面是什么处理?弹出编辑窗体还是什么?
追问
操作数据库的
追答
如果实在没有办法就添加一个bool变量,每次执行else if的时候判断一下。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询