Link to home
Start Free TrialLog in
Avatar of m00
m00

asked on

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.
Avatar of AlexJ030200
AlexJ030200

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.
Avatar of m00

ASKER

Seems to have no effect on my JTable unfortunately...
post your code (small compilable example)
//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();
 }
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);
}
Avatar of m00

ASKER

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...
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!
Avatar of m00

ASKER

Adjusted points from 50 to 100
Avatar of m00

ASKER

It gets big with a normal BorderLayout, but that's not what i want. Is having both GridBagLayout and a JTable a known problem?
ASKER CERTIFIED SOLUTION
Avatar of mbormann
mbormann

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of m00

ASKER

Thats it. Thanks for your help.
I was going through the same kind of frustration as you were...