GridBagLayout causing JScrollPane to disappear when the frame is smaller than the table

skellyton3 :

I have a JTable, that has been put into a JScrollPane. I then add that JScrollPane to a JFrame so that the JScrollpane will show up on the JFrame. This works.

The issue I am facing is that when I manually resize the JFrame to be smaller than the JScrollPane, the entire JScrollPane just disappears into a small icon. I am using GridBagLayout for my JFrame, and have narrowed it down that for some reason I think GridBag is causing this. I can remove the GridBagLayout and it works as I intend it to.

I have managed to get the functionality I am looking for, but only when I don't have the JFrame set to use GridBagLayout.

import java.awt.*;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;


public class GridBagIssueExample
{
 public static void main(String[] args)
{
JTable NewTable = new JTable(); //create a JTable. Set up the Data model and add some data to it.
DefaultTableModel dtm = new DefaultTableModel();
NewTable.setModel(dtm);
NewTable.setRowHeight(100);
String[] Headers = new String[] {"Test0", "Test1", "LongTest", "SuperLongTest", "ST"};

dtm.setColumnIdentifiers(Headers);
dtm.addRow (new String[] {"0", "1", "123123123123", "2312321", "1"});
dtm.addRow (new String[] {"1", "1", "123123123123", "2312321", "1"});
dtm.addRow (new String[] {"2", "1", "123123123123", "2312321", "1"});
dtm.addRow (new String[] {"3", "1", "123123123123", "2312321", "1"});
dtm.addRow (new String[] {"4", "1", "123123123123", "2312321", "1"});
dtm.addRow (new String[] {"5", "1", "123123123123", "2312321", "1"});
NewTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF); 
//AUTO_RESIZE_OFF is important to get the functionality I want.

//Create a JScrollPane using the table.
  JScrollPane TablePane = new JScrollPane(NewTable); 

  //Create a JFrame and add the ScrollPane to it. 
  JFrame frame = new JFrame("JScrollPane Test");
  frame.getContentPane().add(TablePane);

  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  frame.pack();
  frame.setLocationRelativeTo(null);

  frame.setLayout(new GridBagLayout()); //Remove this line here and it works exactly how I want it to.
  frame.setVisible(true);
}
}

The code above demonstrates the issue. It will create a JTable, add it to a JSrollPane, and add that to a JFrame. Run this and resize the JFrame to be smaller than the JScrollPane to see my issue.

Right at the end there is a line that sets the JFrame to use GridBagLayout.

If you comment out or remove this single line, the entire thing works exactly how I want it to. I need to use GridBagLayout for what I am working on, so simply not using it is not an option here.

Help on this would be greatly appreciated as even after much Googling I cannot figure out why this is happening.

Kaplan :

there are no GridBagConstraints defined

  //Create a JFrame and add the ScrollPane to it. 
  JFrame frame = new JFrame("JScrollPane Test");
  frame.getContentPane().setLayout(new GridBagLayout()); //Remove this line here and it works exactly how I want it to.
  GridBagConstraints c = new GridBagConstraints();
  c.fill = GridBagConstraints.BOTH;
  c.weightx = 1.0;
  c.weighty = 1.0;
  frame.getContentPane().add( tablePane, c );

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=162179&siteId=1