0

So I am making a Calendar program for my final project in a programming class and to add the days listed in the month I have a whole bunch of JLabels as instance variables (42 for a 6 by 7 grid)

JLabel day1;
JLabel day2;
JLabel day3;
JLabel day4;
JLabel day5;
JLabel day6;
JLabel day7;
JLabel day8;
JLabel day9;
JLabel day10;
etc.

then I have a method that gets the current month I have set and based off of what that returns I have a whole bunch of if statements to set what numbers go to which (I am planning to have the 42 in the set locations and just change their contents based off the month instead of moving the locations of 31)

if (Threads.getMonth() == 0)
    {
        day1 = new JLabel("");
        day2 = new JLabel("");
        day3 = new JLabel("");
        day4 = new JLabel("1");
        day5 = new JLabel("2");
        day6 = new JLabel("3");
        day7 = new JLabel("4");
        day8 = new JLabel("5");
        day9 = new JLabel("6");
        day10 = new JLabel("7");
etc.

my more experienced friend recommended creating the object within the if statements so this is what that looks like. Then after those twelve if statements I have the setting of boundaries and adding it to the frame.

day10.setBounds(100, 100, 500,500);
add(day10);

I had it at that location simply to test that it appeared and used day 10 because no matter which month the 10th day always had an actual value.

Although whenever I run this program the JLabel fails to show up and I cannot get my program to function without being able to do this. If it helps I have around 20 classes (one for each month, a main, a threads class, a gui, and a few others for some other functions that I am still working on.)

This bit of code is coming from the gui class. If it might have a play on this the way I have the classes interact is that the main class calls the threads class the threads get the date and open the class for that month that class opens the gui and then all button presses are run through a handler class that shoots the data to the threads class for getting more done and then you just press the x button to exit.

My gui class name is:

public class Gui extends JFrame

When I call the class I set the flow layout to null and it has worked for everything else(jcomboboxes, jbuttons).

When I use the add(day#) statement since it is extending jframe it doing that should add it to the frame and has worked for all other things including jlabels except for these.

  • could you provide a bit more code? specially your "class name" maube you're not extending from JFrame or you're creating a new JFrame inside your class. As it is that's all I can help you with. – Frakcool May 09 '14 at 18:05
  • Where are you `.add()`-ing your labels? Creating a new label doesn't inherently put it on the screen; you have to tell it **where** you want it to go. – Qix - MONICA WAS MISTREATED May 09 '14 at 18:07
  • Try using the FlowLayout on the parent panel. – Java42 May 09 '14 at 18:42
  • If you have a class for every month your design is flawed. Also create an array of labels and initialize them in a loop, don't create 42 declared instances. If you do want to create many instances of the same type without initializing them, you can write `JLabel label1, label2 label3;` to save many lines of code. – user1803551 May 09 '14 at 19:00
  • For you calendar representation, create a `GridLayout` of 6 by 7 and every month just needs to specify the *first* slot to start placing the labels at. You should use enumeration for the months and their first value, or something similar. – user1803551 May 09 '14 at 19:05
  • 1) For better help sooner, post an [MCVE](http://stackoverflow.com/help/mcve) (Minimal Complete and Verifiable Example). 2) Java GUIs might have to work on a number of platforms, on different screen resolutions & using different PLAFs. As such they are not conducive to exact placement of components. To organize the components for a robust GUI, instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556), along with layout padding & borders for [white space](http://stackoverflow.com/q/17874717/418556). – Andrew Thompson May 10 '14 at 01:53

1 Answers1

2

You need to add you labels to your panel

//adds label to panel
myPanel.add(label);

Then add panel to frame

//adds panel to frame
myFrame.add(myPanel);
javaGeek
  • 304
  • 1
  • 4
  • 11
  • Oh I wasn't using JPanels and I had the gui implementing jframe so I don't have to do the frame.add() and just add. I made a regular label to add and that worked so I fear it may be part of the if statements but I will try using a jPanel. Thanks. – DiggityDug May 10 '14 at 18:16