日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

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

發布時間:2023/12/4 java 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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選擇模式打印選定的行,選定的列或選定的行和列的某些索引。

總結

以上是生活随笔為你收集整理的java获取jtable的路径,Java如何在JTable组件中获取选定的单元格?的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。