File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes JTable in JFrame Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JTable in JFrame" Watch "JTable in JFrame" New topic
Author

JTable in JFrame

ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3830
Hi everybody ,

Plz solve my problem . I want to display table on a frame & I am able to display but not with proper size & position .
How can I do this ? Plz provide code if possible

Here is the code :


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

public class SimpleTableDemo extends JFrame {

public void show() {

Object[][] data = {
{"Mary", "Campione",
"Snowboarding", new Integer(5), new Boolean(false)},
{"Alison", "Huml",
"Rowing", new Integer(3), new Boolean(true)},
{"Kathy", "Walrath",
"Chasing toddlers", new Integer(2), new Boolean(false)},
{"Mark", "Andrews",
"Speed reading", new Integer(20), new Boolean(true)},
{"Angela", "Lih",
"Teaching high school", new Integer(4), new Boolean(false)}
};

String[] columnNames = {"First Name",
"Last Name",
"Sport",
"# of Years",
"Vegetarian"};

JTable table = new JTable(data, columnNames);
JScrollPane scrollPane = new JScrollPane(table);
JPanel panel = new JPanel();
panel.add(scrollPane);
JFrame frame = new JFrame();
frame.getContentPane().add(panel, BorderLayout.CENTER);
frame.setSize(900,200);
frame.setVisible(true);

}


public static void main(String[] args) {
SimpleTableDemo frame = new SimpleTableDemo();
frame.show();
}
}
Pat Hays
Ranch Hand

Joined: Aug 20, 2004
Posts: 138
Hello rathi

Change frame.getContentPane().add(panel, BorderLayout.CENTER);
to be
frame.getContentPane().add(scrollPane, BorderLayout.CENTER);


Download Java GUI Builder, <a href="http://www.mars3000.com" target="_blank" rel="nofollow">http://www.mars3000.com</a>
ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3830
No pet ,
It is not solving my problem .

My final aim is to display all the records of a particular student on a frame . Imagine the frame would be like this :
On the top-middle there will be student name in big font . then in middle a table includes all the marks of that student .after then in bottom some more details about student & a button that will have the functionality of getting printout of this frame .

Accrding to ur solution I am able to control position of table( not completely ) but not the size like the table should escape some space from the both side ( left & right ) .

Any body if have any solution then post ASAP .
Thanks ,
ravi v kumar
Ranch Hand

Joined: Dec 14, 2004
Posts: 56
import javax.swing.*;
import java.awt.*;
//import java.awt.event.*;

public class TableFrame extends JFrame {

public void show() {

Object[][] data = {
{"Mary", "Campione",
"Snowboarding", new Integer(5), new Boolean(false)},
{"Alison", "Huml",
"Rowing", new Integer(3), new Boolean(true)},
{"Kathy", "Walrath",
"Chasing toddlers", new Integer(2), new Boolean(false)},
{"Mark", "Andrews",
"Speed reading", new Integer(20), new Boolean(true)},
{"Angela", "Lih",
"Teaching high school", new Integer(4), new Boolean(false)}
};

String[] columnNames = {"First Name",
"Last Name",
"Sport",
"# of Years",
"Vegetarian"};

JTable table = new JTable(data, columnNames);
JScrollPane scrollPane = new JScrollPane(table);
//JPanel panel = new JPanel();
//add(scrollPane);
JFrame frame = new JFrame();
frame.getContentPane().add(scrollPane, BorderLayout.CENTER);
frame.setSize(900,200);
frame.setVisible(true);

}


public static void main(String[] args) {
TableFrame frame = new TableFrame();
frame.show();
}
}


use this code which displays as u required

with regards ravi
ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3830
vavilipalli , pet already suggested the same solution .
But read my second reply !!
Anyway, Thanks
ravi v kumar
Ranch Hand

Joined: Dec 14, 2004
Posts: 56
run this and extend this code as per as ur requirement
........may this might be helpful.............

bye


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

public class Rajit extends JFrame {
JTable table = null;
JPanel panel = new JPanel();
JScrollPane scrollPane=null;
JTextField namevalue=null;
JLabel name=null;
public void show() {

Object[][] data = {
{"Mary", "Campione",
"Snowboarding", new Integer(5), new Boolean(false)},
{"Alison", "Huml",
"Rowing", new Integer(3), new Boolean(true)},
{"Kathy", "Walrath",
"Chasing toddlers", new Integer(2), new Boolean(false)},
{"Mark", "Andrews",
"Speed reading", new Integer(20), new Boolean(true)},
{"Angela", "Lih",
"Teaching high school", new Integer(4), new Boolean(false)}
};

String[] columnNames = {"First Name",
"Last Name",
"Sport",
"# of Years",
"Vegetarian"};

table = new JTable(data, columnNames);
scrollPane = new JScrollPane(table);

panel.setLayout(new FlowLayout());
scrollPane.setLayout(new ScrollPaneLayout());
panel.add(scrollPane);

name=new JLabel();
namevalue=new JTextField(14);
name.setVisible(false);
namevalue.setVisible(false);
panel.add(name);
panel.add(namevalue);
JFrame frame = new JFrame();
frame.getContentPane().add(panel, BorderLayout.CENTER);
frame.setSize(900,200);
frame.setVisible(true);
table.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
printDebugData();
//validate();
//repaint();
}
});

}

private void printDebugData()
{
// name=new JLabel("name");
//JTextField namevalue=new JTextField();
String NameValue_strtable=table.getValueAt(table.getSelectedRow(),0).toString();
name.setText("Name :");
namevalue.setText(NameValue_strtable);
System.out.print(NameValue_strtable);
//panel.add(name);
//panel.add(namevalue);
name.setVisible(true);
namevalue.setVisible(true);
repaint();
}
public static void main(String[] args) {
Rajit frame = new Rajit();

frame.show();
}
}
ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3830
I have just tried your code but it is displaying just a dash in middle ( no table , no anything ) .
Please give one more try ...
Any help is welcome ...
ravi v kumar
Ranch Hand

Joined: Dec 14, 2004
Posts: 56
check once,

here its is working for me
Nigel Browne
Ranch Hand

Joined: May 15, 2001
Posts: 673
I have rewritten vavilipalli's code using a constructor rather than the show method to initialize the JTable.
ravi v kumar
Ranch Hand

Joined: Dec 14, 2004
Posts: 56
did u got ur problem solved...?
ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3830
Hi RK ,

when I comment this line then I am getting table & a textfield on a frame .
but then also my problem remains the same . I am not able to control size .

scrollPane.setLayout(new ScrollPaneLayout());

thanks .
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JTable in JFrame