Position Jcombobox on the screen

Asked

Viewed 154 times

0

I made a combo box, in netbeans by command, and I can’t change it from location (in frame), Bounts values change, but it doesn’t move. I’ve used all kinds of commands (setBounds, setpoint, setLocation, etc.).

public JComboBox<String> combo_1 = new JComboBox<String>();
public JComboBox<String> combo_2 = new JComboBox<String>();

public Tela(){



    setTitle("Agreste Tour");
    setSize(900,720);
    setLocationRelativeTo(null);
    setDefaultCloseOperation(EXIT_ON_CLOSE);

    setVisible(true);

    setLayout(new BorderLayout());
    setContentPane(new JLabel(new ImageIcon(ClassLoader.getSystemResource("Imagem/Mapa.png"))));
    setLayout(new FlowLayout());

    setSize(900,720);
    setLayout(null);
    setResizable(false);
    setLayout(new FlowLayout());
    add(combo_1);
    add(combo_2);


    combo_1.setName("Origem");
    combo_1.setName("Destino");
    combo_1.setBounds(100, 500, 800, 100);
    System.out.println(""+combo_1.getBounds());


    JButton Okay = new JButton("Mostrar");
    getContentPane().add(Okay);
    Okay.addActionListener(this);

}

1 answer

2


Rather a recommendation:

Avoid using absolute layout, unless it is of extreme necessity and you know the consequences of it, because absolute layout makes it difficult to maintain the screen and makes your application look different depending on the monitor and resolution that is running.

There are several layouts so you don’t have to worry about positioning or manually organizing components. Not to mention that using layouts makes your code easier to maintain than inserting a lot of setbounds, and if you need to change the position of any component, in the absolute layout, you will have to reposition all manually.

But even though you want to insist on using absolute layout at your own risk, you cannot set a layout on the screen, and you are mistakenly setting layout 3 times. Remove the following lines:

setLayout(new FlowLayout());

and

setLayout(new BorderLayout());

leaving only setLayout(null); which is what will override any kind of layout and will indicate that you will be responsible for the positioning of everything on the screen.

Another error in this code is this line:

setContentPane(new JLabel(new ImageIcon(ClassLoader.getSystemResource("Imagem/Mapa.png"))));

If you’re going to use the screen to add more components, you can’t set a label like contentPane, because it doesn’t work very well as a container of other components, and a Contentpane represents the main container of the last level of a Frame(read more about here), where all other components will be added.

There are many other errors in this code that I will not correct to not escape the original scope of the question, but I advise you to study a little more about layouts and java-swing in itself, because small mistakes can generate a tremendous headache after the application is already partially developed, can often even force you to have to start all over again.

  • Okay, thank you very much!

  • @Marcosneto if the answer helped you, you can accept it by clicking on v, so you mark the question as concluded. :)

Browser other questions tagged

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