EightSquared
EightSquared

Reputation: 37

JTable positioning

I'm creating a calendar application using Swing/Java and the MVC pattern. I'm trying to position a JTable to something that resembles the image below, however This.setSize and table.setPreferedSize don't seem to be doing the job. Any feedback is appreciated.

Current GUI: http://gyazo.com/f1d4a3e8b08e40440af5e1c514727be8 Expected GUI: http://gyazo.com/8352843f58eb116a7334f2b01c40c1a4

    public class CalenderView extends JFrame {

//Eclipse freaks out if this isnt here.
private static final long serialVersionUID = 1L;
//JPanel houses the JFrame
JPanel CalenderPanel = new JPanel();
//Table takes in cell values
JTable table = new JTable(5,7);

//This is a Label which has a getter to the current date
JLabel date = new JLabel("Today is : " + getdate());

    public CalenderView(){
        //Close the application when X is pressed
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //Default size of application
        this.setName("Calendar");
        this.getAlignmentX();
        this.getAlignmentY();
        this.setSize(850, 550);
        this.setResizable(false);

        //add GUI to JPanel
        CalenderPanel.add(table);
        CalenderPanel.add(date);

        //add the JPanel to the JFrame
        this.add(CalenderPanel);
        //centers the application native to the users res
        setLocationRelativeTo(null);
    }

Upvotes: 0

Views: 1875

Answers (1)

camickr
camickr

Reputation: 324118

Variable names should NOT start with an upper case character. "table" and "date" are correct, but "CalenderPanel" is not. Be consistent!

A JPanel uses a FlowLayout so the two components are displayed beside one another.

Maybe you can use a BorderLayout:

calenderPanel.setLayout( new BorderLayout() );
calenderPanel.add(table, BorderLayout.CENTER);
calenderPanel.add(date, BorderLayout.PAGE_START);

Upvotes: 1

Related Questions