Estou com o seguinte problema. Criei um ComboBox e quero que sempre que eu selecionar o item no ComboBox um JLabel seja mudado a posição e o nome dele, porém sempre q eu seleciono o item apenas o nome é mudado, e pra mudar a posição eu tenho q selecionar o item dnv, tipo pra mudar o nome e a posição tenho q selecionar 2x o mesmo item. Se eu coloco só o código pra mudar a posição funciona, mas mudar a posição e o nome acontece oq citei acima.
if (Pick.getSelectedItem().equals("Teste") {
MudarNome.setText(h.getNome()[0]);
MudarNome.setLocation(180, 300);
}
*EDIT, fiz o teste errado. :\
if(cb.getSelectedItem().equals("oi")){
ola2.setText("OI");
ola2.setLocation(20, 80);
}else{
if(cb.getSelectedItem().equals("OLA")){
ola1.setLocation(60, 30);
ola1.setText("OLA");
}
}
Aqui tá indo de primeira…
Meu combobox possui como primeiro valor o -, e o restante, oi e OLA.
O seu está como quando vc abre a tela, não se se interfere, mas se já estiver com Teste, vc teria q escolher ele novamente para funcionar.
N adiantou
, criei um novo projeto só pra testar e coloquei igual o seu e n adianta, pra mudar a localização preciso clicar 2x
Item 2
Item 3
E etc
Tbm estou usando só 1 jlabel e n 2 igual vc fez, mas mesmo com 2 jlabel não adianta
Sendo assim, eu baixaria outro JDK, vai que né… seja só um bug.
Só pra saber, qual versão do seu jdk?
Testa ai @Douglas_3D
1. package mguj;
2. import java.awt.Container;
3. import java.awt.event.ActionEvent;
4. import java.awt.event.ActionListener;
5. import java.awt.event.FocusEvent;
6. import java.awt.event.FocusListener;
7. import javax.swing.JComboBox;
8. import javax.swing.JFrame;
9. import javax.swing.JLabel;
10. import javax.swing.JOptionPane;
11. public class MGUJ {
12. private static Container container;
13. private static JFrame tela;
14. private static JComboBox combo;
15. private static JLabel texto;
16.
17. public static void main(String[] args) {
18.
19. tela = new javax.swing.JFrame();
20. tela.setSize(400,250);
21. tela.setTitle("GUJ");
22. tela.setResizable(false);
23. tela.setLocationRelativeTo(null);
24. tela.setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
25. // Container da tela
26. container = tela.getContentPane();
27. container.setLayout(null);
28. // Adiciona
29. combo = new JComboBox(new String []{"Guj","Ola Mundo","Muda"});
30. combo.setBounds(20,20,100,20);
31. combo.addActionListener(new ActionListener() {
32. @Override
33. public void actionPerformed(ActionEvent e) {
34. texto.setLocation(60, 100);
35. texto.setText("Novo Texto");
36. }
37. });
38. texto = new JLabel("TEXTO");
39. texto.setBounds(40,70,100,20);
40. // Adiciona
41. container.add(combo);
42. container.add(texto);
43. // Abre tela
44. tela.setVisible(true);
45. }
46. }
Funcionou vlw ^^, sabe me dizer oq tava errado? acho q foi pq eu criei com o netbeans a interface talvez
Se seu componente está dentro de um container que possui gerenciador de layout, o setLocation não vai funcionar.
Recomendo estudar os gerenciadores de layout (layout managers) pois não é legal ficar ajustando a posição dos componentes pixel a pixel.
1 curtida