Position JCombobox on the screen

0

I made a combo box, in netbeans per command, and I can not change it's location (in the frame), the Bounts values change, but it does not 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);

}
    
asked by anonymous 21.07.2017 / 04:28

1 answer

3

Before a recommendation:

  

Avoid using absolute layout unless you are in dire need and know the consequences , because absolute layout makes it difficult to maintain the screen and make your application look different depending on the monitor and resolution being executed.

     

There are several layouts managers for you to you do not have to worry about manual positioning or organization of components. Not to mention that the use of layouts makes your code easier to maintain than inserting a lot of setbounds , and in case you need to change the position of any component, in the absolute layout, you will have to reposition them all manually.

But you still want to insist on using absolute layout at your own risk, you can not define a layout on the screen, and you're wrongly setting layout 3 times. Remove the following lines:

setLayout(new FlowLayout());

and

setLayout(new BorderLayout());

leaving only setLayout(null); which is what will cancel any type 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 are going to use the screen to add more components, you can not define a label as contentPane because it does not work very well as a container of other components, and a ContentPane represents the last level main container of a Frame (read more about here ) where all other components will be added.

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

    
21.07.2017 / 12:19