开发者

Java Swing Table questions

开发者 https://www.devze.com 2022-12-27 13:56 出处:网络
Hey guys, working on an event calendar. I\'m having some trouble getting my column heads to display.. here is the code

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开发者_如何学C);
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?


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());
        }
    }
});
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号