Win a copy of Head First Agile this week in the Agile forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Problems with JTable  RSS feed

 
David Houghton
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all, im relatively new to Java programming and im having some problems with Swing. I am messing around with creating tables and placing them into a container at Center position using BorderLayout manager. However nothing is showing up when i compile and run the project. i am using netBeans.

I have been able to place other components at other positions within the JFrame i have created. If anybody could help as to why my table isnt showing id be chuffed. Ive include code for my class which extands JFrame, DMMain, the class below it, TableCreator, extends JTable and creates the table as you may have guessed. The project is ran from a class Main which does nothing except create and initialize an instance of DMMain.

Thanks

Dave


// DMMain Class included below

import javax.swing.*;
import java.awt.*;


public class DMMain extends JFrame
{
// Instance Variables

private JFrame JFMain;
private Container cp;
private JMenuBar menu;
private JMenu fileMenu;
private JMenu helpMenu;
private JMenuItem fileOpen;
private JMenuItem fileExit;
private JMenuItem helpHelp;
private JMenuItem helpAbout;
private TableCreator tc;
private JPanel jp;


public DMMain()
{
JFMain = new JFrame();
cp = getContentPane();
cp.setLayout(new BorderLayout());
setLocation(20,20);
setSize(1000,700);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
initComponents();
buildInterface();
}

private void initComponents()
{
menu = new JMenuBar();
fileMenu = new JMenu("File");
helpMenu = new JMenu("Help");
fileOpen = new JMenuItem("Open");
fileOpen.addActionListener(new MenuListener());
fileExit = new JMenuItem("Exit");
helpHelp = new JMenuItem("Help");
helpAbout = new JMenuItem("About");
tc = new TableCreator();
tc.setVisible(true);
}

private void buildInterface()
{
menu.add(fileMenu);
menu.add(helpMenu);

fileMenu.add(fileOpen);
fileMenu.add(fileExit);

helpMenu.add(helpHelp);
helpMenu.add(helpAbout);

setJMenuBar(menu);
cp.add(tc.getTableHeader(), BorderLayout.PAGE_START);
cp.add(tc,BorderLayout.CENTER);
}
}

// TableCreator class below


import javax.swing.*;


public class TableCreator extends JTable
{


/** Creates a new instance of TableCreator */
public TableCreator()
{
//super();
String [] fieldNames = {"Forename","Surname","Tel Number"};
Object [][] data = {
{"David","Houghton","0000000000"},
{"George","Houghton","00000000000"}
};

JTable table = new JTable(data, fieldNames);
}

}



 
Jeff Storey
Ranch Hand
Posts: 230
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David,

You have a TableCreator class that extends JTable, but then inside your class you create another JTable. That is the issue here. You are adding the data to that variable table, but then you are adding the table creator to your frame. Rather than TableCreator extending a table, you may want to add a create() method that returns the created JTable. Then, you can add that table to the frame. In this case though, I'm not sure having a TableCreator class is really necessary since the data it is creating is hard coded anyway.

Hope this helps,
Jeff
 
Rob Spoor
Sheriff
Posts: 21095
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And please Use Code Tags the next time.
 
David Houghton
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Jeff, have seen the error of my ways and feel a bit stupid now! Must have been Tired.

Much appreciated,

Dave
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!