Where are my columnheader in JTable with model?

I have drawn a JTable in a form with JBuilder and use the following code. The table is fiiled properly but I don't see the columnheaders.

How can i make them visible?

DefaultTableModel model = new DefaultTableModel();

model.addColumn("Bezoeker_id");

model.addColumn("Voorletters");

model.addColumn("Tussenvoegsel");

model.addColumn("Achternaam");

model.addColumn("Adres");

model.addColumn("Postcode");

model.addColumn("Woonplaats");

model.addColumn("Telefoonnummer");

DataInputStream dis = null;

String record = null;

int recCount = 0;

try

{

FileInputStream fis = new FileInputStream("C:\\BEZOEKER.dat");

BufferedInputStream bis = new BufferedInputStream(fis);

dis = new DataInputStream(bis);

while ( (record=dis.readLine()) != null )

{

if (recCount > 0)

{

String[] items = record.split("\t");

model.addRow(items);

}

recCount++;

}

}

catch (IOException ie)

{

}

finally

{

// if the file opened okay, make sure we close it

if (dis != null)

{

try

{

dis.close();

}

catch (IOException ioe)

{

}

}

}

jTable1.setModel(model);

}

[b/]

[1350 byte] By [n303ya] at [2007-11-27 6:56:02]
# 1

Can you please tell my why you're using a DataInputStream?

And I suppose you should either read the JBuilder manual if you want to handle the JTable for you, or you might coinsider it doing yourself, going by this tutorial: http://java.sun.com/docs/books/tutorial/uiswing/components/table.html

By the way, we have a Swing forum. People there are more likely to know an answer to your Swing questions.

CeciNEstPasUnProgrammeura at 2007-7-12 18:32:50 > top of Java-index,Java Essentials,New To Java...
# 2
It is just a piece of code that I found somewhere. Why, do you have a better solution?I read the JTable part of the Jbuilder manual which is the same as the link you posted but all the examples assumes that the "design-mode" is not used.
n303ya at 2007-7-12 18:32:50 > top of Java-index,Java Essentials,New To Java...
# 3
If you don't put the JTable inside a JScrollPane then you don't get column headers. I can't tell from your code whether you are doing that.
DrClapa at 2007-7-12 18:32:50 > top of Java-index,Java Essentials,New To Java...
# 4

Here is the whole class. The JTabvle is inside a scrollpane but the columnheader still aren't visible. Any idea?

package thuiszorg;

import java.awt.BorderLayout;

import javax.swing.JFrame;

import javax.swing.JButton;

import java.awt.*;

import javax.swing.JTable;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.io.*;

import java.util.ArrayList;

import javax.swing.table.DefaultTableModel;

import javax.swing.JScrollPane;

import javax.swing.JPanel;

import javax.swing.table.*;

import javax.swing.JScrollBar;

import javax.swing.JPanel;

/**

*

Title: Thuiszorg

*

*

Description:

*

*

Copyright: Copyright (c) 2007

*

*

Company: Avans

*

* @author DT2-I4-Groep2

* @version 1.0

*/

public class FrameMain extends JFrame {

public FrameMain() {

try {

jbInit();

} catch (Exception exception) {

exception.printStackTrace();

}

}

private void jbInit() throws Exception {

getContentPane().setLayout(null);

this.setJMenuBar(null);

this.setResizable(false);

this.setState(Frame.ICONIFIED);

this.setTitle("Test");

jButton2.setBounds(new Rectangle(20, 120, 120, 23));

jButton2.setText("Hulpmiddelen");

jButton1.setBounds(new Rectangle(19, 218, 120, 23));

jButton1.setText("Facturen");

jButton4.setBounds(new Rectangle(18, 186, 120, 23));

jButton4.setText("Verhuren");

jButton3.setBounds(new Rectangle(20, 154, 120, 23));

jButton3.setText("Hulpmiddelsoorten");

cmdBezoekers.addActionListener(new FrameMain_cmdBezoekers_actionAdapter(this));

jTable1.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);

scrollPane1.setBounds(new Rectangle(181, 86, 590, 358));

this.getContentPane().add(cmdBezoekers);

this.getContentPane().add(jButton2);

this.getContentPane().add(jButton3);

this.getContentPane().add(jButton4);

this.getContentPane().add(jButton1);

this.getContentPane().add(scrollPane1);

scrollPane1.add(jTable1);

cmdBezoekers.setBounds(new Rectangle(18, 85, 120, 23));

cmdBezoekers.setText("Bezoekers");

}

