Advertisement
Guest User

Untitled

a guest
Jun 22nd, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 2.75 KB | None | 0 0
  1. import javax.swing.*;
  2. import java.awt.*;
  3. import java.awt.event.ActionEvent;
  4. import java.awt.event.ActionListener;
  5.  
  6. public class ComboBoxes {
  7. // ------------------------------ FIELDS ------------------------------
  8.  
  9.     private ActionListener automobilListener = new ActionListener() {
  10.         @Override
  11.         public void actionPerformed(ActionEvent e) {
  12.             Automobil automobil = (Automobil) ((JComboBox) e.getSource()).getSelectedItem();
  13.             if (automobil != null)
  14.                 comboBox2.setModel(new DefaultComboBoxModel<>(db.getModelos(automobil.id)));
  15.         }
  16.     };
  17.     private ListCellRenderer automobilRenderer = new DefaultListCellRenderer() {
  18.         @Override
  19.         public Component getListCellRendererComponent(JList<?> list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
  20.             super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
  21.             setText(((Automobil) value).nombre);
  22.             return this;
  23.         }
  24.     };
  25.  
  26.     private JComboBox<Automobil> comboBox1;
  27.     private JComboBox<String> comboBox2;
  28.     private JPanel contentPane;
  29.     private Db db;
  30.  
  31.     private JPanel init() {
  32.         db = new Db();
  33.  
  34.         comboBox1.setModel(new DefaultComboBoxModel<>(db.getAutomobiles()));
  35.         comboBox1.addActionListener(automobilListener);
  36.         comboBox1.setRenderer(automobilRenderer);
  37.  
  38.         comboBox2.setModel(new DefaultComboBoxModel<>(db.getModelos(1)));
  39.  
  40.         return contentPane;
  41.     }
  42.  
  43.     class Automobil {
  44.         int id;
  45.         String nombre;
  46.  
  47.         Automobil(int id, String nombre) {
  48.             this.id = id;
  49.             this.nombre = nombre;
  50.         }
  51.     }
  52.  
  53.     class Db {
  54.         Automobil[] CARS = {
  55.                 new Automobil(1, "FORD"),
  56.                 new Automobil(2, "HONDA"),
  57.                 new Automobil(3, "TOYOTA")
  58.         };
  59.  
  60.         Automobil[] getAutomobiles() {
  61.             return CARS;
  62.         }
  63.  
  64.         String[] getModelos(int autoId) {
  65.             if (autoId == 1) {
  66.                 return new String[]{"TAURUS"};
  67.             } else if (autoId == 2) {
  68.                 return new String[]{"ACCORD"};
  69.             } else if (autoId == 3) {
  70.                 return new String[]{"PRIUS"};
  71.             } else {
  72.                 return new String[0];
  73.             }
  74.         }
  75.     }
  76.  
  77. // --------------------------- main() method ---------------------------
  78.  
  79.     public static void main(String[] args) {
  80.         JFrame frame = new JFrame("<class name>");
  81.         frame.setContentPane(new ComboBoxes().init());
  82.         frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  83.         frame.setMinimumSize(new Dimension(400, 200));
  84.         frame.pack();
  85.         frame.setVisible(true);
  86.     }
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement