Correct display of Radionbuttons

Asked

Viewed 49 times

0

I’m trying to implement a JRadioButton but after entering the 2 in the form, only one is displayed, someone 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 gets like this when I run:

inserir a descrição da imagem aqui

How to Show Other Radio ?

  • Provide a [mcve] so that it is possible to execute the code and simulate the problem.

  • Done @Articuno

  • Why ta using absolute layout? It’s a bad practice unless you know how much it gives headache, like this your problem.

  • @Articuno I’m learning these Frames yet.... so I don’t know how to solve.

2 answers

2


The problem is the fact that you are using absolute layout, and this is a very bad practice, because it manages 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 any components, you will have to move them all.

The method setBounds receives 4 parameters, being them the horizontal and vertical position, the width and the height of the component. Care should be taken with this, because when positioning a component, you need to pay attention to where the previous one begins and ends, and your radio component is being inserted below the other, so it is not being displayed. This can be observed in these 2 lines:

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

The first radio is positioned on the X-axis at position 190 and is 190px in size. The other radio is positioned on the X-axis at position 220. Note that the former starts at 190 and goes up to 190+190, that is, the following should be positioned at position 380 of the X axis, and not at 220.

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

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

The final code remained:

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 the main method should dispatch the creation of the GUI to the EDT in a swing application?

Ways to dispatch the interface to the EDT

Avoiding using absolute layouts

  • Your answer preceded mine by a few seconds, and you’re better than her. I would just recommend adding a part about not using static variables for that sort of thing and then it would be perfect.

  • Thank you, I unfortunately have no knowledge in these J. I’m doing this only as branch break since my app was all dependent on command line and was bad for the user. What is the best layout for this case of mine ?

  • @Douglas There is no "the best", there is the one that best suits your situation. You can make this screen there with all the java layouts quietly, some will be more complex, others can be simpler, is relative.

  • @Victorstafusa opa, you edit later with this information, thank you :)

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 than it is. The 190 pixels wide of the "Yes" are more than enough to hide all the contents of the "No" below it.

The solution is to reset the coordinates:

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

I suggest also consider what there is in that other question and use the mechanism described there not to create your screen in the main thread. Keep JFrame and JPanel on static variables is also not a good idea.

  • Thank you, I took this example ready.. rs, I will study this technology further.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.