Search code examples
javaswingjframejpanel

Trying to display 2 JPanels in a JFrame


My goal is to create a square box underneath the calendar where I can display events when the date is clicked on as I progress in my program. First, I need to create that box. Here is my current code:

package calendar;
import javax.swing.JFrame; 
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.table.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;


public class CalendarView extends JFrame  {
    DefaultTableModel table;
    Calendar cal = new GregorianCalendar();
    JLabel label;
    JLabel eventsLabel;
    

    CalendarView() {
        this.setTitle("Sai's Calendar :}");
        this.setSize(400, 400); 
        this.setResizable(false);
        this.setLayout(new BorderLayout());
        this.setVisible(true);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


        label = new JLabel("Sais Calendar :)");
        eventsLabel = new JLabel("EVENTS");
        
        //label.setHorizontalAlignment(SwingConstants.CENTER);
        
        JButton create = new JButton("CREATE");
        create.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent ae) {
                 
            }
        });
        
        JButton quit = new JButton("QUIT");
        create.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent ae) {
                 
            }
        });
        
        

        //making prev & next buttons 
        JButton prev = new JButton("<<");
        prev.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent ae) {
                cal.add(Calendar.MONTH, -1);
                monthUpdate(); 
            }
        });


        JButton next = new JButton(">>"); 
        next.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent ae) {
                cal.add(Calendar.MONTH, +1);
                monthUpdate(); 
            }
        });
        
        JPanel root = new JPanel(new BorderLayout());

        JPanel panel = new JPanel();
        panel.setLayout(new BorderLayout());
        panel.add(prev,BorderLayout.WEST);
        panel.add(label,BorderLayout.SOUTH);
        panel.add(next,BorderLayout.EAST);
        panel.add(create,BorderLayout.NORTH);
        panel.add(quit,BorderLayout.CENTER);
        
        
        JPanel events = new JPanel();
        events.setLayout(new BorderLayout());
        events.setBackground(Color.red);
        events.setPreferredSize(getPreferredSize());
        
        
        
        String[] days = {"Sun", "Mon", "Tue", "Wed", "Thurs", "Fri", "Sat"}; 
        table = new DefaultTableModel(null, days);
        JTable caltable = new JTable(table);
        caltable.setCellSelectionEnabled(true);
        caltable.getSelectionModel().setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

        // listen for cell selections using mouse listener to listen for clicks
        caltable.addMouseListener(new MouseListener() {
            public void mousePressed(MouseEvent e) {
                int row = caltable.getSelectedRow();
                int column = caltable.getSelectedColumn();

                //going to have to this to get stuff done on this date on calendar metohod 
                Object data = table.getValueAt(row, column);
            }
            public void mouseClicked(MouseEvent e) { }
            public void mouseReleased(MouseEvent e) { }
            public void mouseEntered(MouseEvent e) { }
            public void mouseExited(MouseEvent e) { }
        });
        JScrollPane pane = new JScrollPane(caltable);

        this.add(panel,BorderLayout.NORTH);
        this.add(pane,BorderLayout.CENTER);
        this.add(events,BorderLayout.SOUTH);

        this.monthUpdate();


    }

    protected void monthUpdate() {
        cal.set(Calendar.DAY_OF_MONTH, 1);
        String month = cal.getDisplayName(Calendar.MONTH, Calendar.LONG, Locale.US);
        int year = cal.get(Calendar.YEAR);
        label.setText(month + " " + year);

        int firstDay = cal.get(Calendar.DAY_OF_WEEK);
        int daysInMonth = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
        int weeksInMonth = cal.getActualMaximum(Calendar.WEEK_OF_MONTH);

        table.setRowCount(0);
        table.setRowCount(weeksInMonth);

        int x = firstDay - 1;

        for(int i=1;i <=daysInMonth; i++){
            table.setValueAt(i, x/7 , x%7 );    
            x = x + 1;
        }

    }


}

here is my current output:

https://i.sstatic.net/sIWOd.png

As you can see, the events panel is a tiny rectangle at the bottom, I would like this to be a square preferrable to right of the calendar, but the bottom is fine as well.

Can anyone please help me figure out how I can implement this


Solution

  • I would say the issue with your code is that your calendar takes up too much space. Your calendar will only take up 5 rows.

    You should allow your "red" panel to grow to fill the extra space. Therefore it should be the panel added to the "CENTER" of the border layout.

    So you need to create another "wrapper" panel:

    this.add(panel,BorderLayout.NORTH);
    //this.add(pane,BorderLayout.CENTER);
    JPanel wrapper = new JPanel( new BorderLayout() );
    this.add(wrapper, BorderLayout.CENTER);
    wrapper.add(pane, BorderLayout.PAGE_START);
    //this.add(events,BorderLayout.SOUTH);
    wrapper.add(events, BorderLayout.CENTER);
    
    this.monthUpdate();
    table.setPreferredScrollableViewportSize(table.getPreferredSize());
    

    The last statement will allow the scroll pane to size itself to completely display the table with no extra space.