java获取jtable的路径,Java如何在JTable组件中获取选定的单元格?

以下示例显示如何获取选定的行或选定的列,或如何选择JTable组件中的多个单元格。要侦听选择事件,我们可以JTable通过调用JTable.getSelectionModel().addListSelectionListener()方法将选择侦听器添加到组件。该方法接受实现ListSelectionListener接口的对象。package org.nhooo.example.swing;

import javax.swing.*;

import javax.swing.event.ListSelectionEvent;

import javax.swing.event.ListSelectionListener;

import javax.swing.table.AbstractTableModel;

import java.awt.*;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.util.Arrays;

public class JTableGetSelectedCells extends JPanel {

private JTable table = null;

public JTableGetSelectedCells() {

initializePanel();

}

private void initializePanel() {

setLayout(new BorderLayout());

setPreferredSize(new Dimension(500, 200));

table = new JTable(new PremiereLeagueTableModel());

table.getColumnModel().getColumn(0).setMinWidth(150);

table.getSelectionModel().addListSelectionListener(

new RowColumnListSelectionListener());

table.setFillsViewportHeight(true);

JScrollPane pane = new JScrollPane(table);

JPanel control = new JPanel(new FlowLayout());

final JCheckBox cb1 = new JCheckBox("Row Selection");

final JCheckBox cb2 = new JCheckBox("Columns Selection");

final JCheckBox cb3 = new JCheckBox("Cells Selection");

// 更改行选择允许状态

cb1.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

table.setRowSelectionAllowed(cb1.isSelected());

table.setColumnSelectionAllowed(!cb1.isSelected());

cb2.setSelected(!cb1.isSelected());

}

});

// 更改列选择允许状态

cb2.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

table.setColumnSelectionAllowed(cb2.isSelected());

table.setRowSelectionAllowed(!cb2.isSelected());

cb1.setSelected(!cb2.isSelected());

}

});

// 启用单元格选择

cb3.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

table.setCellSelectionEnabled(cb3.isSelected());

table.setRowSelectionAllowed(cb3.isSelected());

table.setColumnSelectionAllowed(cb3.isSelected());

}

});

control.add(cb1);

control.add(cb2);

control.add(cb3);

add(pane, BorderLayout.CENTER);

add(control, BorderLayout.SOUTH);

}

private class RowColumnListSelectionListener implements ListSelectionListener {

public void valueChanged(ListSelectionEvent e) {

if (table.getRowSelectionAllowed() &&

!table.getColumnSelectionAllowed()) {

int[] rows = table.getSelectedRows();

System.out.println("Selected Rows: " + Arrays.toString(rows));

}

if (table.getColumnSelectionAllowed() &&

!table.getRowSelectionAllowed()) {

int[] cols = table.getSelectedColumns();

System.out.println("Selected Columns: " + Arrays.toString(cols));

} else if (table.getCellSelectionEnabled()) {

int selectionMode = table.getSelectionModel().getSelectionMode();

System.out.println("selectionMode = " + selectionMode);

if (selectionMode == ListSelectionModel.SINGLE_SELECTION) {

int rowIndex = table.getSelectedRow();

int colIndex = table.getSelectedColumn();

System.out.printf("Selected [Row,Column] = [%d,%d]\n", rowIndex, colIndex);

} else if (selectionMode == ListSelectionModel.SINGLE_INTERVAL_SELECTION ||

selectionMode == ListSelectionModel.MULTIPLE_INTERVAL_SELECTION) {

int rowIndexStart = table.getSelectedRow();

int rowIndexEnd = table.getSelectionModel().getMaxSelectionIndex();

int colIndexStart = table.getSelectedColumn();

int colIndexEnd = table.getColumnModel().getSelectionModel().getMaxSelectionIndex();

for (int i = rowIndexStart; i <= rowIndexEnd; i++) {

for (int j = colIndexStart; j <= colIndexEnd; j++) {

if (table.isCellSelected(i, j)) {

System.out.printf("Selected [Row,Column] = [%d,%d]\n", i, j);

}

}

}

}

}

}

}

private static void showFrame() {

JPanel panel = new JTableGetSelectedCells();

panel.setOpaque(true);

JFrame frame = new JFrame("JTable Selected Cells Demo");

frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

frame.setContentPane(panel);

frame.pack();

frame.setVisible(true);

}

public static void main(String[] args) {

SwingUtilities.invokeLater(new Runnable() {

public void run() {

showFrame();

}

});

}

