Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

JTable width

Posted on 2000-05-09
12
Medium Priority
?
509 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Build and deliver software with DevOps

A digital transformation requires faster time to market, shorter software development lifecycles, and the ability to adapt rapidly to changing customer demands. DevOps provides the solution.

 
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
 
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 400 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

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

Question has a verified solution.

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

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
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:
Suggested Courses

721 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