Table機能のプレゼンテーション(Tableの追加、削除、行番号の設定)

8686 ワード

import javax.swing.*;
import javax.swing.table.*;
import java.awt.*;
import java.awt.event.*;

final public class TableDemo extends JFrame {
	private static final long serialVersionUID = 1L;
	private JButton increaseHeightButton, reduceHeightButton, addRowButton,
			deleteRowButton;
	private LineNumberTablePane tablePane;

	private JTable primaryTable = new JTable(), numberTable = new JTable();

	public TableDemo() throws HeadlessException {
		super("line number table");
		initButton();
		// buildPrimaryTable();
		tablePane = new LineNumberTablePane(primaryTable, numberTable);
		buildPrimaryTable();
		doLay();
		// tablePane.refershRowHeader();
	}

	private void buildPrimaryTable() {
		String[][] datas = new String[][] { { " ", "28", "56.2" },
				{ " ", "30", "56.2" }, { " ", "33", "56.2" },
				{ " ", "35", "156.2" } };
		String[] columnNames = new String[] { " ", " ", " " };
		primaryTable.setModel(new DefaultTableModel(datas, columnNames));
		tablePane.notifyRowsChanged();
	}

	private void initButton() {
		increaseHeightButton = new JButton("increase row height");
		increaseHeightButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				int old = primaryTable.getRowHeight();
				primaryTable.setRowHeight(old + 5);
			}
		});

		reduceHeightButton = new JButton("reduce row height");
		reduceHeightButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				int old = primaryTable.getRowHeight();
				primaryTable.setRowHeight(old - 5);
			}
		});

		addRowButton = new JButton("add row");
		addRowButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				DefaultTableModel dm = (DefaultTableModel) primaryTable
						.getModel();

				String[] insertedRowData = new String[] { " ", "X", "Y" };
				dm.insertRow(0, insertedRowData);
				tablePane.notifyRowsChanged();
				tablePane.scrollRowToVisible(10);
			}
		});

		deleteRowButton = new JButton("delete Row");
		deleteRowButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				DefaultTableModel dm = (DefaultTableModel) primaryTable
						.getModel();
				dm.removeRow(0);
				tablePane.notifyRowsChanged();
				tablePane.scrollRowToVisible(10);
			}
		});
	}

	private void doLay() {
		Container container = getContentPane();

		JPanel buttonsPane = new JPanel(new FlowLayout(FlowLayout.LEFT, 0, 0));
		buttonsPane.add(increaseHeightButton);
		buttonsPane.add(reduceHeightButton);
		buttonsPane.add(addRowButton);
		buttonsPane.add(deleteRowButton);

		container.add(buttonsPane, BorderLayout.NORTH);
		container.add(tablePane, BorderLayout.CENTER);
		setSize(470, 200);
		setVisible(true);
	}

	public static void main(String[] args) {
		new TableDemo();
	}
}

 
import javax.swing.*;
import javax.swing.table.*;
import java.awt.*;
import java.beans.*;
// Table 
final public class LineNumberTablePane extends JScrollPane {
	private static final long serialVersionUID = 1L;

	private JTable primaryTable = new JTable();
	private JTable numberTable = new JTable();

	private static final int DEFAULT_MIN_ROW_HEADER_WIDTH = 30;

	private static final String ROW_HEADER_IDENTIFIER = "lineNumberColumn";
	private static final String ROW_HEADER_COLUMN_NAME = "lineNumberRowColumn";
	private static final String ROW_HEADER_VALUE = " ";

	private TableColumn rowHeaderColumn = new TableColumn(0);
	private DefaultTableModel rowHeaderTableModel = new NumberTableModel();

	public LineNumberTablePane(final JTable primaryTableComponent,
			final JTable numberTableComponent) {
		super(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
				JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
		primaryTable = primaryTableComponent;
		numberTable = numberTableComponent;
		numberTable.setModel(rowHeaderTableModel);
		numberTable.setColumnModel(new NumberTableColumnModel());

		checkPropertyListeners();
		notifyRowsChanged();
	}

	public void notifyRowsChanged() {
		while (rowHeaderTableModel.getRowCount() > 0)
			rowHeaderTableModel.removeRow(0);
		for (int i = 0, j = primaryTable.getRowCount(); i < j; i++) {
			rowHeaderTableModel.addRow(new Integer[] { i + 1 });
		}
		adjustRowHeaderHeight();
	}

	private void adjustRowHeaderHeight() {
		setViewportView(primaryTable);
		setRowHeaderView(numberTable);
		int h = this.getVerticalScrollBar().getSize().height;
		getRowHeader().setPreferredSize(
				new Dimension(DEFAULT_MIN_ROW_HEADER_WIDTH, h));
	}

	private void checkPropertyListeners() {
		primaryTable.addPropertyChangeListener(new PropertyChangeListener() {
			public void propertyChange(PropertyChangeEvent evt) {
				if (evt.getPropertyName().equals("rowHeight")) {
					Integer h = (Integer) evt.getNewValue();
					numberTable.setRowHeight(h);
					adjustRowHeaderHeight();
				}
			}
		});
	}

	public void scrollRowToVisible(int row) {
		int rowMargin = primaryTable.getRowMargin();

		int y = 0, w = 0;
		for (int i = 0; i < row; i++)
			y += primaryTable.getRowHeight(i) + rowMargin;

		TableColumnModel columnModel = primaryTable.getColumnModel();
		for (int i = 0, j = columnModel.getColumnCount(); i < j; i++)
			w += columnModel.getColumn(i).getWidth();

		primaryTable.scrollRectToVisible(new Rectangle(0, y, w, primaryTable
				.getRowHeight(row)));
	}

	public JTable getPrimaryTable() {
		return primaryTable;
	}

	public JTable getRowHeaderTable() {
		return numberTable;
	}

	private class NumberTableModel extends DefaultTableModel {
		private static final long serialVersionUID = 1L;

		public int getColumnCount() {
			return 1;
		}

		public boolean isCellEditable(int rowIndex, int columnIndex) {
			return false;
		}

		public Object getValueAt(int rowIndex, int columnIndex) {
			return columnIndex == 0 ? rowIndex + 1 : null;
		}

		public String getColumnName(int column) {
			return column == 0 ? ROW_HEADER_COLUMN_NAME : "";
		}

		public Class<?> getColumnClass(int columnIndex) {
			return columnIndex == 0 ? Integer.class : null;
		}
	}

	private class NumberTableColumnModel extends DefaultTableColumnModel {
		private static final long serialVersionUID = 1L;

		private NumberTableColumnModel() {
			rowHeaderColumn.setIdentifier(ROW_HEADER_IDENTIFIER);
			rowHeaderColumn.setHeaderValue(ROW_HEADER_VALUE);

			TableCellRenderer renderer = new RowHeaderCellRenderer();

			rowHeaderColumn.setCellRenderer(renderer);
			rowHeaderColumn.setHeaderRenderer(renderer);
			addColumn(rowHeaderColumn);
		}

		public int getColumnCount() {
			return 1;
		}
	}

	private class RowHeaderCellRenderer implements TableCellRenderer {
		public Component getTableCellRendererComponent(JTable table,
				Object value, boolean isSelected, boolean hasFocus, int row,
				int column) {
			JLabel label = new JLabel() {
				private static final long serialVersionUID = 1L;

				public Insets getInsets() {
					return new Insets(0, 0, 0, 0);
				}
			};

			label.setHorizontalAlignment(SwingConstants.RIGHT);
			if (column == 0) {
				label.setText(value.toString());
				label.setOpaque(true);
				label.setBackground(Color.BLACK);
				label.setForeground(Color.YELLOW);
			}
			return label;
		}
	}
}