table里单元格怎么获取焦点?

   AWT  JTable选中单元格代码  table.setColumnSelectionInterval(2,2); 
 table.setRowSelectionInterval(2, 2); 

 

package aa;

import java.util.ArrayList; 
import java.util.List; 
import java.util.Vector;

import javax.swing.table.AbstractTableModel; 

public class Table_Model extends AbstractTableModel { 
  
    //创建数据源 
    private List content = null;

    private String[] title_name = { "qqqqqqq", "姓名", "性别", "年龄" };

    public Table_Model() { 
        content = new Vector(); 
    } 
      public boolean isCellEditable(int rowIndex, int columnIndex) { 
        if (columnIndex == 0) { 
            return false; 
        } 
        return true; 
    } 
    //返回 columnIndex 位置的列的名称。 
    public String getColumnName(int col) { 
        return title_name[col]; 
    } 
    //返回该模型中的列数。 
    public int getColumnCount() { 
        return title_name.length; 
    } 
    //返回该模型中的行数。 
    public int getRowCount() { 
        return content.size(); 
    } 
    //返回 columnIndex 和 rowIndex 位置的单元格值 
    public Object getValueAt(int row, int col) { 
        return ((Vector) content.get(row)).get(col); 
    }

    public void addRow(String id,String name, String sex, String age) { 
        Vector v = new Vector(4); 
        v.add(0, id); 
        v.add(1, name); 
        v.add(2, sex); 
        v.add(3, age); 
        content.add(v); 
    } 
}

 

 

 

 

 

package aa;

import java.awt.BorderLayout; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.awt.event.KeyAdapter; 
import java.awt.event.KeyEvent; 
import java.util.ArrayList; 
import java.util.List;

import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.JScrollPane; 
import javax.swing.JTable;

 

public class Testss extends KeyAdapter{

    private JFrame frame = null;

    private JTable table = null;

    private Table_Model model = null;

    private JScrollPane s_pan = null;

    private JPanel pane = null; 
    private JButton button_2 = null, button_3 = null; 
    public Testss() {

        frame = new JFrame("JTableTest"); 
        pane = new JPanel(); 
        
        model = new Table_Model(); 
        table = new JTable(model); 
        s_pan = new JScrollPane(table); 

        table.addKeyListener(this); 
        
        button_2 = new JButton("添加数据"); 
        button_2.addActionListener(new ActionListener() { 
            public void actionPerformed(ActionEvent e) { 
             addData(); 
            } 
            
        }); 
        
        
        button_3 = new JButton("测试数据"); 
        button_3.addActionListener(new ActionListener() { 
            public void actionPerformed(ActionEvent e) { 
               table.requestFocusInWindow();

             table.editCellAt(2, 2, null);

            ///选中单元格代码 
             table.setColumnSelectionInterval(2,2); 
          table.setRowSelectionInterval(2, 2); 
         
            }

          }); 
        
        
        pane.add(button_2); 
        pane.add(button_3); 
        frame.add(s_pan, BorderLayout.CENTER); 
        frame.add(pane, BorderLayout.NORTH); 
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
        frame.setSize(300, 200); 
        frame.setVisible(true); 
    } 
    public static void main(String args[]) { 
        Testss tt=new Testss(); 
        tt.addData(); 
    } 
    private void addData() { 
     //TableModel中的addRow方法 
          model.addRow("海涛","李逵","男", "19");

         model.addRow("海涛","李逵","男", "19"); 
         model.addRow("海涛","李逵","男", "19");

         model.addRow("海涛","李逵","男", "19"); 
    }

}

  

posted @ 2022-04-24 16:48  信铁寒胜  阅读(314)  评论(0编辑  收藏  举报