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选择模式打印选定的行,选定的列或选定的行和列的某些索引。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值