Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.BorderLayout;
- import java.awt.EventQueue;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import javax.swing.DefaultComboBoxModel;
- import javax.swing.JComboBox;
- import javax.swing.JFrame;
- import javax.swing.JTextField;
- public class Test {
- private JFrame frame;
- private JTextField textField;
- /**
- * Launch the application.
- */
- public static void main(String[] args) {
- EventQueue.invokeLater(new Runnable() {
- public void run() {
- try {
- Test window = new Test();
- window.frame.setVisible(true);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- });
- }
- /**
- * Create the application.
- */
- public Test() {
- initialize();
- }
- /**
- * Initialize the contents of the frame.
- */
- private void initialize() {
- frame = new JFrame();
- frame.setBounds(100, 100, 450, 300);
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- String[][] items = {{"alias1", "select1..."}, {"alias2", "select2..."}, {"alias3", "select3..."}};
- String[] alias = new String[items.length];
- String[] select = new String[items.length];
- JComboBox comboBox = new JComboBox();
- comboBox.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- int i = comboBox.getSelectedIndex();
- textField.setText(select[i]);
- // System.out.println(select[i]);
- }
- });
- for (int i = 0; i < items.length; i++) {
- alias[i] = items[i][0];
- }
- for (int i = 0; i < items.length; i++) {
- select[i] = items[i][1];
- }
- comboBox.setModel(new DefaultComboBoxModel(alias));
- frame.getContentPane().add(comboBox, BorderLayout.CENTER);
- textField = new JTextField();
- frame.getContentPane().add(textField, BorderLayout.SOUTH);
- textField.setColumns(10);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement