This shows you the differences between two versions of the page.
| Both sides previous revision Previous revision Next revision | Previous revision | ||
|
swing:combo_box [2015/05/18 07:00] gthanos [Listener Interface] |
swing:combo_box [2018/04/13 08:27] |
||
|---|---|---|---|
| Line 1: | Line 1: | ||
| - | ====== Combo Boxes ====== | ||
| - | |||
| - | Ένα Combo Box αποτελείται από μία // | ||
| - | |||
| - | By default ένα Combo Box είναι // | ||
| - | <code java> | ||
| - | comboList.setEditable(true); | ||
| - | </ | ||
| - | |||
| - | ===== Listener Interface ===== | ||
| - | |||
| - | Ένα Combo Box συνδέεται με ένα ή περισσότερα αντικείμενα του τύπου | ||
| - | <code java> | ||
| - | public void actionPerformed(ActionEvent e) { | ||
| - | JComboBox cb = (JComboBox)e.getSource(); | ||
| - | String newOption = (String)cb.getSelectedItem(); | ||
| - | } | ||
| - | </ | ||
| - | |||
| - | Παρακάτω δίνονται δύο παραδείγματα από το site της Oracle, ένα για // | ||
| - | |||
| - | ==== Νon-editable combo box ==== | ||
| - | |||
| - | <code java ComboBoxDemo.java> | ||
| - | package components; | ||
| - | |||
| - | import java.awt.*; | ||
| - | import java.awt.event.*; | ||
| - | import javax.swing.*; | ||
| - | |||
| - | /* | ||
| - | * ComboBoxDemo.java uses these additional files: | ||
| - | | ||
| - | | ||
| - | | ||
| - | | ||
| - | | ||
| - | */ | ||
| - | public class ComboBoxDemo extends JPanel | ||
| - | implements ActionListener { | ||
| - | JLabel picture; | ||
| - | |||
| - | public ComboBoxDemo() { | ||
| - | super(new BorderLayout()); | ||
| - | |||
| - | String[] petStrings = { " | ||
| - | |||
| - | //Create the combo box, select the item at index 4. | ||
| - | //Indices start at 0, so 4 specifies the pig. | ||
| - | JComboBox petList = new JComboBox(petStrings); | ||
| - | petList.setSelectedIndex(4); | ||
| - | petList.addActionListener(this); | ||
| - | |||
| - | //Set up the picture. | ||
| - | picture = new JLabel(); | ||
| - | picture.setFont(picture.getFont().deriveFont(Font.ITALIC)); | ||
| - | picture.setHorizontalAlignment(JLabel.CENTER); | ||
| - | updateLabel(petStrings[petList.getSelectedIndex()]); | ||
| - | picture.setBorder(BorderFactory.createEmptyBorder(10, | ||
| - | |||
| - | //The preferred size is hard-coded to be the width of the | ||
| - | //widest image and the height of the tallest image + the border. | ||
| - | //A real program would compute this. | ||
| - | picture.setPreferredSize(new Dimension(177, | ||
| - | |||
| - | //Lay out the demo. | ||
| - | add(petList, | ||
| - | add(picture, | ||
| - | setBorder(BorderFactory.createEmptyBorder(20, | ||
| - | } | ||
| - | |||
| - | /** Listens to the combo box. */ | ||
| - | public void actionPerformed(ActionEvent e) { | ||
| - | JComboBox cb = (JComboBox)e.getSource(); | ||
| - | String petName = (String)cb.getSelectedItem(); | ||
| - | updateLabel(petName); | ||
| - | } | ||
| - | |||
| - | protected void updateLabel(String name) { | ||
| - | ImageIcon icon = createImageIcon(" | ||
| - | picture.setIcon(icon); | ||
| - | picture.setToolTipText(" | ||
| - | if (icon != null) { | ||
| - | picture.setText(null); | ||
| - | } else { | ||
| - | picture.setText(" | ||
| - | } | ||
| - | } | ||
| - | |||
| - | /** Returns an ImageIcon, or null if the path was invalid. */ | ||
| - | protected static ImageIcon createImageIcon(String path) { | ||
| - | java.net.URL imgURL = ComboBoxDemo.class.getResource(path); | ||
| - | if (imgURL != null) { | ||
| - | return new ImageIcon(imgURL); | ||
| - | } else { | ||
| - | System.err.println(" | ||
| - | return null; | ||
| - | } | ||
| - | } | ||
| - | |||
| - | /** | ||
| - | * Create the GUI and show it. For thread safety, | ||
| - | * this method should be invoked from the | ||
| - | * event-dispatching thread. | ||
| - | */ | ||
| - | private static void createAndShowGUI() { | ||
| - | //Create and set up the window. | ||
| - | JFrame frame = new JFrame(" | ||
| - | frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | ||
| - | |||
| - | //Create and set up the content pane. | ||
| - | JComponent newContentPane = new ComboBoxDemo(); | ||
| - | newContentPane.setOpaque(true); | ||
| - | frame.setContentPane(newContentPane); | ||
| - | |||
| - | //Display the window. | ||
| - | frame.pack(); | ||
| - | frame.setVisible(true); | ||
| - | } | ||
| - | |||
| - | public static void main(String[] args) { | ||
| - | //Schedule a job for the event-dispatching thread: | ||
| - | //creating and showing this application' | ||
| - | javax.swing.SwingUtilities.invokeLater(new Runnable() { | ||
| - | public void run() { | ||
| - | createAndShowGUI(); | ||
| - | } | ||
| - | }); | ||
| - | } | ||
| - | } | ||
| - | </ | ||
| - | |||
| - | Το παραπάνω πρόγραμμα προϋποθέτει τις παρακάτω εικόνες {{: | ||
| - | |||
| - | ==== Εditable combo box ==== | ||
| - | |||
| - | <code java ComboBoxDemo2.java> | ||
| - | |||
| - | package components; | ||
| - | |||
| - | import java.awt.*; | ||
| - | import java.awt.event.*; | ||
| - | import javax.swing.*; | ||
| - | import javax.swing.border.*; | ||
| - | import java.util.*; | ||
| - | import java.text.SimpleDateFormat; | ||
| - | |||
| - | /* ComboBoxDemo2.java requires no other files. */ | ||
| - | public class ComboBoxDemo2 extends JPanel | ||
| - | | ||
| - | static JFrame frame; | ||
| - | JLabel result; | ||
| - | String currentPattern; | ||
| - | |||
| - | public ComboBoxDemo2() { | ||
| - | setLayout(new BoxLayout(this, | ||
| - | String[] patternExamples = { | ||
| - | " | ||
| - | " | ||
| - | " | ||
| - | " | ||
| - | " | ||
| - | " | ||
| - | " | ||
| - | " | ||
| - | " | ||
| - | }; | ||
| - | |||
| - | currentPattern = patternExamples[0]; | ||
| - | |||
| - | //Set up the UI for selecting a pattern. | ||
| - | JLabel patternLabel1 = new JLabel(" | ||
| - | JLabel patternLabel2 = new JLabel(" | ||
| - | |||
| - | JComboBox patternList = new JComboBox(patternExamples); | ||
| - | patternList.setEditable(true); | ||
| - | patternList.addActionListener(this); | ||
| - | |||
| - | //Create the UI for displaying result. | ||
| - | JLabel resultLabel = new JLabel(" | ||
| - | JLabel.LEADING); | ||
| - | result = new JLabel(" | ||
| - | result.setForeground(Color.black); | ||
| - | result.setBorder(BorderFactory.createCompoundBorder( | ||
| - | | ||
| - | | ||
| - | )); | ||
| - | |||
| - | //Lay out everything. | ||
| - | JPanel patternPanel = new JPanel(); | ||
| - | patternPanel.setLayout(new BoxLayout(patternPanel, | ||
| - | | ||
| - | patternPanel.add(patternLabel1); | ||
| - | patternPanel.add(patternLabel2); | ||
| - | patternList.setAlignmentX(Component.LEFT_ALIGNMENT); | ||
| - | patternPanel.add(patternList); | ||
| - | |||
| - | JPanel resultPanel = new JPanel(new GridLayout(0, | ||
| - | resultPanel.add(resultLabel); | ||
| - | resultPanel.add(result); | ||
| - | |||
| - | patternPanel.setAlignmentX(Component.LEFT_ALIGNMENT); | ||
| - | resultPanel.setAlignmentX(Component.LEFT_ALIGNMENT); | ||
| - | |||
| - | add(patternPanel); | ||
| - | add(Box.createRigidArea(new Dimension(0, | ||
| - | add(resultPanel); | ||
| - | |||
| - | setBorder(BorderFactory.createEmptyBorder(10, | ||
| - | |||
| - | reformat(); | ||
| - | } // | ||
| - | |||
| - | public void actionPerformed(ActionEvent e) { | ||
| - | JComboBox cb = (JComboBox)e.getSource(); | ||
| - | String newSelection = (String)cb.getSelectedItem(); | ||
| - | currentPattern = newSelection; | ||
| - | reformat(); | ||
| - | } | ||
| - | |||
| - | /** Formats and displays today' | ||
| - | public void reformat() { | ||
| - | Date today = new Date(); | ||
| - | SimpleDateFormat formatter = | ||
| - | new SimpleDateFormat(currentPattern); | ||
| - | try { | ||
| - | String dateString = formatter.format(today); | ||
| - | result.setForeground(Color.black); | ||
| - | result.setText(dateString); | ||
| - | } catch (IllegalArgumentException iae) { | ||
| - | result.setForeground(Color.red); | ||
| - | result.setText(" | ||
| - | } | ||
| - | } | ||
| - | |||
| - | /** | ||
| - | * Create the GUI and show it. For thread safety, | ||
| - | * this method should be invoked from the | ||
| - | * event-dispatching thread. | ||
| - | */ | ||
| - | private static void createAndShowGUI() { | ||
| - | //Create and set up the window. | ||
| - | JFrame frame = new JFrame(" | ||
| - | frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | ||
| - | |||
| - | //Create and set up the content pane. | ||
| - | JComponent newContentPane = new ComboBoxDemo2(); | ||
| - | newContentPane.setOpaque(true); | ||
| - | frame.setContentPane(newContentPane); | ||
| - | |||
| - | //Display the window. | ||
| - | frame.pack(); | ||
| - | frame.setVisible(true); | ||
| - | } | ||
| - | |||
| - | public static void main(String[] args) { | ||
| - | //Schedule a job for the event-dispatching thread: | ||
| - | //creating and showing this application' | ||
| - | javax.swing.SwingUtilities.invokeLater(new Runnable() { | ||
| - | public void run() { | ||
| - | createAndShowGUI(); | ||
| - | } | ||
| - | }); | ||
| - | } | ||
| - | } | ||
| - | </ | ||
| - | |||
| - | |||