Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

153
Visualizações
Placing a JPanel on a new line using GridBagLayout

I'm trying to figure out a way to place a panel containing combo boxes in my JFrame on a new line using gridbaglayout. At the moment it appears next to another panel of the same type.

Here's what it looks like:

https://i.stack.imgur.com/74Eui.gif

Here is the code for the particular areas

JPanel saleOfferPanel = new JPanel();
JLabel dateOfSale = new JLabel("Select Date of Sale:");     
saleOfferPanel.add(dateOfSale);
JLabel saleDay = new JLabel("Day:");
saleOfferPanel.add(saleDay);
JComboBox<Integer> dayOfSale = new JComboBox<>();
saleOfferPanel.add(dayOfSale);
JLabel saleMonth = new JLabel("Month:");
saleOfferPanel.add(saleMonth);
JComboBox<Integer> monthOfSale = new JComboBox<>();
saleOfferPanel.add(monthOfSale);
JLabel saleYear = new JLabel("Year:");
saleOfferPanel.add(saleYear);
JComboBox<Integer> yearOfSale = new JComboBox<>();
saleOfferPanel.add(yearOfSale);



JPanel endSalePanel = new JPanel();
JLabel endOfSale = new JLabel("Select End Date of Sale:");
saleOfferPanel.add(endOfSale);
JLabel endDay = new JLabel("Day:");
saleOfferPanel.add(endDay);
JComboBox<Integer> endDayOfSale = new JComboBox<>();
saleOfferPanel.add(endDayOfSale);
JLabel endMonth = new JLabel("Month:");
saleOfferPanel.add(endMonth);
JComboBox<Integer> endMonthOfSale = new JComboBox<>();
saleOfferPanel.add(endMonthOfSale);
JLabel endYear = new JLabel("Year:");
saleOfferPanel.add(endYear);
JComboBox<Integer> endYearOfSale = new JComboBox<>();
saleOfferPanel.add(endYearOfSale);

 setLayout(new GridBagLayout());
 GridBagConstraints gbc = new GridBagConstraints();
 gbc.weightx = 0.5;
 gbc.weighty = 0;
 gbc.anchor = GridBagConstraints.NORTHWEST;
 //other panels...

 //panels in question
 gbc.gridx = 0;
 gbc.gridy = 9;
 gbc.gridheight = 1;
 add(saleOfferPanel, gbc);


 gbc.gridx = 0;
 gbc.gridy = 10;
 gbc.gridheight = 1;
 add(endSalePanel, gbc);

Hoping that someone may be able to offer assistance.

Thanks

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

I have changed your code a little and succeeded changing its position. here is the code snippet.

`//      setLayout(new GridBagLayout());
         GridBagLayout gb = new GridBagLayout();
         this.setLayout(gb);
         GridBagConstraints gbc = new GridBagConstraints();
         gbc.weightx = 1;
         gbc.weighty = 1;
         gbc.anchor = GridBagConstraints.NORTHWEST;
         gbc.fill = GridBagConstraints.BOTH;
         //other panels...

         //panels in question
         gbc.gridwidth = GridBagConstraints.REMAINDER; //this will tell GridBagLayout component below should be last one on this line.
         gb.setConstraints(saleOfferPanel, gbc);
         this.add(saleOfferPanel);

         gb.setConstraints(endSalePanel, gbc);
         this.add(endSalePanel);
         this.pack();
         this.setSize(500, 200);
         this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         this.setVisible(true);`
over 4 years ago · Santiago Trujillo Relatório

0

Look carefully at the code that creates and populates endSalePanel:

JPanel endSalePanel = new JPanel();
JLabel endOfSale = new JLabel("Select End Date of Sale:");
//[KA] Here's where it all starts to go wrong!!!  You wanted to add
//  endOfSale to endSalePanel, but...
saleOfferPanel.add(endOfSale);
JLabel endDay = new JLabel("Day:");
saleOfferPanel.add(endDay);

Cut-and-paste strikes again!

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda