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.