public static void main(String[] args) {

FrameMain framemain = new FrameMain();

framemain.setSize(800, 600);

framemain.show();

}

JButton jButton2 = new JButton();

JButton cmdBezoekers = new JButton();

JButton jButton3 = new JButton();

JButton jButton4 = new JButton();

JButton jButton1 = new JButton();

JTable jTable1 = new JTable();

ScrollPane scrollPane1 = new ScrollPane();

public void cmdBezoekers_actionPerformed(ActionEvent e) {

ArrayList arr = new ArrayList();

ArrayList arrItem = new ArrayList();

//DefaultTableModel model = new DefaultTableModel();

DefaultTableModel model = new DefaultTableModel()

{

public boolean isCellEditable(int row, int column) {

return false ;

}

};

model.addColumn("Bezoeker_id");

model.addColumn("Voorletters");

model.addColumn("Tussenvoegsel");

model.addColumn("Achternaam");

model.addColumn("Adres");

model.addColumn("Postcode");

model.addColumn("Woonplaats");

model.addColumn("Telefoonnummer");

DefaultTableModel f = (DefaultTableModel)jTable1.getModel();

f.addColumn("YourName1");

f.addColumn("YourName2");

f.addColumn("YourName3");

int listCount = 0;

DataInputStream dis = null;

String record = null;

int recCount = 0;

try {

FileInputStream fis = new FileInputStream("C:\\BEZOEKER.dat");

BufferedInputStream bis = new BufferedInputStream(fis);

dis = new DataInputStream(bis);

while ((record = dis.readLine()) != null) {

if (recCount > 0) {

Bezoeker persoon = new Bezoeker();

String[] items = record.split("\t");

// persoon.SetBezoekerId((int)items[0]);

persoon.SetVoorletters(items[1]);

persoon.SetTussenvoegsel(items[2]);

persoon.SetAchternaam(items[3]);

persoon.SetAdres(items[4]);

persoon.SetPostcode(items[5]);

persoon.SetWoonplaats(items[6]);

persoon.SetTelefoonNummer(items[7]);

model.addRow(items);

}

recCount++;

}

} catch (IOException ie) {

} finally {

// if the file opened okay, make sure we close it

if (dis != null) {

try {

dis.close();

} catch (IOException ioe) {

}

}

}

jTable1.setModel(model);

//JScrollPane myScrollPane = new JScrollPane(jTable1);;

this.getContentPane().add(scrollPane1);

}

}

class FrameMain_cmdBezoekers_actionAdapter implements ActionListener {

private FrameMain adaptee;

FrameMain_cmdBezoekers_actionAdapter(FrameMain adaptee) {

this.adaptee = adaptee;

}

public void actionPerformed(ActionEvent e) {

adaptee.cmdBezoekers_actionPerformed(e);

}

}

n303ya at 2007-7-12 18:32:50 > top of Java-index,Java Essentials,New To Java...
# 5

Get rid of that, which doesn't seem to have anything to do with anything:

DefaultTableModel f = (DefaultTableModel)jTable1.getModel();

f.addColumn("YourName1");

f.addColumn("YourName2");

f.addColumn("YourName3");

What is this for? delete it and use a proper layout manager:

getContentPane().setLayout(null);

You seem to be calling

this.getContentPane().add(scrollPane1);

twice.

I would add a jtable1.revalidate() in there after calling jTable1.setModel().

ejpa at 2007-7-12 18:32:50 > top of Java-index,Java Essentials,New To Java...