• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

Problems with JTable

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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);
}

}



 
Ranch Hand
Posts: 230
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 22684
128
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And please Use Code Tags the next time.
 
David Houghton
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Of course, I found a very beautiful couch. Definitely. And this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic