0

I created a JScrollPane with a JTable on it. When the table's height is larger than the height of the scroll pane, a scroll bar appears. If minimize the JFrame although I didn't change the size of it, the scroll bar vanishes and the scroll pane extends downwards.

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;

public class Main {
    
    static String columns[] = {"Date", "Price", "URL", "Expired"};
    static String data[][] = new String[8][4];
    
    /*
     * The data that should be provided to the JTable is 
     * replaced with some example data because the method
     * of getting this data is complicated and doesn't
     * change anything at the outcome.
     */

    public static void main(String[] args) {
        
        loadGui();
    }
    
    public static void loadGui() {
        
        for (int i = 0; i < 8; i++) {
            for (int j = 0; j < 4; j++) {
                data[i][j] = "Example data " + i + " " + j;
            }
        }
        
        JFrame mainFrame = new JFrame();
        mainFrame.setSize(800, 300);
        mainFrame.setResizable(false);
        mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        mainFrame.setVisible(true);
        
        JTable table = new JTable(data, columns);;
        table.setPreferredScrollableViewportSize(table.getPreferredSize());
        
        JScrollPane pane = new JScrollPane(table);
        pane.setViewportView(table);
        pane.setSize(785, 100);
        mainFrame.add(pane);
        table.getTableHeader().setReorderingAllowed(false);
        table.setDefaultEditor(Object.class, null);
        table.setFocusable(false);
        table.setRowSelectionAllowed(false);
    }
}

I search for a way to stop the scroll pane to extend downwards and keeping it in its size.

Luqus
  • 109
  • 1
  • 11
  • 2
    Swing was designed to be used with layout managers. You should not be using setSize(). The layout manager will set the size/location of components. The layout manager is invoked when the frame size is restored, so it overrides your setSize() statement. Don't use setSize(). You can suggest the size of the viewport of the scrollpane as demonstrated in this example: https://stackoverflow.com/a/56877885/131872. You might want to override the height. Also, Swing components are visible by default, there is no need for "pane.setVisible(true)". Post a proper [mre] if you need more help. – camickr May 24 '21 at 14:32
  • Still waiting for your [mre]... – camickr May 24 '21 at 21:53
  • Sorry, I was a bit late, but now you should see the minimal reproducible example. – Luqus May 25 '21 at 17:43

1 Answers1

1
  1. You can use BorderLayout layout manager to make your scroll pane stick to the top

  2. You can use setPreffered size to suggest to the layout manager what dimensions element should have if possible

  3. Use setVisible when all components are already added

    public class Main {

    static String columns[] = {"Date", "Price", "URL", "Expired"};
    static String data[][] = new String[8][4];
    
    /*
     * The data that should be provided to the JTable is 
     * replaced with some example data because the method
     * of getting this data is complicated and doesn't
     * change anything at the outcome.
     */
    
    public static void main(String[] args) {
    
        loadGui();
    }
    
    public static void loadGui() {
    
        for (int i = 0; i < 8; i++) {
            for (int j = 0; j < 4; j++) {
                data[i][j] = "Example data " + i + " " + j;
            }
        }
    
        JFrame mainFrame = new JFrame();
        mainFrame.setSize(800, 300);
        mainFrame.setResizable(false);
        mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    

//set our layour manager mainFrame.setLayout(new BorderLayout());

        JTable table = new JTable(data, columns);;
        table.setPreferredScrollableViewportSize(table.getPreferredSize());
        table.getTableHeader().setReorderingAllowed(false);
        table.setDefaultEditor(Object.class, null);
        table.setFocusable(false);
        table.setRowSelectionAllowed(false);


        JScrollPane pane = new JScrollPane(table);
        pane.setViewportView(table);
// we would like to have its height at 100px, width does not matter thus 0
        pane.setPreferredSize(new Dimension(0,100));

//using NORTH will "stick" the component to the top
           mainFrame.add(pane,BorderLayout.NORTH);

//all the calculation will be done now. The same happens when you minimize/restore the frame.
            mainFrame.setVisible(true);
    }
}
Antoniossss
  • 31,590
  • 6
  • 57
  • 99