-
June 27th, 2001, 09:46 AM
#1
Column Headers in JTable
Hello again. I am still having problems with JTable (god I can't wait until the book I ordered gets here!). My problem is this:
I have used the RowHeaderRenderer code found at the website http://www2.gol.com/users/tame/swing...gExamples.html and I thank the person who wrote it immensely. I have implemented this into an application that I am writing, and everything works fine with one minor exception. I can't seem to get Column Headers to display on the fixed table (the row headers). I use a test form to try out code, and when I get it to work I cut and paste (it is easier to run a JFrame than it is to launch a JFrame and then run a JInternalFrame and then click this button and then that button just to get a new JInternalFrame to load and... you get the idea). Here is the code for the test form and the JTable that I am trying to create:
/*
* Test.java
*
* Created on June 26, 2001, 9:01 AM
*/
package salon.frames;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
import javax.swing.table.*;
import javax.swing.event.*;
import salon.tables.*;
import salon.misc.*;
/**
*
* @author root
* @version
*/
public class Test extends javax.swing.JFrame {
/** Creates new form Test */
public Test() {
initComponents ();
pack ();
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the FormEditor.
*/
private void initComponents() {//GEN-BEGIN:initComponents
addWindowListener(new java.awt.event.WindowAdapter() {
public void windowOpened(java.awt.event.WindowEvent evt) {
formWindowOpened(evt);
}
public void windowClosing(java.awt.event.WindowEvent evt) {
exitForm(evt);
}
}
);
}//GEN-END:initComponents
private void formWindowOpened(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowOpened
// Add your handling code here:
DefaultTableModel model = new DefaultTableModel(50, 36);
JTable table = new JTable(model) {
protected JTableHeader createDefaultTableHeader() {
return new GroupableTableHeader(columnModel);
}
};
table.setRowMargin(0);
TableColumnModel cm = table.getColumnModel();
ColumnGroup g1 = new ColumnGroup("8:00");
ColumnGroup g2 = new ColumnGroup("9:00");
ColumnGroup g3 = new ColumnGroup("10:00");
ColumnGroup g4 = new ColumnGroup("11:00");
ColumnGroup g5 = new ColumnGroup("12:00");
ColumnGroup g6 = new ColumnGroup("1:00");
ColumnGroup g7 = new ColumnGroup("2:00");
ColumnGroup g8 = new ColumnGroup("3:00");
ColumnGroup g9 = new ColumnGroup("4:00");
cm.setColumnMargin(0);
g1.add(cm.getColumn(0));
g1.add(cm.getColumn(1));
g1.add(cm.getColumn(2));
g1.add(cm.getColumn(3));
g2.add(cm.getColumn(4));
g2.add(cm.getColumn(5));
g2.add(cm.getColumn(6));
g2.add(cm.getColumn(7));
g3.add(cm.getColumn(8));
g3.add(cm.getColumn(9));
g3.add(cm.getColumn(10));
g3.add(cm.getColumn(11));
g4.add(cm.getColumn(12));
g4.add(cm.getColumn(13));
g4.add(cm.getColumn(14));
g4.add(cm.getColumn(15));
g5.add(cm.getColumn(16));
g5.add(cm.getColumn(17));
g5.add(cm.getColumn(18));
g5.add(cm.getColumn(19));
g6.add(cm.getColumn(20));
g6.add(cm.getColumn(21));
g6.add(cm.getColumn(22));
g6.add(cm.getColumn(23));
g7.add(cm.getColumn(24));
g7.add(cm.getColumn(25));
g7.add(cm.getColumn(26));
g7.add(cm.getColumn(27));
g8.add(cm.getColumn(28));
g8.add(cm.getColumn(29));
g8.add(cm.getColumn(30));
g8.add(cm.getColumn(31));
g9.add(cm.getColumn(32));
g9.add(cm.getColumn(33));
g9.add(cm.getColumn(34));
g9.add(cm.getColumn(35));
GroupableTableHeader header = (GroupableTableHeader) table.getTableHeader();
header.addColumnGroup(g1);
header.addColumnGroup(g2);
header.addColumnGroup(g3);
header.addColumnGroup(g4);
header.addColumnGroup(g5);
header.addColumnGroup(g6);
header.addColumnGroup(g7);
header.addColumnGroup(g8);
header.addColumnGroup(g9);
Object data[][] = new Object[50][50];
Object column[] = {"Name", "Level"};
AttributiveCellTableModel fixedModel = new AttributiveCellTableModel(column, 50) {
public boolean CellEditable(int row, int col) {
return false;
}
};
MultiSpanCellTable fixedTable = new MultiSpanCellTable(fixedModel);
TableColumn col = null;
for (int i = 0; i < 2; i++)
{
col = fixedTable.getColumnModel().getColumn(i);
switch(i)
{
case 0 : col.setMinWidth(175);
break;
case 1 : col.setMinWidth(75);
break;
}
}
fixedTable.setRowMargin(0);
fixedTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
fixedTable.setDefaultRenderer(Object.class, new RowHeaderRenderer(fixedTable));
fixedTable.setGridColor(table.getTableHeader().getBackground());
table.setGridColor(Color.lightGray);
BorderCellRenderer renderers = new BorderCellRenderer();
TableColumnModel cols = table.getColumnModel();
renderers.setColumnBorder(new javax.swing.border.LineBorder(table.getGridColor(), 1));
for (int x = 0; x < table.getColumnCount(); x++)
{
cols.getColumn(x).setCellRenderer(renderers);
}
renderers = createRenderer(Color.black, new Insets(1, 1, 1, 1));
int y = 0;
while (y < table.getColumnCount())
{
cols.getColumn(y).setCellRenderer(renderers);
y = y + 4;
}
JScrollPane scroll = new JScrollPane(table);
JViewport viewport = new JViewport();
viewport.setView(fixedTable);
viewport.setPreferredSize(fixedTable.getPreferredSize());
scroll.setRowHeaderView(viewport);
getContentPane().add(scroll, BorderLayout.CENTER);
setSize(400, 400);
header.revalidate();
}//GEN-LAST:event_formWindowOpened
/** Exit the Application */
private void exitForm(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_exitForm
System.exit (0);
}//GEN-LAST:event_exitForm
/**
* @param args the command line arguments
*/
public static void main (String args[]) {
new Test ().show ();
}
class RowHeaderRenderer extends JLabel implements TableCellRenderer {
RowHeaderRenderer(JTable table) {
JTableHeader header = table.getTableHeader();
setOpaque(true);
setBorder(UIManager.getBorder("TableHeader.cellBorder"));
setHorizontalAlignment(CENTER);
setForeground(header.getForeground());
setBackground(header.getBackground());
setFont(header.getFont());
}
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int row, int column) {
setText((value == null) ? "" : value.toString());
return this;
}
}
private static BorderCellRenderer createRenderer(Color color, Insets inset) {
BorderCellRenderer renderer = new BorderCellRenderer();
renderer.setColumnBorder(new LinesBorder(color, inset));
return renderer;
}
// Variables declaration - do not modify//
// End of variables declaration//
}
So, does anyone know why I can't get column headers to display in the fixed table? I have tried
TableColumn col = null;
for (int x = 0; x < fixedTable.getColumnCount(); x++)
{
col = fixedTable.getColumnModel().getColumn(x);
col.setHeaderValue(column[x]);
}
but this doesn't seem to work. Any ideas out there? Any at all?
-
June 27th, 2001, 11:06 AM
#2
Re: Column Headers in JTable
I'll be a monkey's uncle. I solved it. But how was I supposed to know that
scroll.setCorner(JScrollPane.UPPER_LEVT_CORNER, fixedTable.getTableHeader());
was the answer? Where are all the good java reference materials?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|