How to set JTable width

I have a JTable in JScrollPane. I want the minimum width to be around 600 as a wide table. I tried to set the minimum size on the table, scroll bar and panel. Size doesn't change at all, what am I missing? This is tricky for Google, because all that’s appropriate is 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 this 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); } } 
+4
source share
3 answers

There are several problems:

  • (as already mentioned in my comment) FlowLayout of the inner panel always changes its children to the corresponding prefix
  • the min / pref / max width table is calculated from the sum of the corresponding column sizes
  • the table is scrollable and, as such, publishes its preferred ScrollableViewportSize (the size of which is used by JScrollPane to calculate its own prefSize)
  • the implementation of prefScrollable ... is missing (to put it mildly) in that it is hard-coded with something like 400 x 450 (or similar)

In addition, there are several screws to configure (after deleting all calls to setXXSize :))

  • make a panel using BorderLayout: scrollPane will fill the entire area if the frame size is changed.
  • extend JTable to return something reasonable for prefScrollableViewportSize (fi in terms of the number of privileges of visible columns / rows)

In the code (and using the JXTable of the SwingX project, because it already has an 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")); 
+8
source

there were some problems with this stuff. use setPreferedSize(new Dimension(800,600)); also. It may solve your problem.

0
source

Layout matters. An IDE such as Netbeans or Eclipse provides free-hand layout design by drag and drop.

You can even achieve code once you understand Swing layouts . The few most used layouts that I mention here.

 BorderLayout BoxLayout FlowLayout GridLayout 

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

Also, you do not need to expand JPanel or JFrame , unless you are overwriting something or adding more material to the frame. Practice learning.

0
source

Source: https://habr.com/ru/post/1444636/


All Articles