-1

I want to use JTabbedPane to display some tables (one table in each Tab). Pretty easy to realise: you make a JTabbedPane, put some scrollPanes (which represents the tabs) then you just add the table to your scrollPanes. My problem is that in scrollPanes you can add only the table and I also need to add a textbox, radiobuttons and labels.

My table without textbox, radiobuttons and labels

I would like to know if there are other alternatives to do that.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Marius
  • 13
  • 4
  • Place all the elements onto a JPanel, and place the JPanel inside the JScrollPane. – Compass Jan 03 '17 at 20:53
  • 1
    As @Compass says. Although likely you'll want only the JTable in the JScrollPane, but then place the JScrollPane into another JPanel that uses BorderLayout in the center position, and add your other components to other JPanels and then add those JPanels to the main JPanel, and then add that to the JTabbedPane. The key thought here being: **yes you can and should nest JPanels, and each one can use its own layout manager.** Just play with it and see what you can come up with. – Hovercraft Full Of Eels Jan 03 '17 at 21:22
  • @Hovercraft Full Of Eels If you place the table into a JPanel you will not be able to see the first row, which respresents the column names :) – Marius Jan 04 '17 at 10:32
  • @Compass already tried that – Marius Jan 04 '17 at 10:32
  • Re-read what I posted, my first statement in fact! Yes, you put the JTable into the JScrollPane but then that goes into nested JPanels. – Hovercraft Full Of Eels Jan 04 '17 at 11:47

2 Answers2

0

if i understand you ... you can put scrollPane to the tab and add to it panel and add to the panel the controls you need and the table;

like on this image

and you can use the jform desiner for generating the code

 jTabbedPane1 = new javax.swing.JTabbedPane();
    jScrollPane1 = new javax.swing.JScrollPane();
    jPanel1 = new javax.swing.JPanel();
    jButton2 = new javax.swing.JButton();
    jScrollPane2 = new javax.swing.JScrollPane();
    jTable1 = new javax.swing.JTable();

    jPanel1.setLayout(new javax.swing.BoxLayout(jPanel1, javax.swing.BoxLayout.Y_AXIS));

    jButton2.setText("jButton2");
    jPanel1.add(jButton2);

    jTable1.setModel(new javax.swing.table.DefaultTableModel(
        new Object [][] {
            {null, null, null, null},
            {null, null, null, null},
            {null, null, null, null},
            {null, null, null, null}
        },
        new String [] {
            "Title 1", "Title 2", "Title 3", "Title 4"
        }
    ));
    jScrollPane2.setViewportView(jTable1);

    jPanel1.add(jScrollPane2);

    jScrollPane1.setViewportView(jPanel1);

    jTabbedPane1.addTab("tab1", jScrollPane1);
jemystack
  • 408
  • 7
  • 12
  • I already tried this and it didn't work. However, your code works, but try to add jTabbedPane1 to a frame. You will see that the table it's not showing up anymore. I already used the jform designer from Eclipse – Marius Jan 04 '17 at 10:25
0
public static void main(String[] args) {

    JFrame Frame = new JFrame();
    JTabbedPane jTabbedPane1 = new javax.swing.JTabbedPane();
    JScrollPane jScrollPane1 = new javax.swing.JScrollPane();
    JPanel jPanel1 = new javax.swing.JPanel();
    JButton jButton1 = new javax.swing.JButton();
    JButton jButton2 = new javax.swing.JButton();
    JScrollPane jScrollPane2 = new javax.swing.JScrollPane();
    JTable jTable1 = new javax.swing.JTable();

    Frame.setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

    jPanel1.setLayout(new javax.swing.BoxLayout(jPanel1, javax.swing.BoxLayout.Y_AXIS));

    jButton1.setText("jButton1");
    jPanel1.add(jButton1);

    jButton2.setText("jButton2");
    jPanel1.add(jButton2);

    jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object[][]{
                {null, null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null, null},

            },
            new String[]{
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
    ));
    jScrollPane2.setViewportView(jTable1);

    jPanel1.add(jScrollPane2);

    jScrollPane1.setViewportView(jPanel1);

    jTabbedPane1.addTab("tab2", jScrollPane1);

    Frame.setContentPane(jTabbedPane1);
    Frame.setVisible(true);
}
jemystack
  • 408
  • 7
  • 12