• 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
  • Paul Clapham
  • Ron McLeod
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Jj Roberts
  • Al Hobbs
  • Piet Souris

JTable in JFrame

 
Ranch Hand
Posts: 3851
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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();
}
}
 
Ranch Hand
Posts: 138
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello rathi

Change frame.getContentPane().add(panel, BorderLayout.CENTER);
to be
frame.getContentPane().add(scrollPane, BorderLayout.CENTER);
 
ankur rathi
Ranch Hand
Posts: 3851
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ,
 
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3851
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
vavilipalli , pet already suggested the same solution .
But read my second reply !!
Anyway, Thanks
 
ravi v kumar
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3851
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
check once,

here its is working for me
 
Ranch Hand
Posts: 704
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have rewritten vavilipalli's code using a constructor rather than the show method to initialize the JTable.
 
ravi v kumar
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
did u got ur problem solved...?
 
ankur rathi
Ranch Hand
Posts: 3851
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 .
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic