Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calendar GridLayout

I'm having difficulties getting my header to be on it's own line. The header runs into the days of the week instead of showing the header and then at the bottom the number of days in the calendar. Is this missing a panel? I've tried multiple changes but I can't really tell

package Exercise15_5;
import java.awt.*;
import javax.swing.*;
import java.util.*;

public class Exercise15_5 extends JFrame {
    public Exercise15_5(){
        //Create panel with gridlayout
        JPanel calendar = new JPanel(new BorderLayout());
        calendar.setLayout(new GridLayout(5,7));
        //Add headers
        String[] headers = {"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"};
        for(int i = 0; i <7; i++){
            calendar.add(new JLabel("" + headers[i]));
        }
        //Add days to calendar
        for(int i = 1; i <31; i++){
            calendar.add(new JLabel("" + i));
        }

        JPanel monthHeader = new JPanel(new BorderLayout());
        monthHeader.add(new JTextField("\t\t\t04/2014"), BorderLayout.NORTH);

        monthHeader.add(calendar, BorderLayout.CENTER);



        add(monthHeader);
    }

    public static void main(String[] args) {
        Exercise15_5 frame = new Exercise15_5();
        frame.setTitle("Exercise 15_5");
        frame.setSize(600,300);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);


    }

}
like image 552
MontyMax Avatar asked Apr 13 '14 03:04

MontyMax


2 Answers

Try setting the layout of your panel to new GridLayout(0,7) instead.

The GridLayout API states the following:

When both the number of rows and the number of columns have been set to non-zero values, either by a constructor or by the setRows and setColumns methods, the number of columns specified is ignored. Instead, the number of columns is determined from the specified number of rows and the total number of components in the layout. So, for example, if three rows and two columns have been specified and nine components are added to the layout, they will be displayed as three rows of three columns. Specifying the number of columns affects the layout only when the number of rows is set to zero.

like image 103
PakkuDon Avatar answered Sep 30 '22 03:09

PakkuDon


Your row count was off by 1 (but 0 is more general), I also recommend you clean up your constructor a little. Maybe like this,

// Create panel with gridlayout
JPanel calendar = new JPanel(new GridLayout(6, 7)); // 6 (or 0), not 5.
// Add headers
String[] headers = { "Sunday", "Monday",
    "Tuesday", "Wednesday", "Thursday", "Friday",
    "Saturday" };
// Use for-each loop.
for (String header : headers) {
  calendar.add(new JLabel(header));
}
// Add days to calendar, use String.valueOf
for (int i = 1; i < 31; i++) {
  calendar.add(new JLabel(String.valueOf(i)));
}

JPanel monthHeader = new JPanel(new BorderLayout());
monthHeader.add(new JTextField("\t\t\t04/2014"),
    BorderLayout.NORTH);

monthHeader.add(calendar, BorderLayout.CENTER);

add(monthHeader);
like image 21
Elliott Frisch Avatar answered Sep 30 '22 01:09

Elliott Frisch