show the time in a combobox

2019-08-08 18:42发布

问题:

I'm trying to implement a ComboBox that shows the list of every hour of the day, without success. It gives me "The constructor JComboBox(JSpinner) is undefined" error. any help? Thank you

    Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.HOUR_OF_DAY, 24);
    calendar.set(Calendar.MINUTE, 0);
    SpinnerDateModel model = new SpinnerDateModel();
    model.setValue(calendar.getTime());
    JSpinner spinner = new JSpinner(model);
    departureTime = new JComboBox<String>(spinner);

回答1:

No, this makes no sense. You should be passing the combobox a ComboBoxModel filled with the values you want displayed

See How to use comboboxes and How to Use Spinners for more details

You can either use something like this which uses a JSpinner to display the time and allows the user to manipulate it

Or you need to fill the combobox with the values you want the user to be allowed to select, such as a given time interval...

import java.awt.Component;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridBagLayout;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import javax.swing.DefaultComboBoxModel;
import javax.swing.DefaultListCellRenderer;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Test {

    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        public TestPane() {
            setLayout(new GridBagLayout());
            Calendar calendar = Calendar.getInstance();
            calendar.set(Calendar.HOUR_OF_DAY, 0);
            calendar.set(Calendar.MINUTE, 0);

            Calendar end = Calendar.getInstance();
            end.set(Calendar.HOUR_OF_DAY, 23);
            end.set(Calendar.MINUTE, 59);
            DefaultComboBoxModel<Date> model = new DefaultComboBoxModel<>();
            do {
                model.addElement(calendar.getTime());
                calendar.add(Calendar.MINUTE, 15);
            } while (calendar.getTime().before(end.getTime()));

            JComboBox<Date> cb = new JComboBox<>(model);
            cb.setRenderer(new DateFormattedListCellRenderer(new SimpleDateFormat("HH:mm")));

            add(cb);

        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(200, 200);
        }

        public class DateFormattedListCellRenderer extends DefaultListCellRenderer {

            private DateFormat format;

            public DateFormattedListCellRenderer(DateFormat format) {
                this.format = format;
            }

            @Override
            public Component getListCellRendererComponent(JList<?> list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
                if (value instanceof Date) {
                    value = format.format((Date) value);
                }
                return super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
            }

        }

    }

}


回答2:

I dont know what u want to do with the calendar, but every day has 24 hours so add a list with 24 hours or create a spinner with maxvalue 24.