JTable width

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.
m00Asked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
mbormannConnect With a Mentor Commented:
0
 
AlexJ030200Commented:
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
 
m00Author Commented:
Seems to have no effect on my JTable unfortunately...
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
heyhey_Commented:
post your code (small compilable example)
0
 
Ik_Khan_PkCommented:
//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
 
mbormannCommented:
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
 
m00Author Commented:
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
 
mbormannCommented:
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
 
m00Author Commented:
Adjusted points from 50 to 100
0
 
m00Author Commented:
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
 
m00Author Commented:
Thats it. Thanks for your help.
0
 
mbormannCommented:
I was going through the same kind of frustration as you were...
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.