Tri Nguyen
Tri Nguyen

Reputation: 1808

Java - Display swing GUI

My appointmentViewWeeks constructor receives parameters from other classes and creates a GUI. How can I call it from my main method without knowing the values of year and month?

public class appointmentViewWeeks extends Frame{

    public appointmentViewWeeks(String year,String month){

        List<LocalDate> mondays = new ArrayList<LocalDate>();
        Month m = Month.valueOf(monthIntToString(month).toUpperCase());

        LocalDate date = Year.of(Integer.valueOf(year)).atMonth(m).atDay(1).
             with(TemporalAdjusters.firstInMonth(DayOfWeek.MONDAY));
        Month mi = date.getMonth();
        while (mi == m) {
            mondays.add(date);
            date = date.with(TemporalAdjusters.next(DayOfWeek.MONDAY));
            mi = date.getMonth();
        }
        String[] mondaysList = mondays.toArray(new String[0]);

        JComboBox mondayList = new JComboBox(mondaysList);
        JLabel welcome = new JLabel("Please choose a monday in "+month+"/"+year);

        Container contentPane = getContentPane();
        contentPane.add(mondayList);
        contentPane.add(welcome);
    }

    public String monthIntToString(String month) {
         return new DateFormatSymbols().getMonths()[Integer.valueOf(month)-1];
    }

    public static void main(String[] args) {

        //windows feel and look
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (ClassNotFoundException | 
            InstantiationException | 
            IllegalAccessException | 
            UnsupportedLookAndFeelException e) {
                e.printStackTrace();
            }
        appointmentViewWeeks view = new appointmentViewWeeks();
        view.setVisible(true);
    }
}

Upvotes: 0

Views: 154

Answers (1)

Gulllie
Gulllie

Reputation: 558

You will have to pass in a year and month to create it (or just two Strings), unless you add a second constructor without the String parameters:

public appointmentViewWeeks() {
    //Code to create the object without using the String parameters...
    //Maybe just call your original constructor with some default values?
}

You could also get the current year and month, and create it with your current constructor:

LocalDateTime date = LocalDateTime.now();
appointmentViewWeeks view = new appointmentViewWeeks(String.valueOf(date.getYear()),
                                                        String.valueOf(date.getMonth()));

Upvotes: 1

Related Questions