tags:

views:

56

answers:

1

Hey guys, working on an event calendar. I'm having some trouble getting my column heads to display.. here is the code

private JTable calendarTable;
private DefaultTableModel calendarTableModel; 

final private String [] days = {"Sunday", "Monday", "Tuesday",
                                    "Wednesday", "Thursday", "Friday",
                                    "Saturday"};
//////////////////////////////////////////////////////////////////////
/* Setup the actual calendar table */


calendarTableModel = new DefaultTableModel() {
    public boolean isCellEditable(int row, int col){
             return false;
    }
};

// setup columns
for(int i = 0; i < 7; i++)
    calendarTableModel.addColumn(days[i]); 

calendarTable = new JTable(calendarTableModel);

calendarTable.getTableHeader().setResizingAllowed(false);
calendarTable.getTableHeader().setReorderingAllowed(false);

calendarTable.setColumnSelectionAllowed(true);
calendarTable.setRowSelectionAllowed(true);
calendarTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

calendarTable.setRowHeight(105);
calendarTableModel.setColumnCount(7);
calendarTableModel.setRowCount(6);

Also, Im sort of new with tables.. how can I make the rowHeight split between the max size of the table?

+2  A: 

Table header

Either put calendarTable in a JScrollPane, or add calendarTable.getTableHeader() as well.

Row height

Add a listener to change row height whenever the table is resized.

calendarTable.addComponentListener(new ComponentAdapter() {
    public void componentResized(ComponentEvent evt) {
        if (evt.getID() == ComponentEvent.COMPONENT_RESIZED) {
            calendarTable.setRowHeight(calendarTable.getHeight() / calendarTable.getRowCount());
        }
    }
});
lins314159
Alright, well the row size is working great, but even with the JScrollPane and getTableHeader I can't get the headers to dis play!JScrollPane scrollPane = new JScrollPane(calendarPanel);
Dalton Conley
working now! thanks! :) I simply needed to add the scrollPane to the panel rather than calendarTable
Dalton Conley