0

I have to put different JPanels into a JScrollPane. I am using a JFrame as it is defined below:

public class VistaFCFS extends JFrame {
    private final JScrollPane panelScroll = new JScrollPane(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);

    public VistaFCFS() throws HeadlessException {
    super("Tittle");

    this.setSize(1400, 500);
    this.setDefaultCloseOperation(EXIT_ON_CLOSE);
    this.setResizable(false);
    this.setLocationRelativeTo(null);
    this.setContentPane(panelScroll);
    /*If I try this nothing happens
    this.add(new MainTable());*/
    /*If I try this I can only see one JPanel
    panelScroll.getViewport().add(new MainTable());
    panelScroll.getViewport().add(new MainProcess());*/
}

The MainTable() class is defined below:

public class MainTable extends JPanel {

    private JTable tblProcesses;
    private JScrollPane scrollTable;
    
    private Object[][] text;
    private final Object[] header = {"Proceso", "Llegada", "Ejecución", "Blq. Inicio", "Blq. Duración"};
    
    public MainTable() {       
        text = new Object[6][5];
        
        this.setLayout(new BorderLayout());
        this.add(getTable(), BorderLayout.CENTER);
    }
    
    private JScrollPane getTable() {
        tblProcesses = new JTable(text, header);
        tblProcesses.setRowHeight(30);
        scrollTable = new JScrollPane(tblProcesses);
        tblProcesses.setEnabled(false);
       
        return scrollTable;
    }
}

If I use panelScroll.getViewport().add(new MainTable()); the JPanel defined in MainTable() is added to the JScrollPane in VistaFCFS. However, I need to add several Panels, and if I try to add a second panel, it seems the second JPanel gets over the first one, and so on (e.g. using panelScroll.getViewport().add(new MainProcess()); only shows the JPanel defined on the class MainProcess(), and not both panels MainTable() and MainProcess() as I expect).

I'm not sure if this is related to the layout, every time I try to change it I get layout of JScrollPane must be a ScrollPaneLayout.

Here is defined the main method:

public static void main(String[] args) {

    VistaFCFS v = new VistaFCFS();
    javax.swing.SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
            v.setVisible(true);
        }
    });

}

Thanks in advance.

camickr
  • 321,443
  • 19
  • 166
  • 288
  • Take a look at the [JavaDocs for `JScrollPane`](https://docs.oracle.com/javase/10/docs/api/javax/swing/JScrollPane.html) and [How to Use Scroll Panes](https://docs.oracle.com/javase/tutorial/uiswing/components/scrollpane.html) for more details on how `JScrollPane` works. – MadProgrammer Sep 24 '21 at 05:09
  • Basically, a `JScrollPane` has a `JViewPort`, which is the primary container for the `JScrollPane`. A `JViewPort` contains a single view or component. This facilities what is going to be scrolled. With this in mind, if you want to add multiple components to the `JScrollPane`, you need to create a "base" container onto which those components get added and then apply that (base container) as the viewport's view – MadProgrammer Sep 24 '21 at 05:10
  • As a basic [concept](https://stackoverflow.com/questions/24987832/jscrollpane-for-a-jpanel-inside-a-jpanel/24987880#24987880) – MadProgrammer Sep 24 '21 at 05:20
  • Thank you @MadProgrammer I figured to place a main JPanel and use the JScrollPane only with that panel. – Smooth Researcher Oct 10 '21 at 18:30

0 Answers0