tags:

views:

38

answers:

2

Alright, so I'm simply defining a combo box like so...

JComboBox yearSelect = new JComboBox(); 

Now, I have not even added this to a panel or anything, I've just defined it. When I run the app, nothing displays.. when I comment out this line.. the app displays the other panels like I want it to.. is there something I'm doing wrong? Maybe I'm forgetting something that has to do with combo boxes? I think it may be something stupid that I'm missing.

Here is my entire constructor for the content.

private Container pane;         // content pane
private JPanel calendarPanel;   // where our calendar will go
private JPanel datePanel;       // where "todays date" will go
private JPanel pnlToolbar;      // tool bar for moving in the year
private JPanel bottomPanel;
private JPanel yearPanel;
private JLabel lbCurrentMonth;
private JLabel dateLabel;
private JButton monthBack;
private JButton monthForward;
private JComboBox yearSelect;

private Date today = new Date();
private int currentMonth = today.getMonth();
private int currentYear = today.getYear(); 
final private String [] days = {"Sunday", "Monday", "Tuesday",
                                "Wednesday", "Thursday", "Friday",
                                "Saturday"};

final private String [] months = {"January", "Febuary", "March", "April",
                                  "May", "June", "July", "August",
                                  "September", "October", "November",
                                  "December"};


public Calendar() {

    // call the calendar frame class constructor (Window class)
    // this function will setup our basic window
    super();

    // define the current date of the calendar as today



    // below are attributes to our window. They define what content
    // is on them.


    // define our window
    pane = window.getContentPane();
    pane.setLayout(new BorderLayout());

    calendarPanel = new JPanel(new FlowLayout());
    calendarPanel.setBorder(BorderFactory.createTitledBorder("Calendar"));

    pnlToolbar = new JPanel(new FlowLayout(FlowLayout.CENTER, 40, 5));
    pnlToolbar.setSize(300, 45);


    //////////////////////////////////////////////////////////////////////
    /* setup our lower date panel, that displays todays date
    and the year drop down menu */

    // for "todays date"
    dateLabel = new JLabel(returnDateString());
    dateLabel.setFont(new Font(null, Font.BOLD, 12));

    datePanel = new JPanel(new FlowLayout(FlowLayout.LEFT, 5, 5));
    datePanel.add(new JLabel("Todays Date: "), BorderLayout.WEST);
    datePanel.add(dateLabel, BorderLayout.EAST);

    // for "year select"
    // yearSelect = new JComboBox(); 

    yearPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT, 5, 5));
    //yearPanel.add(yearSelect);

    bottomPanel = new JPanel(new BorderLayout());
    bottomPanel.add(datePanel, BorderLayout.WEST);
    //bottomPanel.add(yearPanel, BorderLayout.EAST);

    // setup the tool bar panel
    lbCurrentMonth = new JLabel(months[currentMonth]);
    monthBack = new JButton("<<");
    monthForward = new JButton(">>");
    monthForward.setEnabled(true);
    monthBack.setEnabled(true);

    pnlToolbar.add(monthBack);
    pnlToolbar.add(lbCurrentMonth);
    pnlToolbar.add(monthForward);



    // add everything to the content panel
    pane.add(calendarPanel, BorderLayout.CENTER);
    pane.add(bottomPanel, BorderLayout.SOUTH);
    pane.add(pnlToolbar, BorderLayout.NORTH);
+2  A: 

You need to add a model to display your data. See JComboBox.setModel.

Chuk Lee
Why setModel is required on *every* JComboBox to display data?
ring bearer
A model is a representation of your data. There is a clear separation between the view and the model in Swing controls so you can represent your data in the most optimal way. Also there are 2 types of models, ComboBoxModel and MutableComboBoxModel which tells the combobox if your data is updatable. Yes it is a pain if you are just displaying simple data, but incredibly flexible if your calendar, for example, also lookup holdiays, your alarms, appointments etc and annotate. If you further set a cell renderer then you can get a instance of your data and render it in your way instead of a String.
Chuk Lee
A: 

Yea still nothing, I've attached the rest of my code above. Even when I don't add the object to a panel, it does this. I'm simply instantiating the object, nothing more. Any ideas? Also, I've created a seperate application to make sure I'm doing the combo boxes correctly here is something I noticed. It doesnt the exact same thing in the sperate application.. below.. (very dirty)

public class Main {


public static void main(String[] args) {
    String [] selectFill = {"Cat", "Dog"};
    JFrame window = new JFrame("Window");
    window.setBounds(0, 0 , 800, 600);
    window.setVisible(true);
    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    window.setLayout(null); 

    //JComboBox selectBox = new JComboBox(selectFill);
    JLabel check = new JLabel("Select: ");
    JPanel contentPanel = new JPanel(new FlowLayout());
    Container pane = window.getContentPane();
    pane.add(contentPanel);
    pane.setLayout(new FlowLayout()); 
    contentPanel.add(check);
    //contentPanel.add(selectBox);
}

}

For some strange reason when I resize the window, or minmize it loads the content. Or.. if I comment out the line for the combo box object it displays the content as well.

Dalton Conley