Advertisement
Guest User

Untitled

a guest
Apr 24th, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.08 KB | None | 0 0
  1. package testeCombo;
  2.  
  3. import java.awt.Dimension;
  4. import java.awt.EventQueue;
  5. import java.awt.GridLayout;
  6. import java.awt.event.ActionEvent;
  7. import java.awt.event.ActionListener;
  8. import java.util.Arrays;
  9. import javax.swing.JButton;
  10. import javax.swing.JComboBox;
  11. import javax.swing.JFrame;
  12. import javax.swing.JLabel;
  13. import javax.swing.JPanel;
  14. import javax.swing.JTextField;
  15.  
  16. public class MainCombo extends JFrame implements ActionListener
  17. {
  18. private final String logradouro[] = {"<Selecione>", "Avenida", "Rua", "Estrada"};
  19. private JComboBox cmb = new JComboBox();
  20. private JTextField tf = new JTextField();
  21. JPanel painel = new JPanel();
  22. JPanel painelBotao = new JPanel();
  23. private final JButton incluir = new JButton("OK");
  24. private final JButton consultar = new JButton("CONSULTAR");
  25.  
  26. private String salvaBD = "";
  27. JLabel label = new JLabel();
  28. JLabel label2 = new JLabel();
  29.  
  30. public MainCombo()
  31. {
  32. setTitle("Combo + Text");
  33. setSize(550, 150);
  34. setResizable(false);
  35. cmb = new JComboBox(logradouro);
  36. tf = new JTextField();
  37. tf.setPreferredSize(new Dimension(110, 25));
  38. painel.add(cmb);
  39. painel.add(tf);
  40. getContentPane().add("South", painelBotao);
  41. painelBotao.setLayout(new GridLayout(1, 2));
  42. adicionaBotao(incluir);
  43. adicionaBotao(consultar);
  44. painel.add(label);
  45. painel.add(label2);
  46. add(painel);
  47. setLocationRelativeTo(null);
  48. setDefaultCloseOperation(EXIT_ON_CLOSE);
  49. }
  50.  
  51. public void acao()
  52. {
  53. cmb.getSelectedItem();
  54. tf.getText();
  55. salvaBD = (cmb.getSelectedItem() + " " + tf.getText());
  56. label.setText(salvaBD);
  57. cmb.setSelectedIndex(0);
  58. tf.setText("");
  59. }
  60.  
  61. public void consultar()
  62. {
  63. separa();
  64. }
  65.  
  66. public void separa()//separa para quando consultar, o logradouro ir para o combo e o nome do logradouro pro TextField
  67. {
  68. String[] arrayValores = salvaBD.split(" ");
  69. label2.setText(" " + Arrays.toString(arrayValores));
  70. //cmb.setSelectedIndex(); //setar valor no textField quando consulta
  71. //tf.setValor(salvaBD); //setar valor no textField quando consulta
  72. label.setText("");// so para limpar o outro label
  73. }
  74.  
  75.  
  76. @Override
  77. public void actionPerformed(ActionEvent ae)
  78. {
  79.  
  80. if (ae.getSource() == incluir)
  81. {
  82. acao();
  83. }
  84.  
  85. if (ae.getSource() == consultar)
  86. {
  87. consultar();
  88. }
  89. }
  90.  
  91. private void adicionaBotao(JButton botao)
  92. {
  93. painelBotao.add(botao);
  94. botao.addActionListener(this);
  95. }
  96.  
  97. public static void main(String[] args)
  98. {
  99. EventQueue.invokeLater(() ->
  100. {
  101. MainCombo combo = new MainCombo();
  102. combo.setVisible(true);
  103. });
  104. }
  105.  
  106. class textF extends JTextField
  107. {
  108. public void setValor(Object valor)
  109. {
  110. setText("" + valor);
  111. }
  112. }
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement