Add several buttons in jTabbedPane Java

0

I'm making an application and I just created a jTabbedPane but to each tab of the I want to add more than one button or some selection point, that is to say that in the tab1 I left button1 and next to the button2.

Something like what appears in the image, that would be ideal

    
asked by Jesus Castellanos 18.12.2018 в 01:52
source

1 answer

-1

To achieve the desired effect you must use a JPanel which will contain the elements and organize them (using a Layout) the way you want. the Jpanel is created in the following way

public class ButtonPanel extends javax.swing.JPanel {

private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JButton jButton3;
private javax.swing.JButton jButton4;
private javax.swing.JButton jButton5;
private javax.swing.JButton jButton6;
private javax.swing.JButton jButton7;
private javax.swing.JButton jButton8;

public ButtonPanel() {
    jButton1 = new javax.swing.JButton("BTT1");
    jButton2 = new javax.swing.JButton("BTT2");
    jButton5 = new javax.swing.JButton("BTT3");
    jButton7 = new javax.swing.JButton("BTT4");
    jButton4 = new javax.swing.JButton("BTT5");
    jButton3 = new javax.swing.JButton("BTT6");
    jButton6 = new javax.swing.JButton("BTT7");
    jButton8 = new javax.swing.JButton("BTT8");
    //este Layout ordena los compentes en el Panel 
    //de forma que se vean como una grilla de 2 filas y 4 collumnas con un espacio
    //vertical y horizontal entre los componentes de 10 
    setLayout(new java.awt.GridLayout(2, 4, 10, 10));
    add(jButton1);
    add(jButton2);
    add(jButton3);
    add(jButton4);
    add(jButton5);
    add(jButton6);
    add(jButton7);
    add(jButton8);
//...
//agregar eventos a los botones? o otros componentes a el panel
//...
}
//...
}

NOTE: this is just one way, another way is to use a Method that creates an instance of JPanel and add the Layout and the bottones:

public JPanel getButtonPanel() {
    JButton jButton = new JButton("BTT1");
    //...
    //crear los demas botones
    JPanel tab = new JPanel();
    tab.setLayout(new java.awt.GridLayout(2, 4, 10, 10));
    tab.add(jButton);
    //...
    //agrega el resto de Bottones
    //....
    // agregar eventos etc. 
    return tab;

}

this generates a panel that will look something like this:

to add it to JTabbedPane is done in the following way:

//...
MyTabbedPane= new javax.swing.JTabbedPane();
ButtonPanel mytab= new ButtonPanel();
MyTabbedPane.addTab("Pestania 1", mytab);
//...

the result:

recommended readings:

Panels tutorials

Layouts tutorial

OH and another thing. so you have a border use:

setBorder(javax.swing.BorderFactory.createEmptyBorder(10, 10, 10, 10));

    
answered by 19.12.2018 в 02:05