当前位置: 代码迷 >> J2SE >> 小疑点··请君留步
  详细解决方案

小疑点··请君留步

热度:219   发布时间:2016-04-24 12:40:15.0
小问题··请君留步
Object[][] cells = {  
  {Boolean.FALSE, "必选","",""},  
  {Boolean.FALSE, "可选","",""},  
  {Boolean.FALSE, "必选","",""},  
  {Boolean.FALSE, "可选","",""},  
  {Boolean.FALSE, "必选","",""},};  
  String[] columnNames = {"选择","重要级","文件名","说明"};  


  DefaultTableModel model = new DefaultTableModel(cells, columnNames) {  
  public Class getColumnClass(int columnIndex)  
  {  
  return columnIndex == 0 ? Boolean.class : String.class;  
  }  
  };  
   
  jTablesecond = new JTable(model);
  JScrollPane sp = new JScrollPane(jTablesecond);
   
  //设置Table列宽
  TableColumnModel tcm = jTablesecond.getColumnModel();
  int columnCount = jTablesecond.getColumnCount();
  TableColumn[] tc = new TableColumn[columnCount];
  tc[0] = tcm.getColumn(0);
  tc[0].setPreferredWidth(50);
  tc[1] = tcm.getColumn(1);
  tc[1].setPreferredWidth(75);
  tc[2] = tcm.getColumn(2);
  tc[2].setPreferredWidth(75);
  tc[3] = tcm.getColumn(3);
  tc[3].setPreferredWidth(240);



  jTablesecond.getTableHeader().setReorderingAllowed(false); //设置头不可变
  jTablesecond.getTableHeader().setResizingAllowed(false); //设置列宽不可变

  jTablesecond.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);//设置为不自动调整大小
  jTablesecond.setBackground(Color.white);
  jPanelcenter.add(new JScrollPane(jTablesecond)); //显示表头



我在JTable里面添加了一个JChecnkBox列表·请问我应该如何获得这个JCheckBox的是否被选中 ,还用被选中了··我应该获得该行所有列的值···
谢谢··分不多····
请拿去···

------解决方案--------------------
不晓得行不行哈,希望能对你有帮助
Java code
package test;import java.applet.Applet;import java.awt.Color;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.JButton;import javax.swing.JScrollPane;import javax.swing.JTable;import javax.swing.table.DefaultTableModel;import javax.swing.table.TableColumn;import javax.swing.table.TableColumnModel;import javax.swing.table.TableModel;public class Test extends Applet implements ActionListener{    JTable jTablesecond;    public Test() {        Object[][] cells = { { Boolean.FALSE, "必选", "", "" },                { Boolean.FALSE, "可选", "", "" },                { Boolean.FALSE, "必选", "", "" },                { Boolean.FALSE, "可选", "", "" },                { Boolean.FALSE, "必选", "", "" }, };        String[] columnNames = { "选择", "重要级", "文件名", "说明" };        DefaultTableModel model = new DefaultTableModel(cells, columnNames){            public Class getColumnClass(int columnIndex) {                return columnIndex == 0 ? Boolean.class : String.class;                }        };        jTablesecond = new JTable(model);        JScrollPane sp = new JScrollPane(jTablesecond);        // 设置Table列宽        TableColumnModel tcm = jTablesecond.getColumnModel();        int columnCount = jTablesecond.getColumnCount();        TableColumn[] tc = new TableColumn[columnCount];        tc[0] = tcm.getColumn(0);        tc[0].setPreferredWidth(50);        tc[1] = tcm.getColumn(1);        tc[1].setPreferredWidth(75);        tc[2] = tcm.getColumn(2);        tc[2].setPreferredWidth(75);        tc[3] = tcm.getColumn(3);        tc[3].setPreferredWidth(240);        jTablesecond.getTableHeader().setReorderingAllowed(false); // 设置头不可变        jTablesecond.getTableHeader().setResizingAllowed(false); // 设置列宽不可变        jTablesecond.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);// 设置为不自动调整大小        jTablesecond.setBackground(Color.white);        add(new JScrollPane(jTablesecond)); // 显示表头        JButton button = new JButton("aa");        button.addActionListener(this);        add(button);    }    public void actionPerformed(ActionEvent e) {        TableModel model = jTablesecond.getModel();        int count = model.getRowCount();        for(int i = 0 ; i < count ; i++){            boolean checked = (Boolean)model.getValueAt(i, 0);            if(checked){                System.out.println(i);            }        }    }}
  相关解决方案