7

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);
    }
}
Steve's a D
  • 3,801
  • 10
  • 39
  • 60

3 Answers3

12

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"));
Hasaan Ali
  • 1,192
  • 16
  • 22
kleopatra
  • 51,061
  • 28
  • 99
  • 211
  • Thanks! I put it in a border layout and it seems to look good. You are missing a right parenthesis on the line above where the code starts. Its driving me crazy :) – Steve's a D Nov 07 '12 at 20:14
0

had some Problems with this stuff too. use the setPreferedSize(new Dimension(800,600)); too. this could solve your problem

SomeJavaGuy
  • 7,307
  • 2
  • 21
  • 33
  • That worked! I set the preferred size on the JScrollPane, setting it on the others didn't seem to have an effect. Also you spelled preferred wrong. Thanks! – Steve's a D Nov 07 '12 at 18:15
  • 2
    no - never ever call any of the setXXSize of components: [some reasons](http://stackoverflow.com/questions/7229226/should-i-avoid-the-use-of-setpreferredmaximumminimumsize-methods-in-java-swi/7229519#7229519) – kleopatra Nov 07 '12 at 18:16
  • @kleopatra How would you suggest I fix the width problem? – Steve's a D Nov 07 '12 at 18:17
  • @Steve - even if it seems to be helping now, it's asking for pain in future. Simply don't, never. – kleopatra Nov 07 '12 at 18:18
  • @kleopatra I'm not suggesting you're wrong. I'm asking what is the correct way to achieve what I'm attempting. – Steve's a D Nov 07 '12 at 18:20
  • simultaneous comments :-) @Steve the answer to layout problems is always the same: choose a suitable LayoutManager. Here the problem stems from the FlowLayout of the inner panel which will size everything to its pref – kleopatra Nov 07 '12 at 18:21
  • with the Gridbaglayout for example, if you initialize an empty JTextfield it will be shown as an JTextfield with a width around 25. This is a pretty solid way to initialize Components with a fixed with or height. Sorry for the misspelling ;) – SomeJavaGuy Nov 07 '12 at 18:23
  • @kelopatra which layout manager should I be using? I could live with like 80% width of the page, but I have a feeling thats not something that can be easily obtained with java from my limited experience – Steve's a D Nov 07 '12 at 18:25
  • @Steve If you are allowed to go for a third party manager: my current favourite is MigLayout. It's highly flexible, powerful (nearly nothing it can't do and intuitive to use. What do you mean by _80% width of the page_ ? – kleopatra Nov 07 '12 at 18:32
  • @kleopatra sorry I'm a web developer haha. 80% the width of the window, so if its maximized it should still be 80% – Steve's a D Nov 07 '12 at 18:41
0

Layout matters. IDE like Netbeans or Eclipse provides hands free layout design by drag and drop.

You can achieve even by code once you understand Swing layouts. Few most used layouts I'm mentioning here

BorderLayout
BoxLayout
FlowLayout
GridLayout

in yours, if you add panel.setLayout( new GridLayout(1,1));, table will get fixed within frame.

Also you need not extend JPanel or JFrame unless if you overwrite something or adding more stuff to frame. Practice to learn.

vels4j
  • 11,208
  • 5
  • 38
  • 63