当前位置: 代码迷 >> Java相关 >> 最后问个swing的问题,看看有没有人回答
  详细解决方案

最后问个swing的问题,看看有没有人回答

热度:315   发布时间:2007-10-23 15:26:09.0
最后问个swing的问题,看看有没有人回答

谁能告诉我被注释掉的那四行的作用是什么?
这个代码的作用是让一个表可以行选中,列选中,单元格选中,等等。。。
public class TableSelectionTest {
public static void main(String[] args){
JFrame frame = new TableSelectionFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}

class TableSelectionFrame extends JFrame{
public TableSelectionFrame(){
setTitle("TableSelectionTest");
setSize(DEFAULT_WIDTH,DEFAULT_HEIGHT);
model = new DefaultTableModel(10,10);

for(int i = 0; i < model.getRowCount(); i++){
for(int j = 0; j<model.getColumnCount(); j++){
model.setValueAt((i+1)*(j+1),i,j);
}
}

table = new JTable(model);

add(new JScrollPane(table),BorderLayout.CENTER);

removedColumns = new ArrayList<TableColumn>();

JMenuBar menuBar = new JMenuBar();
setJMenuBar(menuBar);

JMenu selectionMenu = new JMenu("Selection");
menuBar.add(selectionMenu);

final JCheckBoxMenuItem rowsItem = new JCheckBoxMenuItem("Rows");
final JCheckBoxMenuItem columnsItem = new JCheckBoxMenuItem("Columns");
final JCheckBoxMenuItem cellsItem = new JCheckBoxMenuItem("Cells");

rowsItem.setSelected(table.getRowSelectionAllowed());
columnsItem.setSelected(table.getColumnSelectionAllowed());
cellsItem.setSelected(table.getCellSelectionEnabled());

rowsItem.addActionListener(new
ActionListener(){
public void actionPerformed(ActionEvent event){
table.clearSelection();
table.setRowSelectionAllowed(rowsItem.isSelected());
//cellsItem.setSelected(table.getCellSelectionEnabled());
}
});
selectionMenu.add(rowsItem);

columnsItem.addActionListener(new
ActionListener(){
public void actionPerformed(ActionEvent event){
table.clearSelection();
table.setColumnSelectionAllowed(columnsItem.isSelected());
//cellsItem.setSelected(table.getCellSelectionEnabled());
}
});
selectionMenu.add(columnsItem);

cellsItem.addActionListener(new
ActionListener(){
public void actionPerformed(ActionEvent event){
table.clearSelection();
table.setCellSelectionEnabled(cellsItem.isSelected());
//rowsItem.setSelected(table.getRowSelectionAllowed());
//columnsItem.setSelected(table.getColumnSelectionAllowed());
}
});
selectionMenu.add(cellsItem);

JMenu tableMenu = new JMenu("Edit");
menuBar.add(tableMenu);

JMenuItem hideColumnsItem = new JMenuItem("Hide Columns");
hideColumnsItem.addActionListener(new
ActionListener(){
public void actionPerformed(ActionEvent event){
int[] selected = table.getSelectedColumns();
TableColumnModel columnModel = table.getColumnModel();

for(int i = selected.length-1;i >=0;i--){
TableColumn column = columnModel.getColumn(selected[i]);
table.removeColumn(column);
removedColumns.add(column);
}
}
});
tableMenu.add(hideColumnsItem);

JMenuItem showColumnsItem = new JMenuItem("Show Columns");
showColumnsItem.addActionListener(new
ActionListener(){
public void actionPerformed(ActionEvent event){
for(TableColumn tc:removedColumns)
table.addColumn(tc);
removedColumns.clear();
}
});
tableMenu.add(showColumnsItem);

JMenuItem addRowItem = new JMenuItem("Add Row");
addRowItem.addActionListener(new
ActionListener(){
public void actionPerformed(ActionEvent event){
Integer[] newCells = new Integer[model.getColumnCount()];
for(int i = 0;i<newCells.length;i++)
newCells[i] = (i+1)*(model.getRowCount()+1);
model.addRow(newCells);
}
});
tableMenu.add(addRowItem);

JMenuItem removeRowsItem = new JMenuItem("Remove Rows");
removeRowsItem.addActionListener(new
ActionListener(){
public void actionPerformed(ActionEvent event){
int[] selected = table.getSelectedRows();
for(int i = selected.length-1;i>=0;i--)
model.removeRow(selected[i]);
}
});
tableMenu.add(removeRowsItem);

JMenuItem clearCellsItem = new JMenuItem("Clear Cells");
clearCellsItem.addActionListener(new
ActionListener(){
public void actionPerformed(ActionEvent event){
for(int i = 0;i<table.getRowCount();i++)
for(int j = 0;j<table.getColumnCount();j++)
if(table.isCellSelected(i,j))
table.setValueAt(0,i,j);
}
});
tableMenu.add(clearCellsItem);

}

private DefaultTableModel model;
private JTable table;
private ArrayList<TableColumn> removedColumns;

private static final int DEFAULT_WIDTH = 400;
private static final int DEFAULT_HEIGHT = 300;
}

搜索更多相关的解决方案: swing  

----------------解决方案--------------------------------------------------------
兄弟,有很多类包你没有导入,怎么用啊
----------------解决方案--------------------------------------------------------
JTable的getCellSelectionEnabled()在JDK API文档中的描述:
public boolean getCellSelectionEnabled()
如果同时启用了行选择模型和列选择模型,则返回 true。
等效于 getRowSelectionAllowed() && getColumnSelectionAllowed()
JCheckBoxMenuItem 中的setSelected()描述如下:
public void setSelected(boolean b)
设置按钮的状态.注意,此方法不会触发 actionEvent.调用 doClick 以执行程序上的动作更改。
如果要选择按钮,则该参数设为 true,否则为 false;
大概的意思就是如果table启用了行选择模型或列选择模型之类的,
就把JCheckBoxMenuItem对应的菜单按钮设成选中状态之类的吧.............
----------------解决方案--------------------------------------------------------