Results 1 to 2 of 2
  1. #1
    shall is offline Senior Member
    Join Date
    Apr 2012
    Posts
    211
    Rep Power
    0

    Default Why is JTable not filling top panel in GridLayout(2, 1)?

    Hello,

    The table in the code below is not filling the topPanel. What am I doing wrong?

    Thanks,
    Stephen


    Java Code:
        
    JPanel generateTable() {
            // Create a panel to hold all other components
            JPanel parameterTable  = new JPanel(new GridLayout(2, 1));
            JPanel topPanel = new JPanel(new GridBagLayout());
            JPanel bottomPanel = new JPanel(new GridBagLayout());
            
            GridBagConstraints topPanelConstraints = new GridBagConstraints();
            topPanelConstraints.insets = new Insets(2, 2, 2, 2); 
            topPanelConstraints.fill = GridBagConstraints.BOTH;
            
            GridBagConstraints bottomPanelConstraints = new GridBagConstraints();
            
            JButton writeButton = new JButton("Write");
            
            readfile();
    
            // Create columns names
            String columnNames[] = {"Parameter Address", "Parameter Name", "Default Parameter Value", "Current Value"};
    
            String[][] tempTable = new String[dataValuesList.size()][];
    
            int i = 0;
            for (ArrayList<String> next : dataValuesList) {
                tempTable[i++] = next.toArray(new String[next.size()]); // return Object[][]
            }
            // Create a new table instance
            table = new JTable(tempTable, columnNames);
            
            
            table.setEnabled(false);
            table.setCellSelectionEnabled(true);
    
            DefaultTableCellRenderer centerRenderer = new DefaultTableCellRenderer();
            centerRenderer.setHorizontalAlignment(SwingConstants.CENTER);
            table.setDefaultRenderer(String.class, centerRenderer);
    
            for (int x = 0; x < 4; x++) {
                if (x != 1) {
                    table.getColumnModel().getColumn(x).setCellRenderer(centerRenderer);
                }
            }
        
            topPanel.add(table,topPanelConstraints);
            scrollPane = new JScrollPane(topPanel);
    
            bottomPanel.add(writeButton);
    
    
            parameterTable.add(scrollPane);
            parameterTable.add(bottomPanel);
          
            return parameterTable;
        }

  2. #2
    shall is offline Senior Member
    Join Date
    Apr 2012
    Posts
    211
    Rep Power
    0

    Default Re: Why is JTable not filling top panel in GridLayout(2, 1)?

    Ok, I found the answer.

    I just needed to set weightx and weighty to non-zero values, i.e.
    topPanelConstraints.weightx = 1;
    topPanelConstraints.weighty = 1;

Similar Threads

  1. can't navigate from child panel to main panel
    By sharmapiyush in forum New To Java
    Replies: 76
    Last Post: 04-09-2014, 09:39 PM
  2. Replies: 0
    Last Post: 05-14-2012, 04:57 PM
  3. Filling Text Field from information from JTable
    By Dcalladi in forum AWT / Swing
    Replies: 3
    Last Post: 10-20-2011, 05:12 PM
  4. Filling a JTable
    By aborgeld in forum Advanced Java
    Replies: 0
    Last Post: 01-08-2011, 01:37 PM
  5. SWT GridLayout
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-08-2008, 09:04 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •