首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >JTable行选择

问JTable行选择
EN

Stack Overflow用户
提问于 2011-02-18 15:42:04
回答 3查看 27.4K关注 0票数 4

当我单击JTable上的行时,我需要选择一行。默认行为是当按下鼠标时,该行被选中。如何更改此行为?我的期望是::

鼠标按下-->鼠标释放==> selected

鼠标按下-->鼠标拖动-- >鼠标释放==>未选中

鼠标单击选中的==>行

当鼠标被拖动时,我想做一些其他的事情,但不想改变在该操作上的前一行选择。

EN

回答 3

Stack Overflow用户

发布于 2011-02-18 15:57:14

代码语言:javascript
复制
import java.awt.event.*;
import javax.swing.*;

/**
 *
 * @author Jigar
 */
public class JTableDemo  extends MouseAdapter   {
int selection;


    public static void main(String[] args) throws Exception
    {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        String[] headers = {"A", "B", "C"};
        Object[][] data = {{1, 2, 3}, {4, 5, 6}};
        JTable table = new JTable(data, headers);
        JScrollPane scroll = new JScrollPane();
        scroll.setViewportView(table);
        frame.add(scroll);
        frame.pack();
        frame.setVisible(true);
        table.addMouseListener(new JTableDemo());
        scroll.addMouseListener(new JTableDemo());
    }

    @Override
    public void mousePressed(MouseEvent e)
    {
        JTable jtable = (JTable) e.getSource();
        selection= jtable.getSelectedRow();
        jtable.clearSelection();
    }
    @Override
    public void mouseReleased(MouseEvent e){
        JTable jtable = (JTable) e.getSource();
        //now you need to select the row here check below link
    }



}
票数 6
EN

Stack Overflow用户

发布于 2011-10-15 08:52:30

我并不觉得这很容易。我试图在其中突出显示行的表不是当前活动的组件,因此您需要类似以下内容:

代码语言:javascript
复制
// get the selection model
ListSelectionModel tableSelectionModel = table.getSelectionModel();

// set a selection interval (in this case the first row)
tableSelectionModel.setSelectionInterval(0, 0);

// update the selection model
table.setSelectionModel(tableSelectionModel);

// repaint the table
table.repaint();
票数 2
EN

Stack Overflow用户

发布于 2013-03-20 19:15:58

它是"stange“,但对我来说是有效的:

代码语言:javascript
复制
table.setDragEnabled(true); 
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5038781

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档