I am trying to create a JTable that is a 3X3 grid. I have managed to get a JTable to display on the screen. However, I cannot get the JTable to have just 3 columns and 3 rows. Can someone please help.
The code is as follows:
import javax.swing.*;
import javax.swing.table.*;
import java.awt.*;
import java.awt.event.*;
public class Frame1 extends JFrame{
private String [] colHeads ={ "Planet", "Radius", "Moons","Gaseous","Color"};
private Object[] cells ={"Mercury",new Double(2440),new Integer(0),Boolean.FALSE, Color.yellow};
private DefaultTableModel model = new DefaultTableModel(colHeads,0);
private JTable table = new JTable(model);
private JScrollPane scroll = new JScrollPane(table);
private JTabbedPane tabbedPane = new JTabbedPane();
private JScrollPane mainScroll = new JScrollPane(tabbedPane);
private JPanel topPane= new JPanel();
private JPanel outerPane = new JPanel();
private JPanel buttomPane = new JPanel();
public static void main(String[] args){
JFrame f = new Frame1();
f.show();
}
public Frame1(){
setSize(900,900);
setVisible(true);
Container contentPane = getContentPane();
tabbedPane.addTab("Log in",outerPane);
outerPane.setLayout(new BorderLayout() );
buttomPane.add(new Button() );
topPane.setSize(200,200);
outerPane.add(topPane,"North"
;
outerPane.add(buttomPane, "South"
;
topPane.add(scroll);
outerPane.setBorder( BorderFactory.createEtchedBorder() );
topPane.setBorder( BorderFactory.createEtchedBorder() );
buttomPane.setBorder( BorderFactory.createEtchedBorder() );
contentPane.add(mainScroll);
for (int i=0;i<100;i++)
model.addRow(cells);
}
}
------------
Thank you
The code is as follows:
import javax.swing.*;
import javax.swing.table.*;
import java.awt.*;
import java.awt.event.*;
public class Frame1 extends JFrame{
private String [] colHeads ={ "Planet", "Radius", "Moons","Gaseous","Color"};
private Object[] cells ={"Mercury",new Double(2440),new Integer(0),Boolean.FALSE, Color.yellow};
private DefaultTableModel model = new DefaultTableModel(colHeads,0);
private JTable table = new JTable(model);
private JScrollPane scroll = new JScrollPane(table);
private JTabbedPane tabbedPane = new JTabbedPane();
private JScrollPane mainScroll = new JScrollPane(tabbedPane);
private JPanel topPane= new JPanel();
private JPanel outerPane = new JPanel();
private JPanel buttomPane = new JPanel();
public static void main(String[] args){
JFrame f = new Frame1();
f.show();
}
public Frame1(){
setSize(900,900);
setVisible(true);
Container contentPane = getContentPane();
tabbedPane.addTab("Log in",outerPane);
outerPane.setLayout(new BorderLayout() );
buttomPane.add(new Button() );
topPane.setSize(200,200);
outerPane.add(topPane,"North"
outerPane.add(buttomPane, "South"
topPane.add(scroll);
outerPane.setBorder( BorderFactory.createEtchedBorder() );
topPane.setBorder( BorderFactory.createEtchedBorder() );
buttomPane.setBorder( BorderFactory.createEtchedBorder() );
contentPane.add(mainScroll);
for (int i=0;i<100;i++)
model.addRow(cells);
}
}
------------
Thank you