Board index » jbuilder » Column Headers Don't Show in Table

Column Headers Don't Show in Table


2003-11-21 01:23:53 AM
jbuilder17
I've created a table within a scroll pane, but the column headers don't show
up. They headers show up in the JBuilder9 IDE but when I run my application
the column headers don't show. The two rows of data that I created show up
fine. I've done other examples outside of JBuilder9 and column headers show
up fine. I feel like I,m missing something small like there's a parameter
I'm not setting to enable the column headers ot something.
I have a file called StigmergyTable Model.java:
import javax.swing.table.*;
public class StigmergyTableModel extends AbstractTableModel{
private String[] columnNames;
private Object[][] data;
// StigmergyTableModel Class constructor
public StigmergyTableModel( String[] cNames, Object[][] dataArg ) {
columnNames = cNames;
data = dataArg;
}
public int getColumnCount() {
return columnNames.length;
}
public int getRowCount() {
return data.length;
}
public Object getValueAt(int row, int col) {
return data[row][col];
}
public String getColumnName(int col) {
return columnNames[col];
}
} // End class StigmergyTableModel
Another File where the above table model is instantiated.:
import javax.swing.*;
import java.awt.*;
import javax.swing.border.*;
import java.awt.event.*;
import javax.swing.table.*;
...
public class SimulationBuilder extends JPanel {
...
JPanel buildingAgentPanel = new JPanel();
...
String[] columnNames = new String[]
{"FileName","Path","Test","Test1","Test","Test1"};
Object[][] data = new Object[][] {
{"somethigelse.txt","C:\\Folder","Test","Test1","Test","Test1"},
{"somethigelse2.txt","C:\\Folder","Test","Test1","Test","Test1"}
};
StigmergyTableModel agentTableModel = new
StigmergyTableModel(columnNames,data);
JTable agentTable = new JTable(agentTableModel);
JScrollPane agentScrollPane = new JScrollPane(agentTable);
public SimulationBuilder() {
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
private void jbInit() throws Exception {
...
agentTable.setToolTipText("");
agentTable.setTableHeader(null);
agentTable.setPreferredScrollableViewportSize(new Dimension(50,
50));
agentScrollPane.add(agentTable, null);
agentScrollPane.setViewportView(agentTable);
agentScrollPane.setAutoscrolls(true);
agentScrollPane.setOpaque(true);
agentScrollPane.setBounds(new Rectangle(24, 73, 315, 99));
buildingAgentPanel.add(agentScrollPane, null);
}
}
 
 

Re:Column Headers Don't Show in Table

"Matthew Sweet" < XXXX@XXXXX.COM >wrote in message
Quote
agentTable.setToolTipText("");
agentTable.setTableHeader(null);
agentTable.setPreferredScrollableViewportSize(new
Dimension(50,
50));
agentScrollPane.add(agentTable, null);
agentScrollPane.setViewportView(agentTable);
agentScrollPane.setAutoscrolls(true);
agentScrollPane.setOpaque(true);
agentScrollPane.setBounds(new Rectangle(24, 73, 315, 99));
buildingAgentPanel.add(agentScrollPane, null);
The table headers are displayed by a separate component, and there are
two reasons why it isn't displaying (not sure why it shows in the
designer).
First, you're not adding the table header component to your UI. You
should probably add it as the scroll pane's column header.
agentScrollPane.setColumnView(agentTable.getTableHeader());
Second, you're setting the table header to null, so in this case there
isn't actually a table header to add to the scroll pane. You should
remove the line where you set the table header to null.
--
Gillmer J. Derge (TeamB)
 

Re:Column Headers Don't Show in Table

Thanks. It's pretty obvious now. I think I was just looking at it too long
and the IDE was causing confusion.
Originally I had everything except for the
agentScrollPane.setViewportView(agentTable) and couldn't see my table at
all. Then I added the viewport part and could see the table when I ran my
application, but no header. For some reason I didn't think that I might have
to remove something rather than add something.
Thanks for your help.
Matthew Sweet
"Gillmer J. Derge (TeamB)" < XXXX@XXXXX.COM >wrote in message
Quote
"Matthew Sweet" < XXXX@XXXXX.COM >wrote in message
news: XXXX@XXXXX.COM ...
>agentTable.setToolTipText("");
>agentTable.setTableHeader(null);
>agentTable.setPreferredScrollableViewportSize(new
Dimension(50,
>50));
>agentScrollPane.add(agentTable, null);
>agentScrollPane.setViewportView(agentTable);
>agentScrollPane.setAutoscrolls(true);
>agentScrollPane.setOpaque(true);
>agentScrollPane.setBounds(new Rectangle(24, 73, 315, 99));
>buildingAgentPanel.add(agentScrollPane, null);

The table headers are displayed by a separate component, and there are
two reasons why it isn't displaying (not sure why it shows in the
designer).

First, you're not adding the table header component to your UI. You
should probably add it as the scroll pane's column header.

agentScrollPane.setColumnView(agentTable.getTableHeader());

Second, you're setting the table header to null, so in this case there
isn't actually a table header to add to the scroll pane. You should
remove the line where you set the table header to null.

--
Gillmer J. Derge (TeamB)

 

{smallsort}