package sample;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.AdjustmentEvent;
import java.awt.event.AdjustmentListener;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.DefaultCellEditor;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ScrollPaneConstants;
import javax.swing.SwingUtilities;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;
import edu.ucla.loni.inspector.manager.ScrollBarManager;
public class SampleTable extends JFrame implements ActionListener {
private JComboBox comboBox;
private JTable table1;
private JTable table2;
private DefaultTableModel tableModel1;
private DefaultTableModel tableModel2;
private JScrollPane scrollPane1;
private JScrollPane scrollPane2;
private JButton button;
private JPanel panel;
private Box tableBox;
// Constants.
private final String COMBO_BOX_TITLE = "Files";
private final String[] COLUMN_NAMES = {"Column1", "Column2", "Column3"};
private final int FIRST_COLUMN = 0;
private final int SECOND_COLUMN = 1;
private final int THIRD_COLUMN = 2;
private final int FIRST_THREE_COLUMNS = 3;
private final String ADD_COLUMN = "Add Column";
private final int COLUMN_WIDTH = 150;
public SampleTable() {
setBounds(10,10,700,700);
setTables();
tableBox = new Box(BoxLayout.Y_AXIS);
tableBox.add(scrollPane1);
tableBox.add(scrollPane2);
button = new JButton(ADD_COLUMN);
panel = new JPanel();
panel.add(button);
button.addActionListener(this);
button.setActionCommand(ADD_COLUMN);
// Add conponents.
getContentPane().add(new JPanel(),BorderLayout.NORTH); // <--- ここをNORTHにするか、CENTERにするかでRowの表示枠サイズが変化
getContentPane().add(tableBox,BorderLayout.CENTER); // <--- ここをNORTHにするか、CENTERにするかでRowの表示枠サイズが変化
getContentPane().add(panel,BorderLayout.SOUTH);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Not called from EDT.
SwingUtilities.invokeLater(new Runnable() {
public void run() {
setVisible(true);
}
});
}
private void setTables() {
// Set table 1 ------------------------------------------------->
tableModel1 = new DefaultTableModel(1,1);
tableModel1.setValueAt(COMBO_BOX_TITLE, 0, 0);
table1 = new JTable(tableModel1);
table1.setTableHeader(null);
table1.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
int width = COLUMN_WIDTH * FIRST_THREE_COLUMNS;
// table1.getColumnModel().getColumn(FIRST_COLUMN).setWidth(width);
table1.getColumnModel().getColumn(FIRST_COLUMN).setPreferredWidth(width);
table1.getColumnModel().getColumn(FIRST_COLUMN).setMinWidth(width);
table1.getColumnModel().getColumn(FIRST_COLUMN).setMaxWidth(width);
// Set a view port size.
// table1.setPreferredScrollableViewportSize(new Dimension(400,10)); <---- 特定Rowの表示
// Set JComboBox as an editor.
TableColumn fileColumn = table1.getColumnModel().getColumn(FIRST_COLUMN);
comboBox = new JComboBox();
comboBox.addItem(COMBO_BOX_TITLE);
fileColumn.setCellEditor(new DefaultCellEditor(comboBox));
// end of set table 1 <------------------------------------------
// Set table 2 ------------------------------------------------->
tableModel2 = new DefaultTableModel(COLUMN_NAMES,0);
table2 = new JTable(tableModel2);
table2.getTableHeader().setReorderingAllowed(false);
table2.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
// First column.
// table2.getColumnModel().getColumn(FIRST_COLUMN).setWidth(COLUMN_WIDTH);
table2.getColumnModel().getColumn(FIRST_COLUMN).setPreferredWidth(COLUMN_WIDTH);
table2.getColumnModel().getColumn(FIRST_COLUMN).setMinWidth(COLUMN_WIDTH);
table2.getColumnModel().getColumn(FIRST_COLUMN).setMaxWidth(COLUMN_WIDTH);
// Second column.
// table2.getColumnModel().getColumn(SECOND_COLUMN).setWidth(COLUMN_WIDTH);
table2.getColumnModel().getColumn(SECOND_COLUMN).setPreferredWidth(COLUMN_WIDTH);
table2.getColumnModel().getColumn(SECOND_COLUMN).setMinWidth(COLUMN_WIDTH);
table2.getColumnModel().getColumn(SECOND_COLUMN).setMaxWidth(COLUMN_WIDTH);
// Third column.
// table2.getColumnModel().getColumn(THIRD_COLUMN).setWidth(COLUMN_WIDTH);
table2.getColumnModel().getColumn(THIRD_COLUMN).setPreferredWidth(COLUMN_WIDTH);
table2.getColumnModel().getColumn(THIRD_COLUMN).setMinWidth(COLUMN_WIDTH);
table2.getColumnModel().getColumn(THIRD_COLUMN).setMaxWidth(COLUMN_WIDTH);
// end of set table 2 <------------------------------------------
// Set tables in a JScrollPane.
scrollPane1 = new JScrollPane(table1);
scrollPane1.setPreferredSize(new Dimension(400,20)); <---- 特定Rowの表示
scrollPane1.setVerticalScrollBarPolicy(
ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER);
scrollPane1.setHorizontalScrollBarPolicy(
ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
scrollPane2 = new JScrollPane(table2);
scrollPane2.setPreferredSize(new Dimension(400,480));
// Make scroll simultaneous.
final JScrollBar scrollBar1 = scrollPane1.getHorizontalScrollBar();
JScrollBar scrollBar2 = scrollPane2.getHorizontalScrollBar();
scrollBar2.addAdjustmentListener(new AdjustmentListener() {
public void adjustmentValueChanged(AdjustmentEvent e) {
scrollBar1.setValue(e.getValue());
}
});
}
public void actionPerformed(ActionEvent e) {
String command = e.getActionCommand();
if(command.equals(ADD_COLUMN)){
TableColumn column2 = new TableColumn();
table2.getColumnModel().addColumn(column2);
int resizeWidth = 0;
for(int i=FIRST_COLUMN;i<FIRST_THREE_COLUMNS;i++){
resizeWidth += table2.getColumnModel().getColumn(i).getWidth();
}
TableColumn column = new TableColumn();
table1.getColumnModel().addColumn(column);
table1.getColumnModel().getColumn(FIRST_COLUMN).setPreferredWidth(resizeWidth);
}
}
public static void main(String[] args) {
// Convention for starting Swing application?
SwingUtilities.invokeLater(new Runnable() {
public void run() {
SampleTable st = new SampleTable();
}
});
}
}
|