package testing;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;
public class TableTest extends JFrame
{
public TableTest (){
buildGUI();
}
private void buildGUI(){
setSize(700, 500);
setDefaultCloseOperation(EXIT_ON_CLOSE);
getContentPane().setLayout(new GridBagLayout());
final JTable table = new JTable ();
DefaultTableModel tableModel = new DefaultTableModel ();
tableModel.addColumn("Name");
tableModel.addColumn("Vorname");
tableModel.addColumn("Alter");
table.setModel(tableModel);
//table.setPreferredScrollableViewportSize(new Dimension(500, 100));
table.setMinimumSize(new Dimension (100, 50));
table.setMaximumSize(new Dimension (600, 300));
table.setPreferredScrollableViewportSize(new Dimension(400, 200));
table.setColumnModel( new MyTableColumnModel() );
JScrollPane scrollPane = new JScrollPane (){
/**
* @deprecated Ein Listener wäre natürlich noch schöner, als dieser Hack.
*/
public void reshape( int x, int y, int w, int h ){
table.setAutoResizeMode( w > 300 ? JTable.AUTO_RESIZE_SUBSEQUENT_COLUMNS : JTable.AUTO_RESIZE_OFF );
super.reshape( x, y, w, h );
}
};
scrollPane.setViewportView(table);
// scrollPane.setAutoscrolls(false);
tableModel.addRow(new Object [] {"Ey", "Birgit", "21"});
tableModel.addRow(new Object [] {"Fritz", "Andrea", "56"});
tableModel.addRow(new Object [] {"Podlesny", "Gregor", "54"});
getContentPane().add(scrollPane, new GridBagConstraints(0, 0, 1, 1, 1.0, 1.0, GridBagConstraints.NORTHWEST, GridBagConstraints.BOTH, new Insets (15, 15, 0, 0), 0, 0));
this.setVisible(true);
}// endmethod
public static void main (String [] args)
{
TableTest m_ref = new TableTest ();
}
}// endclass
class TableRenderer extends JLabel implements TableCellRenderer{
public TableRenderer(){
}
public Component getTableCellRendererComponent(JTable table, Object value, boolean a, boolean b, int row, int column){
setText( " -!- " + value.toString() );
return this;
}
}
class MyTableColumnModel extends DefaultTableColumnModel
{
protected TableColumn column_1 = new TableColumn(0);
protected TableColumn column_2 = new TableColumn(1);
private TableRenderer cellRenderer = new TableRenderer ();
protected MyTableColumnModel ()
{
super();
setPropertiesColumn_1();
setPropertiesColumn_2();
addColumn(column_1);
addColumn(column_2);
}// end constructor
private void setPropertiesColumn_1 ()
{
column_1.setCellRenderer(cellRenderer);
column_1.setHeaderValue("Hallo");
column_1.setResizable(false);
column_1.setWidth(200);
}
private void setPropertiesColumn_2 ()
{
column_2.setCellRenderer(cellRenderer);
column_2.setHeaderValue("Hallo2");
column_2.setResizable(false);
column_2.setWidth(300);
}
}// end class