0

I have created a simple calendar using swing.I used two panels inside the frame.Both the panels have null layout and the frame also.The program runs fine but no contents are appear until i maximize or minimize the window or resize the window.

Here's mine code:

import java.awt.Color;
import java.time.DayOfWeek;
import java.time.LocalDate;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class CalendarTest {
JFrame f;
JLabel monthYearLbl;
JLabel[] dayLbl,dateLbl;
JPanel monthAndYear,dayAndDate;
LocalDate ld;
String month,year,day;

//constructor
public CalendarTest(){
    f = new JFrame("Calender");
    f.setSize(365,250);
    f.setLayout(null);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setLocationRelativeTo(null);
    f.setVisible(true);
}
public void showMonthAndYear(){
    monthAndYear = new JPanel();
    monthAndYear.setBounds(0,0,350,25);
    f.add(monthAndYear);
    monthAndYear.setBackground(Color.MAGENTA);
    ld = LocalDate.now();
    month = String.valueOf(ld.getMonth());
    year = String.valueOf(ld.getYear());
    monthYearLbl = new JLabel(month+"-"+year);
    monthAndYear.add(monthYearLbl);
}
public void showDayAndDate(){
    dayAndDate = new JPanel();
    dayAndDate.setLayout(null);
    dayAndDate.setBounds(0,27,350,185);
    f.add(dayAndDate);
    dayAndDate.setBackground(Color.cyan);
    //getting the first day name of the current month
    DayOfWeek dow = LocalDate.of(ld.getYear(),ld.getMonth(),1).getDayOfWeek();
    int lengthOfMonth = LocalDate.of(ld.getYear(),ld.getMonth(),1).lengthOfMonth();
    int friday=0;
    dayLbl = new JLabel[8];
    int xCord = 10;
    for(int i=0;i<7;i++){
        String s = String.valueOf(dow);//converting dow object to string 
        String formatedDay = String.format("%.3s",s);//formating day in short form
        if(formatedDay== "FRI"){
            friday = i;
        }
        dayLbl[i] = new JLabel(formatedDay);//creating jlabel
        dow = dow.plus(1);//incrementing day name
        dayLbl[i].setBounds(xCord,10,30,20);
        dayAndDate.add(dayLbl[i]);
        xCord = xCord+50;//dynamic positioning of day name
    }
    dateLbl = new JLabel[35];
    int index = 1,ycord=35;
    //loop for showing the date from 1 to endofmonth
    for(int i=1;i<=5;i++){
        int xcord = 15;
        for(int j=0;j<7;j++){
            dateLbl[index] = new JLabel(String.valueOf(index));
            dateLbl[index].setBounds(xcord,ycord,20,20);
            if(j==friday){
                dateLbl[index].setBackground(Color.black);//indicating holiday
            }
            dayAndDate.add(dateLbl[index]);
            xcord = xcord+50;
            if(index==lengthOfMonth){
                break;
            }
            index++;
            f.add(dayAndDate);
        }
        ycord = ycord+25;
    }
}
public static void main(String[] args) {
    CalendarTest calendar = new CalendarTest();
    calendar.showMonthAndYear();
    calendar.showDayAndDate();

}

}

For better understanding click here to get the jar format of the program.

Screenshot :

Before resizing the window

Before resizing the window

After resizing the window

enter image description here

SHB
  • 585
  • 6
  • 14
  • call `setVisible(true);` after adding all components.or call revalidate and repaint methods – Madhawa Priyashantha Jun 26 '16 at 17:41
  • @FastSnail I tried setVisible(true) but it not works.I don't have any idea about 'revalidate and repaint' are they something like setVisible method?? – SHB Jun 26 '16 at 17:50
  • 1
    Thanks it works now..After adding 'revalidate' on the first panel and 'repaint' on the 2nd panel.. – SHB Jun 26 '16 at 17:55
  • 1
    1) Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). 2) See [Detection/fix for the hanging close bracket of a code block](http://meta.stackexchange.com/q/251795/155831) for a problem I could no longer be bothered fixing. – Andrew Thompson Jun 27 '16 at 00:02

2 Answers2

1

Answering my own Question:

After adding 'revalidate' on the 1st panel and 'repaint' on the 2nd panel it works now..

SHB
  • 585
  • 6
  • 14
-1

You forgot to call setVisible(true) on your components, like Fast Snail mentioned. You should also call the repaint method of your JPanel.

serebit
  • 400
  • 2
  • 13
  • Thanks.. it works now..After adding 'revalidate' on the first panel and 'repaint' on the 2nd panel. – SHB Jun 26 '16 at 17:57