Advertisement
ciskos

Test

Jun 25th, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.80 KB | None | 0 0
  1. import java.awt.BorderLayout;
  2. import java.awt.EventQueue;
  3. import java.awt.event.ActionEvent;
  4. import java.awt.event.ActionListener;
  5.  
  6. import javax.swing.DefaultComboBoxModel;
  7. import javax.swing.JComboBox;
  8. import javax.swing.JFrame;
  9. import javax.swing.JTextField;
  10.  
  11. public class Test {
  12.  
  13.     private JFrame frame;
  14.     private JTextField textField;
  15.  
  16.     /**
  17.      * Launch the application.
  18.      */
  19.     public static void main(String[] args) {
  20.         EventQueue.invokeLater(new Runnable() {
  21.             public void run() {
  22.                 try {
  23.                     Test window = new Test();
  24.                     window.frame.setVisible(true);
  25.                 } catch (Exception e) {
  26.                     e.printStackTrace();
  27.                 }
  28.             }
  29.         });
  30.     }
  31.  
  32.     /**
  33.      * Create the application.
  34.      */
  35.     public Test() {
  36.         initialize();
  37.     }
  38.  
  39.     /**
  40.      * Initialize the contents of the frame.
  41.      */
  42.     private void initialize() {
  43.         frame = new JFrame();
  44.         frame.setBounds(100, 100, 450, 300);
  45.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  46.        
  47.         String[][] items = {{"alias1", "select1..."}, {"alias2", "select2..."}, {"alias3", "select3..."}};
  48.         String[] alias = new String[items.length];
  49.         String[] select = new String[items.length];
  50.         JComboBox comboBox = new JComboBox();
  51.         comboBox.addActionListener(new ActionListener() {
  52.             public void actionPerformed(ActionEvent e) {
  53.                 int i = comboBox.getSelectedIndex();
  54.                 textField.setText(select[i]);
  55. //              System.out.println(select[i]);
  56.             }
  57.         });
  58.  
  59.         for (int i = 0; i < items.length; i++) {
  60.             alias[i] = items[i][0];
  61.         }
  62.  
  63.         for (int i = 0; i < items.length; i++) {
  64.             select[i] = items[i][1];
  65.         }
  66.        
  67.         comboBox.setModel(new DefaultComboBoxModel(alias));
  68.         frame.getContentPane().add(comboBox, BorderLayout.CENTER);
  69.        
  70.         textField = new JTextField();
  71.         frame.getContentPane().add(textField, BorderLayout.SOUTH);
  72.         textField.setColumns(10);
  73.     }
  74.  
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement