Correct display of RadionButtons

0

I am trying to implement a JRadioButton but after inserting the 2 in the form, only one is displayed, does anyone help please?

package com.roknauta.fasttracker.utils;

import java.awt.Component;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;

public class Testes {

    public static JPanel painel = new JPanel();
    private static JFrame formulario = new JFrame();

    public static void main(String[] args) {

        formulario.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        formulario.setTitle("dd");
        formulario.setSize(500, 270);
        // Formulário no centro da tela.
        formulario.setLocationRelativeTo(null);
        // --[ DESLIGANDO O GERENCIADOR DE LAYOUT ]--\
        painel.setLayout(null);
        formulario.add(painel);

        // Labels
        final JLabel codigoInicial = new JLabel("Código inicial:");
        final JLabel codigoFinal = new JLabel("Código final:");
        final JLabel ignorarEntregues = new JLabel("Ignorar os já entregues:");

        // JTexts
        final JTextField jL1 = new JTextField();
        final JTextField jL2 = new JTextField();

        final JRadioButton jbY = new JRadioButton("Sim", false);
        final JRadioButton jbN = new JRadioButton("Não", true);

        // Adicionando os componentes
        adiciona(codigoInicial, 10, 10, 100, 25);
        adiciona(jL1, 190, 10, 190, 25);

        adiciona(codigoFinal, 10, 50, 100, 25);
        adiciona(jL2, 190, 50, 190, 25);

        adiciona(ignorarEntregues, 10, 90, 180, 25);
        adiciona(jbY, 190, 90, 190, 25);
        adiciona(jbN, 220, 90, 190, 25);

        formulario.setVisible(true);
    }

    private static void adiciona(Component componente, int nColuna, int nLinha, int nLargura, int nAltura) {
        painel.add(componente);
        componente.setBounds(nColuna, nLinha, nLargura, nAltura);
    }
}

It looks like this when I run:

How to display the other Radio?

    
asked by anonymous 02.03.2018 / 19:41

2 answers

2

The problem is that you are using absolute layout, and this is a very bad practice because it greets all the appearance of your application to the monitor and resolution that you are developing. Not to mention that if you need to change or add some component, you will have to move all.

The setBounds method receives 4 parameters, which are the horizontal and vertical position, width and height of the component. You should be careful about this, because when you position a component, you need to pay attention to where it starts and ends, and your radio component is being inserted below the other, so it is not being displayed. This can be seen in these 2 lines:

adiciona(jbY, 190, 90, 190, 25);
adiciona(jbN, 220, 90, 190, 25);

The first radius is positioned on the X-axis at position 190 and is 190px in size. The other radius is positioned on the X-axis at position 220. Notice that the previous radius begins at 190 and goes up to 190 + 190, ie the next radius should be positioned at position 380 of the X axis, not at 220.

What I did was to reduce this absurd size of radiobutton to only 80, causing it to start at 190 going to 270, and starting the next radio at its side, just where it ends:

adiciona(jbY, 190, 90, 80, 25);
adiciona(jbN, 270, 90, 80, 25);

The final code is:

import java.awt.Component;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

public class JRadioButtonTest {

    public JPanel painel = new JPanel();
    private JFrame formulario = new JFrame();


    public JRadioButtonTest() {
        formulario.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        formulario.setTitle("dd");
        formulario.setSize(500, 270);
        // Formulário no centro da tela.
        formulario.setLocationRelativeTo(null);
        // --[ DESLIGANDO O GERENCIADOR DE LAYOUT ]--\
        painel.setLayout(null);
        formulario.add(painel);

        // Labels
        JLabel codigoInicial = new JLabel("Código inicial:");
        JLabel codigoFinal = new JLabel("Código final:");
        JLabel ignorarEntregues = new JLabel("Ignorar os já entregues:");

        // JTexts
        JTextField jL1 = new JTextField();
        JTextField jL2 = new JTextField();

        JRadioButton jbY = new JRadioButton("Sim", false);
        JRadioButton jbN = new JRadioButton("Não", true);

        // Adicionando os componentes
        adiciona(codigoInicial, 10, 10, 100, 25);
        adiciona(jL1, 190, 10, 190, 25);

        adiciona(codigoFinal, 10, 50, 100, 25);
        adiciona(jL2, 190, 50, 190, 25);

        adiciona(ignorarEntregues, 10, 90, 180, 25);

        adiciona(jbY, 190, 90, 80, 25);
        adiciona(jbN, 270, 90, 80, 25);

        formulario.setVisible(true);

    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> new JRadioButtonTest() );
    }

    private void adiciona(Component componente, int nColuna, int nLinha, int nLargura, int nAltura) {
        painel.add(componente);
        componente.setBounds(nColuna, nLinha, nLargura, nAltura);
    }
}

I recommend you read the links below:

Why should the main method dispatch GUI creation for EDT in a swing application?

Ways to Dispatch the EDT Interface

Avoiding Absolute Layouts

    
02.03.2018 / 20:03
2

The problem is that the "No" button is hidden behind the "Yes" button:

    adiciona(jbY, 190, 90, 190, 25);
    adiciona(jbN, 220, 90, 190, 25);

These coordinates mean that the "Yes" is 190 pixels wide, but the "No" button is only 30 pixels more to the right of it. The 190 pixel wide "Yes" is more than enough to hide all of the "No" content beneath it.

The solution is to reset the coordinates:

    adiciona(jbY, 190, 90, 100, 25);
    adiciona(jbN, 290, 90, 100, 25);

I also suggest considering what in that other question and use the mechanism described there for not creating your screen in the main thread. Keeping JFrame and JPanel in static variables is not a good idea either.

    
02.03.2018 / 20:04