class PremiereLeagueTableModel extends AbstractTableModel {

// TableModel的列名

private String[] columnNames = {

"TEAM", "P", "W", "D", "L", "GS", "GA", "GD", "PTS"

};

// TableModel的数据

private Object[][] data = {

{ "Liverpool", 3, 3, 0, 0, 7, 0, 7, 9 },

{ "Tottenham", 3, 3, 0, 0, 8, 2, 6, 9 },

{ "Chelsea", 3, 3, 0, 0, 8, 3, 5, 9 },

{ "Watford", 3, 3, 0, 0, 7, 2, 5, 9 },

{ "Manchester City", 3, 2, 1, 0, 9, 2, 7, 7 }

};

public int getRowCount() {

return data.length;

}

public int getColumnCount() {

return columnNames.length;

}

@Override

public String getColumnName(int column) {

return columnNames[column];

}

public Object getValueAt(int rowIndex, int columnIndex) {

return data[rowIndex][columnIndex];

}

}

}

运行程序时,将显示以下屏幕。在中选择一些单元格JTable将以ListSelectionModel.MULTIPLE_INTERVAL_SELECTION选择模式打印选定的行,选定的列或选定的行和列的某些索引。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/305184.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

@scheduled注解配置时间_SpringBoot2.0实战(32)配置定时任务

定时任务的几种实现方式&#xff1a;Timer&#xff1a;Java自带的java.util.Timer类&#xff0c;这个类允许你调度一个java.util.TimerTask任务。使用这种方式可以让你的程序按照某一个频度执行&#xff0c;但不能在指定时间运行。一般用的较少。Quartz&#xff1a;使用Quartz&…

小心 Enum Parse 中的坑

小心 Enum Parse 中的坑Intro最近使用枚举的时候&#xff0c;踩了一个小坑&#xff0c;分享一下&#xff0c;主要是枚举从 int 值转成枚举时可能会遇到Sample来看下面的示例&#xff1a;首先定义一个枚举&#xff1a;public enum Color : byte {Red 0,Green 1,Blue 2, }来看…

python判断列表是否为空_Jinja2: 判断返回的列表是否为空

我们在使用 Python 或者 Ansible 来进行自动化任务的时候常常会进行一些数据的组合和提取来生成文件。但是我们需要为不同的情况来做分析和进行判断。如果我们需要对返回的 list 来进行提取的时候我们常常只是运行一个 for loop 就解决了问题。如果输出如下所示&#xff1a;{&q…

在php中使用kind,KindEditor 4.x在PHP中的应用实例!

1.解压放入php项目静态资源文件夹&#xff0c;如下图&#xff1a;Paste_Image.png2.如果只是php使用&#xff0c;可以删除其它类型语言的文件夹&#xff0c;文件结构如下图&#xff1a;Paste_Image.png3.打开php文件夹&#xff0c;更改upload_json.php里文件上传目录文件夹至Up…

你的朋友国庆假期都去了哪里玩?微信大数据告诉你!最远的朋友圈签到竟然来自……

国庆中秋八天假 你是出门四处浪浪浪了 还是躺在家里看朋友圈里的世界名景 10月8日&#xff0c;微信发布《国庆假期微信大数据报告》 从出境人数、热门地区、境外消费等角度 全方位展示国庆期间微信用户的出游情况 哪些城市的人最爱出境游&#xff1f; 哪个国家是最热门的出境目…

mysql安装版和解压版哪个好_红米k30pro变焦版和荣耀30pro哪个好-哪个更值得入手...

红米k30pro变焦版和荣耀30pro&#xff0c;两款手机都有着很强的性能配置&#xff0c;也在同等的价位上&#xff0c;今天我们就来对比一下&#xff0c;看看红米k30pro变焦版和荣耀30pro哪个性价比更高&#xff0c;有哪些配置区别&#xff01;一、主要参数对比荣耀30 Pro红米K30 …

记一次CPU持续100%及分析方法

背景 某天晚上八点多&#xff0c;突然收到一个 CPU 爆表的告警。过了一会&#xff0c;几个业务线就开始反馈系统变慢了。后面紧急处理了这台机器后&#xff0c;让业务先恢复正常。后续看了一下监控&#xff0c;拔凉拔凉的。这个服务是比较重要的一个老业务&#xff0c;.NET Fra…

php中请写出定义变量的两种方法,php定义变量几种

1、定义常量define("CONSTANT", "Hello world.")&#xff1b;常量只能包含标量数据(boolean&#xff0c;integer&#xff0c;float 和 string)&#xff0c;调用常量时&#xff0c;只需要简单的用名称取得常量的值&#xff0c;而不能加“$”符号。注: 常量和…

八款Js框架介绍及比较~转载

Js框架介绍 目前来看&#xff0c;JS框架以及一些开发包和库类有如下几个&#xff0c;Dojo 、Scriptaculous 、Prototype 、yui-ext 、Jquery 、Mochikit、mootools 、moo.fxDojo &#xff08;JS library and UI component &#xff09;&#xff1a;Dojo是目前最为强大的j s框架…

python等值面图平滑_离散点插值方法、等值线的绘制及平滑技巧

离散点插值方法、等值线的绘制及平滑技巧2008-06-10 22:45由于等值线图看起来非常直观、形象,因此在天气预报、气候预测分析等方面用得非常多,已成为预报员不可缺少的工具之一。如各等压面层的位势高度图、高空环流、温度及降水分布图等等。目前也有一些非常好的微机用绘图软件…

c语言三目运算符_C语言中的三目运算符是啥?有何用处?

一般来说&#xff0c;C语言中的三目运算符为a?b:c即有三个参与运算的量。由条件运算符组成条件表达式的一般形式为&#xff1a;表达式1? 表达式2&#xff1a;表达式3求值规则为&#xff1a;如果表达式1的值为真&#xff0c;则以表达式2 的值作为条件表达式的值&#xff0c;否…

Dotnet的局部函数和委托的对比

上一篇说了一下委托&#xff0c;这篇来说说局部函数和委托的对比。把委托和局部函数放成前后篇&#xff0c;是因为这两个内容很像&#xff0c;用起来容易混。需要了解委托相关内容&#xff0c;可以看这一篇 【传送门】使用委托表达式(Lambda)假设一个场景&#xff1a;我们有一个…

JAVA设置新视口,java – 在更大的图像上移动视口; JLablel JScrollPane

这是一个非常基本的例子.它使用一个图像文件并将其放置在一个滚动窗格内(在一个圆形的方式).从那里,它只是使用Swing Timer来随机生成点(在图像的边界内).每次生成一个新点时,我只需使用scrollToRectVisible,传递它想要渲染的点的位置和大小.这将确保新点(和点)在滚动窗格中可见…

经纬度 c代码中定义_如何将TXT文本格式的批量经纬度值导入到奥维成为标签

文本编辑&#xff1a;示例1&#xff1a;最基本的&#xff0c;只批量导入WGS-84经纬度值成为标签&#xff0c;不需要导入标签名称。 文本编辑格式&#xff1a;经度值空格纬度值换行&#xff0c;如下图&#xff1a;示例2&#xff1a;除WGS-84经纬度外&#xff0c;还要导入标签名称…

C# aspx页面动态加载ascx用户控件 及 利用反射调用其内方法

//控件代码 public partial class WebUserControl : System.Web.UI.UserControl { public void TestMethod(string strID) { this.TextBox1.Text " WebUserControl:" strID; //其他相关操作 } } //控件代码 public partial class WebU…

中国式创新技术“步态识别”终于来临,你大胆地走两步,我就知道你是谁

放完假的数据君&#xff0c;回到办公室&#xff0c;苦恼该码一篇什么文章&#xff0c;来给各位送上“节后的祝福”。 这么想着&#xff0c;数据君便开始浏览最新的科技报道&#xff1a; 什么鬼&#xff01;这难道是什么新兴的黑科技吗&#xff1f;&#xff01; 数据君赶紧查了…

帆软获取上月的第一天与最后一天_《原神》岩港打工第一天怎么玩 岩港打工第一天玩法攻略...

《原神》在11月2日开启了岩港奇珍行记&#xff0c;玩家可以在璃月港进行打工了&#xff0c;可能有的小伙伴还不清楚第一天的打工要怎么做&#xff0c;所以小编这次就为大家带来了《原神》岩港打工第一天玩法攻略&#xff0c;感兴趣的小伙伴可以来看一下。岩港打工第一天玩法攻略…

AgileConfig - RESTful API 介绍

AgileConfigAgileConfig是一个基于.net core开发的轻量级配置中心。AgileConfig秉承轻量化的特点&#xff0c;部署简单、配置简单、使用简单、学习简单&#xff0c;它只提取了必要的一些功能&#xff0c;并没有像Apollo那样复杂且庞大。但是它的功能也已经足够你替换webconfig&…

mysql卸载时弹框,win10卸载mysql5安装mysql8

使用mysql5的过程中使用 datetime DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP 无法执行&#xff0c;原因是版本问题&#xff0c;因此我需要安装更高级的版本。一、卸载原有的mysql1、在控制面板中卸载mysql2、运行“regedit”文件&#xff0c;删除HKEY_LOCAL_MACHINE\SYSTEM\Co…