Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Help with the Layout  RSS feed

 
dave vanweed
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello everyone
could someone please help me?
I'm having trouble puuting my JTable in the middle of my JFrame and also to the right of the JTable i want two text fields could someone give me some code so that i get the idea of how to work with the border layout.
Thankyou all so much
davey
 
ali rafiq
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
import javax.swing.*;
import javax.swing.JScrollPane;
import javax.swing.JPanel;
import javax.swing.JFrame;
import java.awt.*;
import java.awt.event.*;
public class si extends javax.swing.JFrame {
JTextField name = new JTextField(15);
JTextField name1 = new JTextField(15);
public si() {
super("DataBase Loader");
setSize(1025,740);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


JPanel pane = new JPanel();
JPanel pane1 = new JPanel();
JPanel pane2 = new JPanel();
JPanel pane3 = new JPanel();

pane.setLayout(new GridLayout(20,1));

pane1.setLayout(new BorderLayout());
String [][] data = {{"",""},{"",""},{"",""},{"",""},{"",""},};
String[] columnNames = {"First Name", "Last Name"};
System.out.println(data.length);
JTable perstab = new JTable(data, columnNames);
perstab.setGridColor(Color.yellow);
perstab.setPreferredScrollableViewportSize(new Dimension(200,300));
JScrollPane scrollPane = new JScrollPane(perstab);
pane2.add(name);
pane3.add(name1);
pane.add(pane2);
pane.add(pane3);
pane1.add(pane, BorderLayout.WEST);

//pane.add(pane1);
pane1.add(scrollPane, BorderLayout.CENTER);
//con.add(pane1);
setContentPane(pane1);
show();
}
public static void main(String[] args) {
si frame = new si();
frame.setVisible(true);
}}//frame.pack();scrollPane.setBounds();

im sure that was u talking about.. if im not then this calss may helpfull for you to how to manage



import javax.swing.*;
import java.awt.*;
public class JPanef extends JFrame {
JPanel topPanel6 = new JPanel();
JButton but4 = new JButton("South");
JButton but5 = new JButton("North");
JButton but1 = new JButton("Center");
JButton but2 = new JButton("East");
JButton but3 = new JButton("West");
public JPanef() {
topPanel6.setLayout(new BorderLayout());
topPanel6.add(but1 ,BorderLayout.CENTER);
topPanel6.add(but2 ,BorderLayout.NORTH);
topPanel6.add(but3 ,BorderLayout.EAST);
topPanel6.add(but4, BorderLayout.WEST);
topPanel6.add(but5, BorderLayout.SOUTH);

setContentPane(topPanel6);
setSize( 300 , 200 );
}

public static void main( String args[] ){
JPanef JPanea = new JPanef();
JPanea.setVisible( true );
}
}
 
dave vanweed
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cheers mate the first example was the one that i needed.
But how can i make the size of the JTable smaller so that it is not the whole size of the center? cause at the moment it covers the whole center but i only want it in a small part of the center.
Thanks again
davey
 
ali rafiq
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ad this line into ur code Jtabel Will not contain the whole center..i think so
pane1.add(new JPanel(), BorderLayout.EAST);
pane1.add(new JPanel(), BorderLayout.SOUTH);
 
dave vanweed
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello again thanks for the code.
You are right it does not contain the whole center but it still takes up a large amount of space. How can i make the JTable smaller than what it is at the moment?
thanks again
davey
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the setPreferredScrollableViewportSize() method of JTable sets up the preferred size of the scrollpane that holds the table. Might this be the method you are looking for?

Though for this to work, you'll have to add the table to the scrollpane, add the scrollpane to a panel ( with Centered FlowLayout ), and then add the panel to the center section of the BorderLayout. Under BorderLayout, the center component is always made as large as possible.
[ February 10, 2003: Message edited by: Nathan Pruett ]
 
ali rafiq
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
import javax.swing.*;
import javax.swing.JScrollPane;
import javax.swing.JPanel;
import javax.swing.JFrame;
import java.awt.*;
import java.awt.event.*;
public class si extends javax.swing.JFrame {
JTextField name = new JTextField(15);
JTextField name1 = new JTextField(15);
JButton jb = new JButton("OK");
JButton jb1 = new JButton("Reset");

public si() {
super("DataBase Loader");
setSize(1025,740);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JPanel pane = new JPanel();
JPanel pane1 = new JPanel();
JPanel pane2 = new JPanel();
JPanel pane3 = new JPanel();
JPanel pane4 = new JPanel();
JPanel pane5 = new JPanel();
JPanel pane6 = new JPanel();
pane6.add(jb);
pane6.add(jb1);

pane.setLayout(new GridLayout(15,1));

pane1.setLayout(new BorderLayout());
String [][] data = {{"",""},{"",""},{"",""},{"",""},{"",""},};
String[] columnNames = {"First Name", "Last Name"};
System.out.println(data.length);
JTable perstab = new JTable(data, columnNames);
perstab.setGridColor(Color.yellow);
perstab.setPreferredScrollableViewportSize(new Dimension(200,300));
JScrollPane scrollPane = new JScrollPane(perstab);
pane2.add(name);
pane3.add(name1);
pane.add(pane2);
pane.add(pane3);
pane.add(pane6);
pane1.add(pane, BorderLayout.WEST);
JPanel pa = new JPanel();
//pa.setLayout(new GridLayout(1,3));
JPanel[] ap = new JPanel[10];
for(int i = 0; i<= 9 ; i++)
{
ap[i] = new JPanel();
pa.add(ap[i]);
}
pane1.add(pa, BorderLayout.EAST);
pane1.add(scrollPane, BorderLayout.CENTER);
jb1.addActionListener( new ActionListener() {
public void actionPerformed( ActionEvent evt ) {
name1.setText("");
name.setText("");

}});
setContentPane(pane1);
show();
}
public static void main(String[] args) {
si frame = new si();
frame.setVisible(true);
}}//frame.pack();scrollPane.setBounds();


it will be at ur requitment but..it has contain lot of panel.. and panel slow the frame.. if u need more smaller the table then just add more panel at the array of panel its 10.. if u add more then 10 then table will be small..
sorry i have make a button on it . i was just practesing
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!