Results 1 to 17 of 17

Thread: JFrame Layout

  1. #1
    Join Date
    Jan 2010
    Posts
    80
    Rep Power
    0

    Default JFrame Layout

    Hi

    I have a problem with my cuurent project. Using GUI, I have an interface that has 3 sections:
    1. Upper tab that has to be card layout.
    2. Board GUI that has to be Grid Layout.
    3. Left tab that also has to be Box Layout.

    All these 3 sections must be in 1 interface.

    I actually have decided to group them using JFrame and I wrote the following code which has a mistake that I couldn't find it.

    Java Code:
    package Try;
    
    import java.awt.*;
    import java.awt.event.*;
    import java.text.DateFormat;
    import java.util.Calendar;
    import java.util.Date;
    import java.util.GregorianCalendar;
    import java.util.Locale;
    
    import javax.swing.*;
    import javax.swing.border.LineBorder;
    import javax.swing.text.AbstractDocument.Content;
    
    public class ThirdInterface extends JFrame{
    	
    	JFrame pane = new JFrame();
    	
    	JPanel UpperPanel = new JPanel();
    	JPanel BorderPanel = new JPanel();
    	JPanel LeftPanel = new JPanel();
    	
    	//Upper tab
    	JLabel space = new JLabel("            "); // add space
    	JLabel space1 = new JLabel("            "); // add space
    	JLabel label1 = new JLabel("Username: ");
        JLabel label11 = new JLabel("name");
        JLabel label2 = new JLabel("Oponent name: ");
        JLabel label22 = new JLabel("name");
        JLabel label = new JLabel();
    	JLabel Timelabel = new JLabel();
    	Locale alocal = new Locale("CANADA");
    	Date today = new Date();
    	DateFormat dateFormatter = DateFormat.getDateInstance(DateFormat.DEFAULT,alocal);
    	String dateOut = dateFormatter.format(today);
    	Calendar cal = new GregorianCalendar();
    	int hour12 = cal.get(Calendar.HOUR);            // 0..11
    	int hour24 = cal.get(Calendar.HOUR_OF_DAY);     // 0..23
    	int min = cal.get(Calendar.MINUTE);             // 0..59
    	int sec = cal.get(Calendar.SECOND);             // 0..59
    	int ms = cal.get(Calendar.MILLISECOND);         // 0..999
    	int ampm = cal.get(Calendar.AM_PM);             // 0=AM, 1=PM
    	String time = ""+ hour12 + ":"+ min + ":"+ sec ;
    	
    	// Left tab
    	JLabel myInfo = new JLabel("My Information:");
    	JTextArea list = new JTextArea(10,25);
    	JButton scores = new JButton(" My Scores ");
    	JButton oponents = new JButton(" My Oponents ");
    	JButton logout = new JButton(" Logout ");
    	
    	// Board
    	private int currentPlayer;
    	JLabel[][] slots = new JLabel[7][6];
    	
    	
    	
    	public void init(){
    		
    		
    		pane.getContentPane();
    		pane.setLayout(new BorderLayout());
    		pane.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		pane.pack();
    	    pane.setVisible(true);
    	     
    		// Upper tab
    		UpperPanel.setLayout(new CardLayout());
    		UpperPanel.add(label1);
    		UpperPanel.add(label11);
    		UpperPanel.add(space);
    		UpperPanel.add(label2);
    		UpperPanel.add(label22);
    		UpperPanel.add(space1);
    		UpperPanel.add(label);
    		UpperPanel.add(Timelabel);
        	
    		// Border
    		BorderPanel.setLayout(new GridLayout(7,7));
    		for (int row=5; row>=0; row--) {
    			for (int column=0; column<7; column++) {
    			slots[column][row] = new JLabel();
    		    slots[column][row].setFont(new Font("SansSerif", Font.BOLD, 18));
    			slots[column][row].setHorizontalAlignment(SwingConstants.CENTER);
    			slots[column][row].setBorder(new LineBorder(Color.BLACK));
    			BorderPanel.add(slots[column][row]);
    			}
    		}
    		currentPlayer = 1;
    		
    		// Left tab
    		LeftPanel.setLayout(new BoxLayout(pane, BoxLayout.X_AXIS));
    		LeftPanel.add(Box.createVerticalStrut(10));
    		myInfo.setAlignmentX(Component.CENTER_ALIGNMENT);
    		LeftPanel.add(myInfo);
    		list.setAlignmentX(Component.CENTER_ALIGNMENT);
    		list.setEditable(false);
    		LeftPanel.add(list);
    		LeftPanel.add(Box.createVerticalStrut(10));
    		scores.setAlignmentX(Component.CENTER_ALIGNMENT);
    		LeftPanel.add(scores);
    		oponents.setAlignmentX(Component.CENTER_ALIGNMENT);
    		LeftPanel.add(oponents);
    		logout.setAlignmentX(Component.CENTER_ALIGNMENT);
    		LeftPanel.add(logout);
    		
    		pane.add(UpperPanel) ;
    		pane.add(BorderPanel);
    		pane.add(LeftPanel);
    
    	}
    	
    	
    	public static void main(String[] args){
    		ThirdInterface ti = new ThirdInterface();
    	    ti.init();
    		
    	}
    }
    I hope you can help me

    Thanks

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,304
    Rep Power
    20

    Default

    Quote Originally Posted by ŖΫ ỏ Ңόρę View Post
    I wrote the following code which has a mistake that I couldn't find it.
    You forgot to tell us how you deduced that the code has a mistake.

    -- didn't compile? --> post the compile time error
    -- didn't run? --> post the runtime error
    -- didn't give the expected result? --> post the desired and actual results
    -- computer went up in a puff of green smoke? --> don protective gear and grab a fire extinguisher

    db

  3. #3
    Join Date
    Jan 2010
    Posts
    80
    Rep Power
    0

    Default

    Sorry, I forgot that.

    Java Code:
    at java.awt.Container.addImpl(Unknown Source)
    	at java.awt.Container.add(Unknown Source)
    	at Try.ThirdInterface.init(ThirdInterface.java:69)
    	at Try.ThirdInterface.main(ThirdInterface.java:118)
    Line 69:
    Java Code:
    UpperPanel.add(label1);
    Line 118:
    Java Code:
    ti.init();
    These are the mistakes

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,304
    Rep Power
    20

    Default

    Quote Originally Posted by ŖΫ ỏ Ңόρę View Post
    Sorry, I forgot that.

    Java Code:
    at java.awt.Container.addImpl(Unknown Source)
    	at java.awt.Container.add(Unknown Source)
    	at Try.ThirdInterface.init(ThirdInterface.java:69)
    	at Try.ThirdInterface.main(ThirdInterface.java:118)
    You left out the most important first line of the error message. Also, you didn't answer the question: when did this error occur, compile time or run time?

    This is getting to be like pulling teeth.

    db

  5. #5
    StormyWaters is offline Senior Member
    Join Date
    Feb 2009
    Posts
    305
    Rep Power
    6

    Default

    Why are you packing the content pane and setting it visible before anything is added to it/at all? Usually you would pack the actual JFrame and setting that visible after you add everything to it.

    I'm not saying this is you're problem, because as Darryl pointed out you left out the important line of the exception, but this may cause a problem down the line.

    You are also extending JFrame and have a JFrame variable as well. Why extend it if you aren't going to treat it as one? I suggest removing the JFrame variable and adding the components directly to your class object.

  6. #6
    Join Date
    Jan 2010
    Posts
    80
    Rep Power
    0

    Default

    First, I am going to post my code after I added the component before setting the content pane.

    Java Code:
    package Try;
    
    import java.awt.*;
    import java.awt.event.*;
    import java.text.DateFormat;
    import java.util.Calendar;
    import java.util.Date;
    import java.util.GregorianCalendar;
    import java.util.Locale;
    
    import javax.swing.*;
    import javax.swing.border.LineBorder;
    import javax.swing.text.AbstractDocument.Content;
    
    public class ThirdInterface extends JFrame{
    	
    	JFrame pane = new JFrame();
    	
    	JPanel UpperPanel = new JPanel();
    	JPanel BorderPanel = new JPanel();
    	JPanel LeftPanel = new JPanel();
    	
    	//Upper tab
    	JLabel space = new JLabel("            "); // add space
    	JLabel space1 = new JLabel("            "); // add space
    	JLabel label1 = new JLabel("Username: ");
        JLabel label11 = new JLabel("name");
        JLabel label2 = new JLabel("Oponent name: ");
        JLabel label22 = new JLabel("name");
        JLabel label = new JLabel();
    	JLabel Timelabel = new JLabel();
    	Locale alocal = new Locale("CANADA");
    	Date today = new Date();
    	DateFormat dateFormatter = DateFormat.getDateInstance(DateFormat.DEFAULT,alocal);
    	String dateOut = dateFormatter.format(today);
    	Calendar cal = new GregorianCalendar();
    	int hour12 = cal.get(Calendar.HOUR);            // 0..11
    	int hour24 = cal.get(Calendar.HOUR_OF_DAY);     // 0..23
    	int min = cal.get(Calendar.MINUTE);             // 0..59
    	int sec = cal.get(Calendar.SECOND);             // 0..59
    	int ms = cal.get(Calendar.MILLISECOND);         // 0..999
    	int ampm = cal.get(Calendar.AM_PM);             // 0=AM, 1=PM
    	String time = ""+ hour12 + ":"+ min + ":"+ sec ;
    	
    	// Left tab
    	JLabel myInfo = new JLabel("My Information:");
    	JTextArea list = new JTextArea(10,25);
    	JButton scores = new JButton(" My Scores ");
    	JButton oponents = new JButton(" My Oponents ");
    	JButton logout = new JButton(" Logout ");
    	
    	// Board
    	private int currentPlayer;
    	JLabel[][] slots = new JLabel[7][6];
    	
    	
    	
    	public void init(){
    			     
    		// Upper tab
    		UpperPanel.setLayout(new CardLayout());
    		UpperPanel.add(label1);
    		UpperPanel.add(label11);
    		UpperPanel.add(space);
    		UpperPanel.add(label2);
    		UpperPanel.add(label22);
    		UpperPanel.add(space1);
    		UpperPanel.add(label);
    		UpperPanel.add(Timelabel);
        	
    		// Border
    		BorderPanel.setLayout(new GridLayout(7,7));
    		for (int row=5; row>=0; row--) {
    			for (int column=0; column<7; column++) {
    			slots[column][row] = new JLabel();
    		    slots[column][row].setFont(new Font("SansSerif", Font.BOLD, 18));
    			slots[column][row].setHorizontalAlignment(SwingConstants.CENTER);
    			slots[column][row].setBorder(new LineBorder(Color.BLACK));
    			BorderPanel.add(slots[column][row]);
    			}
    		}
    		currentPlayer = 1;
    		
    		// Left tab
    		LeftPanel.setLayout(new BoxLayout(pane, BoxLayout.X_AXIS));
    		LeftPanel.add(Box.createVerticalStrut(10));
    		myInfo.setAlignmentX(Component.CENTER_ALIGNMENT);
    		LeftPanel.add(myInfo);
    		list.setAlignmentX(Component.CENTER_ALIGNMENT);
    		list.setEditable(false);
    		LeftPanel.add(list);
    		LeftPanel.add(Box.createVerticalStrut(10));
    		scores.setAlignmentX(Component.CENTER_ALIGNMENT);
    		LeftPanel.add(scores);
    		oponents.setAlignmentX(Component.CENTER_ALIGNMENT);
    		LeftPanel.add(oponents);
    		logout.setAlignmentX(Component.CENTER_ALIGNMENT);
    		LeftPanel.add(logout);
    		
    	    pane.add(UpperPanel, "south") ;
    		pane.add(BorderPanel);
    		pane.add(LeftPanel);
    		pane.getContentPane();
    		pane.setLayout(new BorderLayout());
    		pane.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		pane.pack();
    	    pane.setVisible(true);
    	   
    
    	}
    	
    
    	
    	public static void main(String[] args){
    		ThirdInterface ti = new ThirdInterface();
    		ti.init();
            	
    	}
    }
    Second, when I run this code with Eclipse program, it gives me:

    Java Code:
    Exception in thread "main" java.lang.IllegalArgumentException: cannot add to layout: constraint must be a string
    	at java.awt.CardLayout.addLayoutComponent(Unknown Source)
    	at java.awt.Container.addImpl(Unknown Source)
    	at java.awt.Container.add(Unknown Source)
    	at Try.ThirdInterface.init(ThirdInterface.java:62)
    	at Try.ThirdInterface.main(ThirdInterface.java:116)
    Third, The idea from this code is to design this interface.



    I want to devide this interface to 3 layouts:
    1) The upper section using CardLayout.
    2) The left section using BoxLayout.
    3) The right section which is the board using GridLayout.

    I want to arrange all these layouts in one panel.

    I hope you can help me.

  7. #7
    Join Date
    Jan 2010
    Posts
    80
    Rep Power
    0

    Default

    It's complicated and I'm really confused

    Waiting u

  8. #8
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,518
    Rep Power
    25

    Default

    at Try.ThirdInterface.init(ThirdInterface.java:62)
    What is the source record at line 62 in your program?
    It appears you are trying to call the add() method with the wrong args:
    IllegalArgumentException: cannot add to layout: constraint must be a string
    Find the statement, then get the type of the Container on that line and read the API doc for its add() method. Check to see if you used a String where it is required.

  9. #9
    Join Date
    Jan 2010
    Posts
    80
    Rep Power
    0

    Default

    First, I discovered the problem with setting Layouts in init() class.

    I found that I have to setLayout after I add components.

    e.g. in this block, I move the line of setLayout after add components

    Java Code:
    UpperPanel.add(label1);
    		UpperPanel.add(label11);
    		UpperPanel.add(space);
    		UpperPanel.add(label2);
    		UpperPanel.add(label22);
    		UpperPanel.add(space1);
    		UpperPanel.add(label);
    		UpperPanel.add(Timelabel);
    		UpperPanel.setLayout(new CardLayout());
    Now, the first part of the problem has solved.

    After I run the program, it gives:

    Java Code:
    at Try.ThirdInterface.init(ThirdInterface.java:106)
    This line refers to

    Java Code:
    pane.pack();
    And also gives me

    Java Code:
    at Try.ThirdInterface.main(ThirdInterface.java:117)
    which refers to

    Java Code:
    ti.init();
    I'm trying to solve the problem.

  10. #10
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,518
    Rep Power
    25

    Default

    Your post left off the error message itself. You've only shown the line number, but have not said what was wrong with the code.

  11. #11
    Join Date
    Jan 2010
    Posts
    80
    Rep Power
    0

    Default

    I did some changes in the code and finally no error messages but the problem now with the components. They don't show up in the frame.

    Java Code:
    package Try;
    
    import java.awt.*;
    import java.awt.event.*;
    import java.text.DateFormat;
    import java.util.Calendar;
    import java.util.Date;
    import java.util.GregorianCalendar;
    import java.util.Locale;
    
    import javax.swing.*;
    import javax.swing.border.LineBorder;
    import javax.swing.text.AbstractDocument.Content;
    
    public class ThirdInterface extends JFrame{
    	
    	//JFrame pane = new JFrame();
    	
    	private JPanel UpperPanel = new JPanel();
    	private JPanel BorderPanel = new JPanel();
    	private JPanel LeftPanel = new JPanel();
    	
    	//Upper tab
    	JLabel space = new JLabel("            "); // add space
    	JLabel space1 = new JLabel("            "); // add space
    	JLabel label1 = new JLabel("Username: ");
        JLabel label11 = new JLabel("name");
        JLabel label2 = new JLabel("Oponent name: ");
        JLabel label22 = new JLabel("name");
        JLabel label = new JLabel();
    	JLabel Timelabel = new JLabel();
    	Locale alocal = new Locale("CANADA");
    	Date today = new Date();
    	DateFormat dateFormatter = DateFormat.getDateInstance(DateFormat.DEFAULT,alocal);
    	String dateOut = dateFormatter.format(today);
    	Calendar cal = new GregorianCalendar();
    	int hour12 = cal.get(Calendar.HOUR);            // 0..11
    	int hour24 = cal.get(Calendar.HOUR_OF_DAY);     // 0..23
    	int min = cal.get(Calendar.MINUTE);             // 0..59
    	int sec = cal.get(Calendar.SECOND);             // 0..59
    	int ms = cal.get(Calendar.MILLISECOND);         // 0..999
    	int ampm = cal.get(Calendar.AM_PM);             // 0=AM, 1=PM
    	String time = ""+ hour12 + ":"+ min + ":"+ sec ;
    	
    	// Left tab
    	JLabel myInfo = new JLabel("My Information:");
    	JTextArea list = new JTextArea(10,25);
    	JButton scores = new JButton(" My Scores ");
    	JButton oponents = new JButton(" My Oponents ");
    	JButton logout = new JButton(" Logout ");
    	
    	// Board
    	private int currentPlayer;
    	JLabel[][] slots = new JLabel[7][6];
    	
    	
    	
    	public ThirdInterface(){
    			     
    		// Upper tab
    		
    		UpperPanel.add(label1);
    		UpperPanel.add(label11);
    		UpperPanel.add(space);
    		UpperPanel.add(label2);
    		UpperPanel.add(label22);
    		UpperPanel.add(space1);
    		UpperPanel.add(label);
    		UpperPanel.add(Timelabel);
    		UpperPanel.setLayout(new CardLayout());
    		// Border
    		BorderPanel.setLayout(new GridLayout(7,7));
    		for (int row=5; row>=0; row--) {
    			for (int column=0; column<7; column++) {
    			slots[column][row] = new JLabel();
    		    slots[column][row].setFont(new Font("SansSerif", Font.BOLD, 18));
    			slots[column][row].setHorizontalAlignment(SwingConstants.CENTER);
    			slots[column][row].setBorder(new LineBorder(Color.BLACK));
    			BorderPanel.add(slots[column][row]);
    			}
    		}
    		currentPlayer = 1;
    		
    		// Left tab
    		
    		LeftPanel.add(Box.createVerticalStrut(10));
    		myInfo.setAlignmentX(Component.CENTER_ALIGNMENT);
    		LeftPanel.add(myInfo);
    		list.setAlignmentX(Component.CENTER_ALIGNMENT);
    		list.setEditable(false);
    		LeftPanel.add(list);
    		LeftPanel.add(Box.createVerticalStrut(10));
    		scores.setAlignmentX(Component.CENTER_ALIGNMENT);
    		LeftPanel.add(scores);
    		oponents.setAlignmentX(Component.CENTER_ALIGNMENT);
    		LeftPanel.add(oponents);
    		logout.setAlignmentX(Component.CENTER_ALIGNMENT);
    		LeftPanel.add(logout);
    		LeftPanel.setLayout(new BoxLayout(getContentPane(), BoxLayout.X_AXIS));
    		
    		getContentPane().add(UpperPanel, BorderLayout.NORTH);
    		getContentPane().add(BorderPanel , BorderLayout.WEST);
    		getContentPane().add(LeftPanel , BorderLayout.EAST);
    		
    		//ti.add(UpperPanel, BorderLayout.CENTER) ;
    		//ti.add(BorderPanel);
    		//ti.add(LeftPanel);
    		//ti.getContentPane();
    		//ti.setLayout(new BorderLayout());
    		//ti.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		
    	   
    
    	}
    	
    
    	
    	public static void main(String[] args){
    		//ThirdInterface tt = new ThirdInterface();
    		
    		JFrame ti = new JFrame();
    		
    		ti.setSize(600, 800);
    	    ti.setVisible(true);
    	}
    }

  12. #12
    StormyWaters is offline Senior Member
    Join Date
    Feb 2009
    Posts
    305
    Rep Power
    6

    Default

    In your main method, you are just creating a empty JFrame.

    Create an instance of your ThirdInterface class and set it visible.

  13. #13
    Join Date
    Jan 2010
    Posts
    80
    Rep Power
    0

    Default

    I did what you say but it gives me an error

    Java Code:
    public static void main(String[] args){
    		ThirdInterface tt = new ThirdInterface();
    		JFrame ti = new JFrame();
    		ti.setSize(600, 800);
    	    ti.setVisible(true);
    	    tt.setVisible(true);
    	}
    The error is

    Java Code:
    tt.setVisible(true);

  14. #14
    curmudgeon is offline Senior Member
    Join Date
    May 2010
    Posts
    436
    Rep Power
    5

    Default

    You just might want to tell folks what the error message is. Just a thought.

  15. #15
    Join Date
    Jan 2010
    Posts
    80
    Rep Power
    0

    Default

    Java Code:
    Exception in thread "main" java.awt.AWTError: BoxLayout can't be shared
    	at javax.swing.BoxLayout.checkContainer(Unknown Source)
    	at javax.swing.BoxLayout.invalidateLayout(Unknown Source)
    	at java.awt.Container.invalidate(Unknown Source)
    	at java.awt.Component.addNotify(Unknown Source)
    	at java.awt.Container.addNotify(Unknown Source)
    	at javax.swing.JComponent.addNotify(Unknown Source)
    	at java.awt.Container.addNotify(Unknown Source)
    	at javax.swing.JComponent.addNotify(Unknown Source)
    	at java.awt.Container.addNotify(Unknown Source)
    	at javax.swing.JComponent.addNotify(Unknown Source)
    	at java.awt.Container.addNotify(Unknown Source)
    	at javax.swing.JComponent.addNotify(Unknown Source)
    	at javax.swing.JRootPane.addNotify(Unknown Source)
    	at java.awt.Container.addNotify(Unknown Source)
    	at java.awt.Window.addNotify(Unknown Source)
    	at java.awt.Frame.addNotify(Unknown Source)
    	at java.awt.Window.show(Unknown Source)
    	at java.awt.Component.show(Unknown Source)
    	at java.awt.Component.setVisible(Unknown Source)
    	at java.awt.Window.setVisible(Unknown Source)
    	at Try.ThirdInterface.main(ThirdInterface.java:123)

  16. #16
    curmudgeon is offline Senior Member
    Join Date
    May 2010
    Posts
    436
    Rep Power
    5

    Default

    Quote Originally Posted by ŖΫ ỏƒ Ңόρę View Post
    Java Code:
    Exception in thread "main" java.awt.AWTError: BoxLayout can't be shared
    	at .....
    	at Try.ThirdInterface.main(ThirdInterface.java:123)
    The error message is telling you what's wrong: you're using BoxLayout wrong on line 123 of the ThirdInterface class. You're setting BoxLayout on the LeftPanel, but for some unknown reason are passing the contentPane to the BoxLayout constructor. Answer: don't do this, but instead pass the panel that you're currently setting to use BoxLayout. Please read the BoxLayout tutorial and API for more details if you're not sure how to use it.

    More importantly, don't assume that error messages are cryptic and mystical but rather understand that usually they contain important information that if used will likely help you fix your code yourself.

  17. #17
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,518
    Rep Power
    25

    Default

    BoxLayout can't be shared
    Are there any BoxLayout objects in your code?
    Is one used in more than one place?
    If so, create a new one for each place you want to have a boxlayout

Similar Threads

  1. Replies: 6
    Last Post: 04-12-2010, 11:33 AM
  2. Edit layout Layout please help me
    By manhtungtnk28@gmail.com in forum New To Java
    Replies: 4
    Last Post: 11-23-2009, 08:41 AM
  3. Passing data from one JFrame to another JFrame
    By tarami in forum New To Java
    Replies: 3
    Last Post: 08-06-2009, 05:44 PM
  4. Replies: 4
    Last Post: 04-22-2009, 11:02 PM
  5. [SOLVED] JFrame Layout
    By AndrewM16921 in forum New To Java
    Replies: 4
    Last Post: 04-09-2009, 01:11 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •