Solved

JTable width

Posted on 2000-05-09
12
503 Views
Last Modified: 2007-12-19
How do you change the width of a JTable? I tried setPreferredSize() and setMinimumSize() but they don't seem to do what they are supposed to do.
0
Comment
Question by:m00
12 Comments
 
LVL 2

Expert Comment

by:AlexJ030200
ID: 2794384
Hi m00,

 setPreferredSize() and setMinimumSize() are hints for the layout manager. Unforttunately, not all of the layout managers follow this hints.

Use instead the  setSize(Dimension d) or the setSize(int width, int height) methods.
0
 

Author Comment

by:m00
ID: 2794628
Seems to have no effect on my JTable unfortunately...
0
 
LVL 16

Expert Comment

by:heyhey_
ID: 2795658
post your code (small compilable example)
0
 
LVL 1

Expert Comment

by:Ik_Khan_Pk
ID: 2795775
//Try this code

public static int WIDTH = 450;
public static int HEIGHT = 450;

JTable table = new JTable(cells,columns);
 {
  super(TITLE);
  addWindowListener(new WindowHandler());
  buildGUI();
  setSize(WIDTH,HEIGHT);
  setBackground(Color.darkGray);
  show();
 }
0
 
LVL 5

Expert Comment

by:mbormann
ID: 2796970
hey i have a similar kind of problem,

I am not able to set height of JTable after putting it into a GridBagLayout.
The 4 buttons should be vertically placed between 2 Jtables which are basically lists,the 2 add buttons and 2 remove button be grouped 1 after the other.

Can you guys help me on that ? I will post a new question for you ,here's my test code...

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

class Test extends JPanel{

      public static void main(String []args) {
            JFrame jf = new JFrame();
            jf.getContentPane().add(new Test());
            Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
            jf.setSize(dim.width,dim.height);
            jf.addWindowListener( new WindowAdapter() {
                  public void windowClosing(WindowEvent e) {
                        System.exit(0);
                  }
                  });
            jf.setVisible(true);
      }
      
      public Test() {
        table1 = new JTable();
            table2 = new JTable();

            GridBagLayout gbl = new GridBagLayout();
            GridBagConstraints gbc = new GridBagConstraints();

            JPanel top = new JPanel();
            top.setLayout(gbl);
            
            //1
            gbc.weightx = 0.5;
            gbc.weighty = 1.0;
            gbc.gridx = 0;
            gbc.gridy = 0;
            gbc.gridwidth = 1;
            gbc.gridheight = 2;
            gbc.insets = new Insets(0,0,0,0);
            gbc.fill = GridBagConstraints.BOTH;
            gbc.anchor = GridBagConstraints.CENTER;
            JScrollPane js1 = new JScrollPane(table1,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
            add(top,js1,gbl,gbc);

            //2
            gbc.weightx = 0.5;
            gbc.weighty = 1.0;
            gbc.gridx = 2;
            gbc.gridy = 0;
            gbc.gridwidth = 1;
            gbc.gridheight = 2;
            gbc.insets = new Insets(0,0,0,0);
            gbc.fill = GridBagConstraints.BOTH;
            gbc.anchor = GridBagConstraints.CENTER;
            JScrollPane js2 = new JScrollPane(table2,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
            add(top,js2,gbl,gbc);

            //3
            gbc.weightx = 0.0;
            gbc.weighty = 0.5;
            gbc.gridx = 1;
            gbc.gridy = 0;
            gbc.gridwidth = 1;
            gbc.gridheight = 1;
            gbc.insets = new Insets(2,4,2,4);
            gbc.fill = GridBagConstraints.NONE;
            gbc.anchor = GridBagConstraints.SOUTH;
            add(top,this.b_add,gbl,gbc);

            //4
            gbc.weightx = 0.0;
            gbc.weighty = 0.5;
            gbc.gridx = 1;
            gbc.gridy = 1;
            gbc.gridwidth = 1;
            gbc.gridheight = 1;
            gbc.insets = new Insets(2,4,2,4);
            gbc.fill = GridBagConstraints.NONE;
            gbc.anchor = GridBagConstraints.NORTH;
            add(top,this.b_addAll,gbl,gbc);
            
            //5
            gbc.weightx = 0.0;
            gbc.weighty = 0.5;
            gbc.gridx = 1;
            gbc.gridy = 2;
            gbc.gridwidth = 1;
            gbc.gridheight = 1;
            gbc.insets = new Insets(2,4,2,4);
            gbc.fill = GridBagConstraints.NONE;
            gbc.anchor = GridBagConstraints.SOUTH;
            add(top,this.b_remove,gbl,gbc);

            //6
            gbc.weightx = 0.0;
            gbc.weighty = 0.5;
            gbc.gridx = 1;
            gbc.gridy = 3;
            gbc.gridwidth = 1;
            gbc.gridheight = 1;
            gbc.insets = new Insets(2,4,2,4);
            gbc.fill = GridBagConstraints.NONE;
            gbc.anchor = GridBagConstraints.NORTH;
            add(top,this.b_removeAll,gbl,gbc);
                  
            this.add(top,BorderLayout.CENTER);

            JPanel buttons = new JPanel(new FlowLayout(FlowLayout.CENTER));
            buttons.add(b_back);
            buttons.add(b_next);
            
            this.add(buttons,BorderLayout.SOUTH);

            System.out.print("\nThe JTable has '"+table1.getRowCount()+ "' rows ");
            System.out.println("and '"+table1.getColumnCount()+ "' columns.");
            setVisible(true);
      }
      
      /**
       * This is a utility method to add a component to the Container using
       * GridBagLayout
       */
      
      public final void add(Container container,Component comp,GridBagLayout gbl,GridBagConstraints gbc) {
            // defaults
            gbc.ipadx = 0;
            gbc.ipady = 0;
            
            gbl.setConstraints(comp,gbc);
            container.add(comp);
      }
      public static final String ADD = "Add >>";
      public static final String REMOVE = "<< Remove";
      public static final String BACK = "Back";
      public static final String NEXT = "Next";
      public static final String ADDALL = "Add All";
      public static final String REMOVEALL = "Remove All";
      
      private JTable table1,table2;
      private JButton b_add = new JButton(ADD);
      private JButton b_remove = new JButton(REMOVE);
      private JButton b_back = new JButton(BACK);
      private JButton b_next = new JButton(NEXT);
      private JButton b_addAll = new JButton(ADDALL);
      private JButton b_removeAll = new JButton(REMOVEALL);
}
0
 

Author Comment

by:m00
ID: 2796985
import javax.swing.*;
import javax.swing.table.*;
import java.awt.*;
               
class JTableTest extends JFrame
{
        GridBagLayout gbLayout;
        GridBagConstraints gbConstraints;

        JTableTest()
        {
                setSize(800,600);
                JTable jTable = new JTable(new SomeModel());
                jTable.setSize(700,400);
                JScrollPane scrollPane = new JScrollPane(jTable);
                gbConstraints = new GridBagConstraints();
                getContentPane().setLayout(gbLayout = new
GridBagLayout());
                gbConstraints.anchor = GridBagConstraints.NORTHWEST;
                gbConstraints.fill = GridBagConstraints.BOTH;
                addComponent(scrollPane,0,0,1,1);
        }

        private void addComponent(Component c, int row, int column, int
width, int height)
        {
                // set gridx and gridy
                gbConstraints.gridx = column;
                gbConstraints.gridy = row;

                // set gridwidth and gridheight  
                gbConstraints.gridwidth = width;
                gbConstraints.gridheight = height;

                // set constraints
                gbLayout.setConstraints(c,gbConstraints);
                getContentPane().add(c);
        }

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

class SomeModel extends AbstractTableModel
{
        SomeModel()
        {      
                super();
        }

        public Object getValueAt(int row, int column)
        {
                return new Integer(row*column);
        }
 
        public int getColumnCount()
        {      
                return 4;
        }

        public int getRowCount()
        {
                return 100;
        }
}

JTable doesn't have width 700 in this example. 400 maybe, no more...
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 5

Expert Comment

by:mbormann
ID: 2797014
Sorry for distrubing u guys but I solved it by adding gbc.gridheight = 4; for the 2 jtables.

I had made it into a 3 columns by 4 rowsand considered the 2 jtables into 2 rows only!
0
 

Author Comment

by:m00
ID: 2797232
Adjusted points from 50 to 100
0
 

Author Comment

by:m00
ID: 2797233
It gets big with a normal BorderLayout, but that's not what i want. Is having both GridBagLayout and a JTable a known problem?
0
 
LVL 5

Accepted Solution

by:
mbormann earned 100 total points
ID: 2798417
0
 

Author Comment

by:m00
ID: 2799517
Thats it. Thanks for your help.
0
 
LVL 5

Expert Comment

by:mbormann
ID: 2800389
I was going through the same kind of frustration as you were...
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now