Set several layouts in a Java window

1

I am trying to combine in the same window two JPannel , in one enter a GridLayout and in another to put a JButton that is not included in the Grid.

The Grid view is displayed correctly but the JButton is not displayed in the window.

The code is as follows:

public class Interface extends JFrame {
//-----ATRIBUTOS-----
private JPanel contentPane;
private JPanel panel2;
private GridLayout gridLayout;
private BorderLayout borderLayout;
private JButton jButton;

public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                Interface frame = new Interface();
                frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

//----------CONSTRUCTOR--------
public Interface(){
    setTitle("Sudoku");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(25,25,650,430); 
    contentPane = new JPanel();
    panel2 = new JPanel();
    panel2.setBorder(new EmptyBorder(400,250,20,20));
    setContentPane(panel2);
    contentPane.setBorder(new EmptyBorder(5,5,150,300)); 
    setContentPane(contentPane);
    contentPane.setLayout(null);

    //Establecemos el gridLayout
    gridLayout = new GridLayout(5, 5);
    contentPane.setLayout(gridLayout);
    //Rellenamos el gridLayout
    for (int i =0;i<gridLayout.getRows();i++){
        for (int j=0;j<gridLayout.getColumns();j++){
            contentPane.add(new JTextField());
        }
    }


    //Establecemos el borderLayout
    borderLayout = new BorderLayout();
    panel2.setLayout(borderLayout);

    //Botón de validar
    jButton = new JButton("VALIDAR");
        jButton.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {

        }
    });
    jButton.setBounds(0, 0, 100,150);
    panel2.add(jButton);


}
}
    
asked by Iván García 08.10.2016 в 13:26
source

2 answers

2

The problem that you present is that you do a setContentPane twice, that is, the first does not take it into account.

 setContentPane(panel2);
 setContentPane(contentPane);

After these two lines of code, the JFrame , all the components that you add to your JFrame will be accommodated to the Layout set for contentPane that's why if you do a

contentPane.add(jButton); /* o add(jButton); se añadira al Layaout Grid*/
panel2.add(jButton); /* No se visualizará ya que no fue añadido al JFrame*/

To Organize Better your Containers including your JFrame you need to use Layouts , The builder might look like this

public Interface(){
   setTitle("Sudoku");
   setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   setBounds(25,25,650,430); 
   setLayout(new BorderLayout());
   contentPane = new JPanel();
   panel2 = new JPanel(new BorderLayout());
//Establecemos el gridLayout
   gridLayout = new GridLayout(5, 5);
   contentPane.setLayout(gridLayout);
//Rellenamos el gridLayout
  for (int i =0;i<gridLayout.getRows();i++){
    for (int j=0;j<gridLayout.getColumns();j++){
        contentPane.add(new JTextField());
    }
}
//Botón de validar
  jButton = new JButton("VALIDAR");
    jButton.addActionListener(new ActionListener() {

    @Override
    public void actionPerformed(ActionEvent e) {

    }
});
  panel2.add(jButton, BorderLayout.CENTER);
  add(contentPane,BorderLayout.CENTER);
  add(panel2,BorderLayout.WEST);
}
    
answered by 08.10.2016 / 20:32
source
0

Good, you have to add the button to the grid, add this line contentPane.add(jButton); then you will be seen. :)

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

public class Interface extends JFrame {
//-----ATRIBUTOS-----
private JPanel contentPane;
private JPanel panel2;
private GridLayout gridLayout;
private BorderLayout borderLayout;
private JButton jButton;

public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                Interface frame = new Interface();
                frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

//----------CONSTRUCTOR--------
public Interface(){
    setTitle("Sudoku");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(25,25,650,430); 
    contentPane = new JPanel();
    panel2 = new JPanel();
    panel2.setBorder(new EmptyBorder(400,250,20,20));
    setContentPane(panel2);
    contentPane.setBorder(new EmptyBorder(5,5,150,300)); 
    setContentPane(contentPane);
    contentPane.setLayout(null);
    //Establecemos el gridLayout
    gridLayout = new GridLayout(5, 5);
    contentPane.setLayout(gridLayout);
    //Rellenamos el gridLayout
    for (int i =0;i<gridLayout.getRows();i++){
        for (int j=0;j<gridLayout.getColumns();j++){
            contentPane.add(new JTextField());
        }
    }


    //Establecemos el borderLayout
    borderLayout = new BorderLayout();
    panel2.setLayout(borderLayout);

    //Botón de validar
    jButton = new JButton("VALIDAR");
        jButton.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {

        }
    });
    jButton.setBounds(0, 0, 100,150);
    panel2.add(jButton);
    contentPane.add(jButton);


}
}

answered by 08.10.2016 в 14:10