How to make columnheaders in JTable visible

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);

}

[1325 byte] By [n303ya] at [2007-11-27 6:56:12]
# 1
Don't forget to use the [url http://forum.java.sun.com/help.jspa?sec=formatting]Code Formatting Tags[/url] so the posted code retains its original formatting.Add the table to a scroll pane and the scroll pane to the frame.
camickra at 2007-7-12 18:33:05 > top of Java-index,Desktop,Core GUI APIs...
# 2
I added two lines below "jTable1.setModel(model);" but now my table isn't even filled. Any idea?JScrollPane myScrollPane = new JScrollPane(jTable1);;this.getContentPane().add(myScrollPane);
n303ya at 2007-7-12 18:33:05 > top of Java-index,Desktop,Core GUI APIs...
# 3
The two line posted will not affect the data in the table.
camickra at 2007-7-12 18:33:05 > top of Java-index,Desktop,Core GUI APIs...
# 4

Here is the whole class. The table is presented in a scrollpane but i still don't see the headers. 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:33:05 > top of Java-index,Desktop,Core GUI APIs...
# 5
Hi!,Where do I add a line to my code to make the columnheaders visible?Thanks in advance.
n303ya at 2007-7-12 18:33:05 > top of Java-index,Desktop,Core GUI APIs...
# 6

See reply 1.

Read the JTable API. You will find a link to the Swing tutorial on "How to Use Tables" where you will find some working examples.

Then is no need to build the columns manually. When you add the TableModel to the tabel the columns will be bult automatically.

If you are having problems, then hardcode your data first to prove that it works. Then work on using dynamic code from a database. Learn to walk before you run.

camickra at 2007-7-12 18:33:05 > top of Java-index,Desktop,Core GUI APIs...