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