Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4145871

JTable within a JScrollPane placed within a GridBagLayout will not display

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P4 P4
    • 1.2.2
    • 1.1.6
    • client-libs
    • 1.2.2
    • x86
    • windows_95



      Name: el35337 Date: 06/04/98


      I'm using Swing 1.0.2.

      If one places a lone JTable within a JScrollPane in a GridBagLayout with the constraints given in the code below, it does not paint and it should.

      However, if one adds a JPanel (in this case
      panel2) to the layout, the JScrollPane does
      display.

      To guarantee that the problem is with the
      JScrollPane and not the way the GridBag layout
      is configured, one can swap out the JScrollPane
      and use a JPanel instead, and it works fine.
      That is, comment out the JScrollPane section
      and the Panel2 section. Panel1, which has
      the same constraints as the JScrollPane, displays
      just fine.


      import java.awt.*;
      import com.sun.java.swing.*;
      import com.sun.java.swing.table.*;


      public class tableDisappears extends JFrame {
        public tableDisappears(){
        }
        
        public static void main(String args[]) {
          tableDisappears f = new tableDisappears();

          GridBagLayout gridBag = new GridBagLayout();
          f.getContentPane().setLayout(gridBag);

          //---------JTable within JScrollPane ---------
          JTable table = new JTable();
          JScrollPane scrollPane = new JScrollPane(
            table,
            JScrollPane.VERTICAL_SCROLLBAR_NEVER,
            JScrollPane.HORIZONTAL_SCROLLBAR_NEVER
          );
          scrollPane.setColumnHeaderView(table.getTableHeader());

          TableModel tm = table.getModel();
          ((DefaultTableModel)tm).addColumn("Column1");

          GridBagConstraints scrollPaneConstraints = new GridBagConstraints();
          scrollPaneConstraints.weightx = 1.0;
          scrollPaneConstraints.weighty = 0.0;
           
          scrollPaneConstraints.gridx = 1;
          scrollPaneConstraints.gridy = 0;
          scrollPaneConstraints.fill = GridBagConstraints.HORIZONTAL;
          scrollPaneConstraints.anchor = GridBagConstraints.NORTH;
          scrollPaneConstraints.gridheight = 2;
          gridBag.setConstraints(scrollPane,scrollPaneConstraints);

          f.getContentPane().add(scrollPane);
          //--------------------------------------------

          /*---------First JPanel-----------------------
          JPanel panel1 = new JPanel();

          panel1.add(new JLabel("test1"));
          
          panel1.setBackground(Color.red);

          GridBagConstraints panel1Constraints = new GridBagConstraints();
          panel1Constraints.weightx = 1.0;
          panel1Constraints.weighty = 0.0;
           
          panel1Constraints.gridx = 1;
          panel1Constraints.gridy = 0;
          panel1Constraints.fill = GridBagConstraints.HORIZONTAL;
          panel1Constraints.anchor = GridBagConstraints.NORTH;
          panel1Constraints.gridheight = 2;
          gridBag.setConstraints(panel1,panel1Constraints);

          f.getContentPane().add(panel1);
          ---------------------------------------------*/
          
          //---------Second JPanel-----------------------
          JPanel panel2 = new JPanel();

          panel2.add(new JLabel("test2"));
          
          panel2.setBackground(Color.green);

          GridBagConstraints panel2Constraints = new GridBagConstraints();
          panel2Constraints.weightx = 0.0;
          panel2Constraints.weighty = 1.0;
           
          panel2Constraints.gridx = 2;
          panel2Constraints.gridy = 0;
          panel2Constraints.fill = GridBagConstraints.BOTH;
          panel2Constraints.gridwidth = GridBagConstraints.RELATIVE;
          panel2Constraints.gridheight = GridBagConstraints.REMAINDER;
        
          gridBag.setConstraints(panel2,panel2Constraints);

          f.getContentPane().add(panel2);
          //---------------------------------------------

          f.setSize(100,100);
          f.show();
        }
      }
      (Review ID: 32823)
      ======================================================================

            pmilnesunw Philip Milne (Inactive)
            elarsen Erik Larsen (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: