JTableあれこれ
セル選択時の状態表示
* setColumnSelectionAllowed(boolean)/setRowSelectionAllowed(boolean) で制御http://www.javadrive.jp/tutorial/jtable/index16.html
サンプル
* 基本編import java.awt.BorderLayout; import java.awt.Dimension; import java.util.ArrayList; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTable; import javax.swing.table.DefaultTableModel; public class TableSample extends JFrame { private static final long serialVersionUID = 1L; public static void main(String[] args) { TableSample sample = new TableSample(); sample.setVisible(true); } public TableSample() { ArrayList<Person> people = new ArrayList<Person>() { private static final long serialVersionUID = 1L; { add(new Person(1L, "Mike", 23)); add(new Person(2L, "Tom", 15)); add(new Person(3L, "Kevin", 36)); add(new Person(4L, "Cocolo", 13)); add(new Person(5L, "Mike", 41)); } }; String[] columnNames = {"ID", "Name", "Age",}; DefaultTableModel tableModel = new DefaultTableModel(columnNames, 0); JTable table = new JTable(tableModel); for(Person person : people){ tableModel.addRow(toTableData(person)); } JScrollPane scrollPane = new JScrollPane(table); scrollPane.setPreferredSize(new Dimension(400, 100)); JPanel panel = new JPanel(); panel.add(scrollPane); this.getContentPane().add(panel, BorderLayout.CENTER); this.setTitle("This is title"); this.setSize(500, 300); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } private static String[] toTableData(Person person) { String[] returnValues = new String[3]; returnValues[0] = person.getId().toString(); returnValues[1] = person.getName(); returnValues[2] = person.getAge().toString(); return returnValues; } }* 応用編(テキストボックスとボタンを付けて検索機能にした)
import java.awt.BorderLayout; import java.awt.Dimension; import java.awt.Toolkit; import java.awt.datatransfer.Clipboard; import java.awt.datatransfer.StringSelection; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.util.ArrayList; import java.util.List; import java.util.stream.Collectors; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JMenuItem; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JPopupMenu; import javax.swing.JScrollPane; import javax.swing.JTable; import javax.swing.JTextField; import javax.swing.table.DefaultTableModel; import javax.swing.table.TableColumn; public class TableSample extends JFrame implements MouseListener, ActionListener { private static final long serialVersionUID = 1L; public static void main(String[] args) { TableSample sample = new TableSample(); sample.setVisible(true); } private String[] columnNames = { "ID", "Name", "Age", }; private List<Person> people; private JPanel tablePanel; private JScrollPane scrollPane; private DefaultTableModel tableModel; private JTable table; private JTextField textField; private JButton searchButton; private JPopupMenu popup; private JMenuItem copyMenuItem; public TableSample() { JPanel textAndButtonPanel = new JPanel(); JLabel label = new JLabel("Target Name : "); textAndButtonPanel.add(label); this.textField = new JTextField(); this.textField.setPreferredSize(new Dimension(100, 20)); textAndButtonPanel.add(this.textField); this.searchButton = new JButton("Search"); this.searchButton.addActionListener(this); textAndButtonPanel.add(this.searchButton); this.getContentPane().add(textAndButtonPanel, BorderLayout.NORTH); this.people = new ArrayList<Person>() { private static final long serialVersionUID = 1L; { add(new Person(1L, "Mike", 23)); add(new Person(2L, "Tom", 15)); add(new Person(3L, "Kevin", 36)); add(new Person(4L, "Cocolo", 13)); add(new Person(5L, "Mike", 41)); } }; this.tableModel = new DefaultTableModel(this.columnNames, 0); this.table = new JTable(this.tableModel); // this.table.setColumnSelectionAllowed(true); // this.table.setRowSelectionAllowed(true); for (Person person : this.people) { this.tableModel.addRow(toTableData(person)); } this.table.addMouseListener(this); this.scrollPane = new JScrollPane(this.table); this.scrollPane.setPreferredSize(new Dimension(400, 100)); this.tablePanel = new JPanel(); this.tablePanel.add(this.scrollPane); this.getContentPane().add(this.tablePanel, BorderLayout.CENTER); // 右クリック this.popup = new JPopupMenu(); this.copyMenuItem = new JMenuItem("コピー"); this.copyMenuItem.addActionListener(this); this.popup.add(this.copyMenuItem); ; this.setTitle("This is title"); this.setSize(500, 300); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } private static String[] toTableData(Person person) { String[] returnValues = new String[3]; returnValues[0] = person.getId().toString(); returnValues[1] = person.getName(); returnValues[2] = person.getAge().toString(); return returnValues; } @Override public void actionPerformed(ActionEvent actionEvent) { Object control = actionEvent.getSource(); if (control == this.searchButton) { this.search(); } else if (control == this.copyMenuItem) { this.copy(); } } private void copy() { int[] rows = table.getSelectedRows(); int[] columns = table.getSelectedColumns(); StringBuffer buffer = new StringBuffer(); for (int i = 0; i < table.getColumnCount(); i++) { TableColumn tableColumn = table.getColumnModel().getColumn(i); buffer.append((String) tableColumn.getIdentifier()); if (i < table.getColumnCount() - 1) { // タブ文字を付加 buffer.append("\t"); } else { buffer.append("\n"); } } for (int i = 0; i < rows.length; i++) { for (int j = 0; j < columns.length; j++) { Object cellValue = table.getValueAt(rows[i], columns[j]); buffer.append((String) cellValue); if (j < columns.length - 1) { buffer.append("\t"); } } buffer.append("\n"); } copyToClipboard(buffer.toString()); } private void copyToClipboard(String copyString) { Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard(); StringSelection contents = new StringSelection(copyString); clipboard.setContents(contents, null); } private void search() { String targetName = this.textField.getText(); List<Person> peopleForFilter; if (targetName == null || targetName.isEmpty()) { peopleForFilter = this.people; } else { peopleForFilter = this.people.stream().filter(x -> x.getName().indexOf(targetName) != -1) .collect(Collectors.toList()); if (peopleForFilter.isEmpty()) { JOptionPane.showMessageDialog(this, "Not Found", "Info", JOptionPane.INFORMATION_MESSAGE); return; } } this.tablePanel.remove(this.scrollPane); this.tableModel = new DefaultTableModel(this.columnNames, 0); this.table = new JTable(this.tableModel); // this.table.setColumnSelectionAllowed(true); // this.table.setRowSelectionAllowed(true); for (Person person : peopleForFilter) { this.tableModel.addRow(toTableData(person)); } this.table.addMouseListener(this); this.scrollPane = new JScrollPane(this.table); this.scrollPane.setPreferredSize(new Dimension(400, 100)); this.tablePanel.add(this.scrollPane); this.repaint(); this.revalidate(); } @Override public void mouseClicked(MouseEvent e) { // Do nothing } @Override public void mousePressed(MouseEvent e) { this.showPopup(e); } @Override public void mouseReleased(MouseEvent e) { this.showPopup(e); } @Override public void mouseEntered(MouseEvent e) { // Do nothing } @Override public void mouseExited(MouseEvent e) { // Do nothing } private void showPopup(MouseEvent event) { if (event.isPopupTrigger()) { this.popup.show(event.getComponent(), event.getX(), event.getY()); } } }
参考文献
http://www.javadrive.jp/tutorial/jtable/http://www.javadrive.jp/tutorial/jtable/index8.html
セルのコピー
http://www6.atpages.jp/ridumi/Java/SwingMenu/SwingTec/JTable/TableDataPasteForExcel.html