JTableを使う練習
2009/11/28
java
jtable
swing
Javaです。
JTableを使った簡単なツールを書くときいつも時間かかってます。
毎回グーグルさんで調べたり 、swingなら、Java Swing Tips - てんぷらメモで調べてます。
組み込みDBを使ってデータをderby,h2とかにもたせて使うととか、csv,tsvでデータをもたせるとかJTableを手軽に使えたほうが何かと便利ですね。
package ui;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SortOrder;
import javax.swing.SwingUtilities;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;
import javax.swing.table.TableModel;
import javax.swing.table.TableRowSorter;
/**
*
* テーブルを使ったUIのプロトタイプを手軽に作りたい目的用途 いい方法が思いつかない。
*
*/
public class PanelTable extends JPanel {
/**
* 参考
* http://terai.xrea.jp/Swing/StripeTable.html
*
*/
class StripeTableRenderer extends DefaultTableCellRenderer {
Color evenColor = new Color(244, 247, 251);
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus,
int row, int column) {
super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
if(isSelected) {
setForeground(table.getSelectionForeground());
setBackground(table.getSelectionBackground());
}else{
setForeground(table.getForeground());
setBackground((row%2==0)?evenColor:table.getBackground());
}
setHorizontalAlignment((value instanceof Number)?RIGHT:LEFT);
return this;
}
}
public class Column {
String s;
int w;
public Column(String s, Integer w) {
this.s = s;
this.w = w;
}
public String toString() {
return this.s;
}
}
public Vector<Column> toColumns(Object[][] os) {
Vector<Column> columns = new Vector<Column>();
for (Object[] os2 : os) {
Column column = new Column((String) os2[0], (Integer) os2[1]);
columns.add(column);
}
return columns;
}
final JTable table = new JTable() {
{
setDefaultRenderer(Object.class, new StripeTableRenderer());
setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
}
};
JScrollPane scrollPane;
{
this.scrollPane = new JScrollPane(table);
setLayout(new BorderLayout());
add(this.scrollPane, BorderLayout.CENTER);
}
/**
* vecOfvecが行指向の場合 行指向のデータの持ち方
*
* @param vecOfvec
* @param columns
*/
public void setModel_RowBase(final Vector<Vector<String>> vecOfvec,
final Vector<Column> columns) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
DefaultTableModel model = new DefaultTableModel();
model.setDataVector(vecOfvec, columns);
setModel(model);
for (int i = 0; i < columns.size(); i++) {
Column c = columns.get(i);
TableColumn tableColumn = table.getColumnModel().getColumn(
i);
tableColumn.setHeaderValue(c.s);
tableColumn.setPreferredWidth(c.w);
}
}
});
}
/**
* vecOfvecが列指向の場合 列指向のデータの持ち方
*
* @param vecOfvec
* @param columns
*/
public void setModel_ColBase(final Vector<Vector<String>> vecOfvec,
final Vector<Column> columns) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
DefaultTableModel model = new DefaultTableModel();
for (Vector<String> v : vecOfvec) {
model.addColumn("", v);
}
setModel(model);
for (int i = 0; i < columns.size(); i++) {
Column c = columns.get(i);
TableColumn tableColumn = table.getColumnModel().getColumn(
i);
tableColumn.setHeaderValue(c.s);
tableColumn.setPreferredWidth(c.w);
}
}
});
}
/**
* 参考
* http://terai.xrea.jp/Swing/TriStateSorting.html
* @param model
*/
protected void setModel(TableModel model) {
this.table.setModel(model);
TableRowSorter<TableModel> sorter = new TableRowSorter<TableModel>(
model) {
@Override
public void toggleSortOrder(int column) {
if (column >= 0 && column < getModelWrapper().getColumnCount()
&& isSortable(column)) {
List<SortKey> keys = new ArrayList<SortKey>(getSortKeys());
if (!keys.isEmpty()) {
SortKey sortKey = keys.get(0);
if (sortKey.getColumn() == column
&& sortKey.getSortOrder() == SortOrder.DESCENDING) {
setSortKeys(null);
return;
}
}
}
super.toggleSortOrder(column);
}
};
this.table.setRowSorter(sorter);
}
}
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SortOrder;
import javax.swing.SwingUtilities;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;
import javax.swing.table.TableModel;
import javax.swing.table.TableRowSorter;
/**
*
* テーブルを使ったUIのプロトタイプを手軽に作りたい目的用途 いい方法が思いつかない。
*
*/
public class PanelTable extends JPanel {
/**
* 参考
* http://terai.xrea.jp/Swing/StripeTable.html
*
*/
class StripeTableRenderer extends DefaultTableCellRenderer {
Color evenColor = new Color(244, 247, 251);
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus,
int row, int column) {
super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
if(isSelected) {
setForeground(table.getSelectionForeground());
setBackground(table.getSelectionBackground());
}else{
setForeground(table.getForeground());
setBackground((row%2==0)?evenColor:table.getBackground());
}
setHorizontalAlignment((value instanceof Number)?RIGHT:LEFT);
return this;
}
}
public class Column {
String s;
int w;
public Column(String s, Integer w) {
this.s = s;
this.w = w;
}
public String toString() {
return this.s;
}
}
public Vector<Column> toColumns(Object[][] os) {
Vector<Column> columns = new Vector<Column>();
for (Object[] os2 : os) {
Column column = new Column((String) os2[0], (Integer) os2[1]);
columns.add(column);
}
return columns;
}
final JTable table = new JTable() {
{
setDefaultRenderer(Object.class, new StripeTableRenderer());
setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
}
};
JScrollPane scrollPane;
{
this.scrollPane = new JScrollPane(table);
setLayout(new BorderLayout());
add(this.scrollPane, BorderLayout.CENTER);
}
/**
* vecOfvecが行指向の場合 行指向のデータの持ち方
*
* @param vecOfvec
* @param columns
*/
public void setModel_RowBase(final Vector<Vector<String>> vecOfvec,
final Vector<Column> columns) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
DefaultTableModel model = new DefaultTableModel();
model.setDataVector(vecOfvec, columns);
setModel(model);
for (int i = 0; i < columns.size(); i++) {
Column c = columns.get(i);
TableColumn tableColumn = table.getColumnModel().getColumn(
i);
tableColumn.setHeaderValue(c.s);
tableColumn.setPreferredWidth(c.w);
}
}
});
}
/**
* vecOfvecが列指向の場合 列指向のデータの持ち方
*
* @param vecOfvec
* @param columns
*/
public void setModel_ColBase(final Vector<Vector<String>> vecOfvec,
final Vector<Column> columns) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
DefaultTableModel model = new DefaultTableModel();
for (Vector<String> v : vecOfvec) {
model.addColumn("", v);
}
setModel(model);
for (int i = 0; i < columns.size(); i++) {
Column c = columns.get(i);
TableColumn tableColumn = table.getColumnModel().getColumn(
i);
tableColumn.setHeaderValue(c.s);
tableColumn.setPreferredWidth(c.w);
}
}
});
}
/**
* 参考
* http://terai.xrea.jp/Swing/TriStateSorting.html
* @param model
*/
protected void setModel(TableModel model) {
this.table.setModel(model);
TableRowSorter<TableModel> sorter = new TableRowSorter<TableModel>(
model) {
@Override
public void toggleSortOrder(int column) {
if (column >= 0 && column < getModelWrapper().getColumnCount()
&& isSortable(column)) {
List<SortKey> keys = new ArrayList<SortKey>(getSortKeys());
if (!keys.isEmpty()) {
SortKey sortKey = keys.get(0);
if (sortKey.getColumn() == column
&& sortKey.getSortOrder() == SortOrder.DESCENDING) {
setSortKeys(null);
return;
}
}
}
super.toggleSortOrder(column);
}
};
this.table.setRowSorter(sorter);
}
}
: