Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set the width of a JTable

I have a JTable in a JScrollPane. I want the minimum width to be around 600 as its a wide table. I tried setting the minimum size on the table, the scroll pane, and the panel its self. The size doesn't change at all, what am I missing? Its hard to google this because all that comes up is how to set the width of the columns.

enter image description here

Here is the code:

class SearchResults extends JPanel {

/**
 * Create the panel.
 */
public SearchResults() {
    setMinimumSize(new Dimension(640, 480));
    String[][] data= new String[][] {
            {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, "VIEW BUTTON"},
            {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, "VIEW BUTTON"}};
    String[] col = new String[] {
            "Last Name", "First Name", "Middle Initial", "Phone Number", "Email", "Project Title", "Project Description", "Amount", "Date Approved", "Date Completed", "College", "Faculty Mentor Name", "Co Grantee", "Major", "Travel Required", "Travel Purpose", "Travel Cost", "Travel Start Date", "Travel End Date", "View"};

      JTable table = new JTable(data,col);
      table.setMinimumSize(new Dimension(600,200));
      JTableHeader header = table.getTableHeader();
      JScrollPane pane = new JScrollPane(table);
      pane.setMinimumSize(new Dimension(600, 23));
      table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
      add(pane);
}

}

And here is where I add it to the JFrame:

public class Test extends JFrame
{

    public static void main(String[] args)
    {
        Test test = new Test();
        test.run();
    }

    public Test()
    {
        super("JAVA TEST!");
    }

    private void run()
    {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        SearchResults resultsPanel = new SearchResults();
        resultsPanel.setMinimumSize(new Dimension(600,200));
        add(resultsPanel);
        setSize(800,600);
        setVisible(true);
    }
}
like image 982
Steve's a D Avatar asked Nov 07 '12 18:11

Steve's a D


People also ask

How do I change the width of a JTable?

By default the width of a JTable is fixed, we can also change the width of each column by using table. getColumnModel(). getColumn(). setPreferredWidth() method of JTable class.

How do you size a JTable?

This can be done easily using these two methods of the JTable class: setRowHeight(int row, int rowHeight): sets the height (in pixels) for an individual row. setRowHeight(int rowHeight): sets the height (in pixels) for all rows in the table and discards heights of all rows were set individually before.

How do you make a JTable column not movable?

setReorderingAllowed() method and set the value as false.


1 Answers

There are several problems:

  • (as already mentioned in my comment) the FlowLayout of the inner panel always sizes its children to their respective prefSize
  • a table's min/pref/max width is calculated from the sum of the respective column sizes
  • a table is-a Scrollable and as such publishes its preferredScrollableViewportSize (which is the size a surrounding JScrollPane uses to calculates its own prefSize)
  • the implementation of prefScrollable is ... lacking (to put it mildly) in that its hard-coded to something like 400 x 450 (or similar)

Consequestly, there are several screws to tweak (after removing all setXXSize calls :) )

  • make the panel use a BorderLayout: the scrollPane will fill the complete area if the frame is resized.
  • extend the JTable to return something reasonable for prefScrollableViewportSize (f.i. in terms of the pref number of visible columns/rows)

In code (and using JXTable of the SwingX project because it already has api for the second :-) )

String[][] data= new String[][] {
        {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, "VIEW BUTTON"},
        {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, "VIEW BUTTON"}};
String[] col = new String[] {
        "Last Name", "First Name", "Middle Initial", "Phone Number", 
        "Email", "Project Title", "Project Description", "Amount", 
        "Date Approved", "Date Completed", "College", "Faculty Mentor Name", 
        "Co Grantee", "Major", "Travel Required", "Travel Purpose", 
        "Travel Cost", "Travel Start Date", "Travel End Date", "View"};

 JXTable table = new JXTable(data,col);
 table.setVisibleColumnCount(10);
 table.setHorizontalScrollEnabled(true);
 JScrollPane pane = new JScrollPane(table);
 JComponent comp = new JPanel(new BorderLayout());
 comp.add(pane);

Edit

To solve the 80% requirement (and a little teaser for MigLayout :-) )

// 80% with a minimum of 600 logical pixel:
MigLayout layout = new MigLayout("wrap 2, debug",
        "[600:pref, fill, grow][20%]");
JComponent comp = new JPanel(layout);
comp.add(pane, "spany");
comp.add(new JLabel("just something"));
like image 140
kleopatra Avatar answered Sep 25 '22 03:09

kleopatra