Table in scrollPane in JTabbedPane

585 views Asked by At

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.

2

There are 2 answers

0
jemystack On BEST ANSWER
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);
}
1
jemystack On

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);