Topic: 如何把焦点定位到JTable的某行?

  Print this page

1.如何把焦点定位到JTable的某行? Copy to clipboard
Posted by: shsen
Posted on: 2003-04-25 17:11

比如一点击查询按钮,表更新显示查询结果,然后第一行自动选中。

2.Re:如何把焦点定位到JTable的某行? [Re: shsen] Copy to clipboard
Posted by: film2000
Posted on: 2003-04-25 17:20

table.getSelectionModel().setSelectionInterval(0,0);

3.Re:如何把焦点定位到JTable的某行? [Re: shsen] Copy to clipboard
Posted by: shsen
Posted on: 2003-04-27 17:17

不行啊

4.Re:如何把焦点定位到JTable的某行? [Re: shsen] Copy to clipboard
Posted by: film2000
Posted on: 2003-04-27 17:23

你的table必须很够选择整行,在该table不可编辑的情况下会选择整行.

5.Re:如何把焦点定位到JTable的某行? [Re: shsen] Copy to clipboard
Posted by: shsen
Posted on: 2003-04-27 18:45

我的table就是不可编辑的,而且用鼠标点击可以的。我是通过一个model创建table的。

6.Re:如何把焦点定位到JTable的某行? [Re: shsen] Copy to clipboard
Posted by: film2000
Posted on: 2003-04-28 09:24

其实table即使可编辑也可以选中一行.

import javax.swing.*;

public class Test
{
  public static void main(String[] args)
  {
    JFrame f = new JFrame();
    JTable table = new JTable(new NoEditableModel(new String[]{"a","b","c",},10));
    //JTable table = new JTable(new String[10][3],new String[]{"a","b","c",});
    f.getContentPane().add(new JScrollPane(table));
    table.getSelectionModel().setSelectionInterval(0,0);
    f.setSize(300,400);
    f.show();
  }
}

class NoEditableModel extends javax.swing.table.DefaultTableModel
{
public boolean isCellEditable(int row, int column)
{
return false;
}

public NoEditableModel(Object[] columns,int row)
{
super(columns,row);
}
}

7.Re:如何把焦点定位到JTable的某行? [Re: shsen] Copy to clipboard
Posted by: shsen
Posted on: 2003-04-28 10:40

还是不行,而且我把代码几乎改成和你一样,结果反而不能显示记录了。

8.Re:如何把焦点定位到JTable的某行? [Re: shsen] Copy to clipboard
Posted by: film2000
Posted on: 2003-04-28 14:27

1.试试加入以下代码:
table.setRowSelectionAllowed(true);

2.我的代码在你的机器上运行的效果如何?
如果不行,建议你使用高版本的JDK试试看!
我的是jdk1.4

9.Re:如何把焦点定位到JTable的某行? [Re: shsen] Copy to clipboard
Posted by: shsen
Posted on: 2003-04-30 14:44

还是不行。我用的就是jdk141-02

10.Re:如何把焦点定位到JTable的某行? [Re: shsen] Copy to clipboard
Posted by: film2000
Posted on: 2003-04-30 15:56

把代码贴出来看看!

11.Re:如何把焦点定位到JTable的某行? [Re: shsen] Copy to clipboard
Posted by: shsen
Posted on: 2003-04-30 16:25

myTableModel m = new myTableModel(); //表格模型变量

    toolBarInt.buttonNegotiateQuery.addActionListener(new ActionListener()
    {
      public void actionPerformed(ActionEvent event)
      {
      //创建表格
      m.setDataVector(rows,columnHeads);
      //m = new myTableModel(rows,columnHeads);
      table = new JTableMusic;
      scrollpaneTable = new JScrollPane( table );
        currentrow = -1;

        //默认选中第一行,暂时无法实现
        //currentrow = 0;
        //table.setRowSelectionInterval(0,0);
        //table.getSelectionModel().setSelectionInterval(0,0);
      }
    });

12.Re:如何把焦点定位到JTable的某行? [Re: shsen] Copy to clipboard
Posted by: shsen
Posted on: 2003-04-30 16:28

//重载表格模式,使其不可编辑
  class myTableModel extends DefaultTableModel
  {
    public boolean isCellEditable(int row,int col)
    {
      return false;
    }

    /*
    public myTableModel(Vector data,Vector colname)
    {
      super(data,colname);
    }
    */
  }

13.Re:如何把焦点定位到JTable的某行? [Re: shsen] Copy to clipboard
Posted by: film2000
Posted on: 2003-04-30 19:22

代码有问题,你如何scrollpaneTable = new JScrollPane( table );
加入界面中。
请修改你的例子以便能够执行。


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923