Results 1 to 13 of 13
Like Tree2Likes
  • 1 Post By JosAH
  • 1 Post By PhHein

Thread: Problems with using three panels

  1. #1
    Join Date
    Dec 2013
    Posts
    8
    Rep Power
    0

    Default Problems with using three panels

    I am assisting my son with his GUI assignment so I don't want a specific answer, rather guidance to correct his code.
    Limited to writing code with notepad++ - not netbeans etc.
    Limited to border layout, flowlayout, and grid layout
    Issue 1. There are three panels (using border layout) - Welcome Panel (top - 40px high), Data Entry Panel (middle - 40px high) and Data Display Panel (bottom - should be remainder of jFrame, however, border layout wants to use the middle panel as the dynamic panel.

    A Welcome message is added to the top panel - no problems;
    Issue 2: Once the TextField is added to the middle panel (line 139/140 of code below) the panels no longer display.

    Any guidance would be much appreciated.

    Thanks
    Peter

    Java Code:
    //  Name: CommissionCalculator2.java
    //  Purpose: 
    //  Input: nil
    //  Output: nil
    //  Author: Allister Peterson
    //  Date: 
    //  Version: 1.0
    //  Limitations: 
    //  Known Bugs: Upon running the program the user must maximise and restore the window size for the panels to show.  
    
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
    import java.awt.BorderLayout;
    
    public class CommissionCalculator2
    {
    	private JFrame myFrame;
    	private JPanel welcomePanel;
    	private JPanel entryPanel;
    	private JPanel dataPanel;
    	private JButton enterButton;
    	private JButton sortButton;
    	private JButton statisticsButton;
    	private JLabel welcomeLabel;
    	private JLabel nameLabel;
    	private JLabel salesLabel;
    	private JTextField nameTextField;
    	private JTextField salesTextField;
    	private int[] StaffSales;
    	private String[] StaffName;
    	private double StaffCommission;
    	private int frameContentHeight;
    	private JTextArea textArea;
    	private String staffSalesName;
    	private String sStaffSalesAmount;
    	private int staffSalesAmount;
    	
    	
    	public CommissionCalculator2()
    	{
    		gui();
    	}
    	
    	public void gui()
    	{
    		frameContentHeight = 0;
    		MyFrameConstructor myFrame = new MyFrameConstructor();
    		
    		//System.out.println("content pane height: "+getContentPane().getHeight());
    		
    		frameContentHeight = myFrame.getContentPane().getHeight();
    		//JOptionPane.showMessageDialog(null, "IN GUI myFrame.. "+myFrame.getContentPane().getHeight());  //  DELETE THIS LATER
    		//JOptionPane.showMessageDialog(null, "IN GUI frameContentHeight "+frameContentHeight);  //  DELETE THIS LATER
    	}
    
    	public class MyFrameConstructor extends JFrame
    	{
    		public MyFrameConstructor()
    		{
    			Dimension screenDimension = Toolkit.getDefaultToolkit().getScreenSize();  // Declare Screen Dimension variable
    			double screenHeight;
    			double screenWidth;
    			int frameWidth;
    			int frameHeight;
    			int frameX;  //  X - top left hand corner of window
    			int frameY;  //  Y - top left hand corner of window
    			
    			setTitle( "Commission Calculator V2" );
    			screenWidth = screenDimension.getWidth();  //  gets width of screen (monitor)
    			screenHeight = screenDimension.getHeight();  //  gets height of screen (monitor)
    			//windowWidth = (int)Math.round(2/3*screenWidth);  //  this line results with a value of zero (0), however, following line works;
    			frameWidth = (int)Math.round(2*screenWidth/3);  //  calculates window width to 2/3 of screen width
    			frameHeight = (int)Math.round(2*screenHeight/3);  //  calculates window height to 2/3 of screen height
    			// JOptionPane.showMessageDialog(null, windowHeight);
    		
    			setSize( frameWidth, frameHeight );  //  sets window size (width * height)
    			
    			frameX = (int)Math.round(screenWidth/6);  //  calculates 
    			frameY = (int)Math.round(screenHeight/6);
    			setLocation( frameX, frameY );
    			//setResizable(false);
    			setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
    			setVisible( true );
    			
    			GUIPanelConstructor GUIPanel = new GUIPanelConstructor();
    			add(GUIPanel);
    		}
    	}
    	
    	public class GUIPanelConstructor extends JPanel
    	{
    		public GUIPanelConstructor()
    		{
    			setLayout( new BorderLayout() );
    			setBackground(Color.YELLOW);
    			WelcomePanelConstructor welcomePanel = new WelcomePanelConstructor();
    			EntryPanelConstructor entryPanel = new EntryPanelConstructor();
    			DisplayPanelConstructor displayPanel = new DisplayPanelConstructor();
    			
    			add(welcomePanel,BorderLayout.PAGE_START);
    			add(entryPanel,BorderLayout.CENTER);
    			add(displayPanel,BorderLayout.PAGE_END);
    		}
    	}
    	
    //================================================================
    	public class WelcomePanelConstructor extends JPanel
    	{
    		public WelcomePanelConstructor()
    		{
    			Dimension welcomePanelSize = getPreferredSize();
    			welcomePanelSize.height = 40;
    			setPreferredSize(welcomePanelSize);
    			setBackground(new java.awt.Color(0, 0, 255));  // DELETE THIS LINE ONCE UNDERSTOOD - BLUE
    			setLayout( new FlowLayout());
    			JLabel welcomeLabel = new JLabel( "Please input salesperson's name and sales amount" );
    			add(welcomeLabel);
    		}
    	}
    //==================================================================
    	public class EntryPanelConstructor extends JPanel
    	{
    		public EntryPanelConstructor()
    		{
    			Dimension entryPanelSize = getPreferredSize();
    			// entryPanelSize.height = 40;
    			setPreferredSize(entryPanelSize);
    			setLayout( new FlowLayout());
    			setBackground(new java.awt.Color(0, 255, 0));   // DELETE THIS LINE ONCE UNDERSTOOD - GREEN
    		
    		
    		nameLabel = new JLabel("Name:");
    		add( nameLabel );
    
    //ISSUE 2 occurs after here!
    		nameTextField = new JTextField(30);  //sales person's name is limited to 30
    		add( nameTextField );
    /*		
    		String staffSalesName = nameTextField.getText();
    		
    		salesLabel = new JLabel( "Sales ('000):" );  
    		
    		salesTextField = new JTextField( 5 );   // value must be >=10 and <= 100
    //		String sStaffSalesAmount = salesTextField.getText();
    //		int staffSalesAmount = Integer.parseInt( sStaffSalesAmount );
    		
    		enterButton = new JButton( "Enter" );
    		sortButton = new JButton( "Sort" );  
    		statisticsButton = new JButton( "Statistics" );
    		
    
    		add( salesLabel );
    		add( salesTextField );
    		add( enterButton );
    		add( sortButton );
    		add( statisticsButton ); 
    */		
    	
    		}
    	}
    //=======================================================
    	public class DisplayPanelConstructor extends JPanel
    	{
    		public DisplayPanelConstructor()
    		{
    			//	MyFrameConstructor myFrame = new MyFrameConstructor();
    			//	int frameHeight = myFrame.frameHeight;
    			Dimension displayPanelSize = getPreferredSize();
    			//==============displayPanelSize.height = (frameHeight - 80);
    			// displayPanelSize.height = ((myFrame.getContentPane().getHeight()) - 80);
    			
    			displayPanelSize.height = (404);
    			setPreferredSize(displayPanelSize);
    			setLayout( new GridLayout(0,1));
    			setBackground(new java.awt.Color(255, 0, 0));   // DELETE THIS LINE ONCE UNDERSTOOD - RED
    			
    //			textArea = new JTextArea( "Please input salesperson's name and sales amount", 10, 15 );
    //			add( textArea );
    		}
    	}
    //===========================================
    /*
    	public static int CalculateCommission()
    	{
    		double StaffCommission = 0;
    		
    		if (staffSalesAmount > 75000) StaffCommission = staffSalesAmount * 0.1;
    		else if (staffSalesAmount > 50000) StaffCommission = staffSalesAmount * 0.08;
    		else if (staffSalesAmount > 25000) StaffCommission = staffSalesAmount * 0.06;
    		else StaffCommission = staffSalesAmount * 0.05;
    		
    		return StaffCommission;
    	}
    //===========================================
    	private class enterButtonHandler implements ActionListener
    	{
    		public void actionPerformed( ActionEvent event )
    		{
    			
    		}
    	}
    //================================================	
    	private class sortButtonHandler implements ActionListener
    	{
    		public void actionPerformed( ActionEvent event )
    		{
    			BubbleSort();
    		}
    	}
    	
    	public void BubbleSort()
    	{
    		for ( int pass = 1; pass < data.length; pass++ )
    		{
    			// loop over elements in array
    			for ( int index = 0; index < data.length - pass; index++ )
    			{
    				// swap adjacent elements if first is greater than second
    				if ( data[ index ] > data[ index + 1 ] )
    				BubbleSwap( index, index + 1 ); // swap adjacent elements
    			}
    		}
    	}
    	
    	public void BubbleSwap(int First, int Second)
    	{
    		String temporary = StaffName[ first ]; // store first in temporary
    		StaffName[ first ] = StaffName[ second ]; // replace first with second
    		StaffName[ second ] = temporary; // put temporary in second
    		
    		int temporary = StaffSales[ first ]; // store first in temporary
    		StaffSales[ first ] = StaffSales[ second ]; // replace first with second
    		StaffSales[ second ] = temporary; // put temporary in second
    	}
    	
    	private class statisticsButtonHandler implements ActionListener
    	{
    		public void actionPerformed ( ActionEvent event )
    		{
    			
    		}
    	}
    	
    	private static int getMaximum()
    	{
    		int n = 0;
    		int maxSales = staffSalesAmount[0];
    		
    		for(n = 0; n < MAX_STAFF; n++)
    			if (maxSales < staffSalesAmount[n])
    				maxSales = staffSalesAmount[n];
    		return maxSales;
    	}
    	
    	private static int getMinimum()
    	{
    		int n = 0;
    		int minSales = staffSalesAmount[0];
    		
    		for(n = 0; n < MAX_STAFF; n++)
    			if (minSales > staffSalesAmount[n])
    				minSales = staffSalesAmount[n];
    				
    		return minSales;
    	}
    	
    	private static int getAverage()
    	{
    		
    	}
    ==========================
    */
    	public static void main( String[] args )
    	{
    		new CommissionCalculator2();
    	}
    }

  2. #2
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,996
    Rep Power
    5

    Default Re: Problems with using three panels

    Issue1 is not really an issue is it - yes BorderLayout works that way, so use it as it is designed. You are free to nest components, so you can easily put a JPanel with a BorderLayout at the top and add the welcome and data entry panels to that panel; then you can treat the center position of your outer BorderLayout as the flexible "bottom" of your application. If you get lost in that description I suggest to draw it on a piece of paper first, GUIs are very hard to explain in words :(

    Issue 2 would require some investigation on my part, I don't really have the time to dive into that right now. Luckily I am by far not the most helpful chap on this forum, with a bit of patience the hints will come from others :)
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  3. #3
    Join Date
    Dec 2013
    Posts
    8
    Rep Power
    0

    Default Re: Problems with using three panels

    gimbal2,
    Thanks for replying. I shall get my son to recode to fix Issue 1 and look forward to any assistance with Issue 2.
    Thanks
    Peter

  4. #4
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,519
    Blog Entries
    7
    Rep Power
    20

    Default Re: Problems with using three panels

    Those Swing components can be quite peculiar: before they are displayed, they don't have a preferred size, i.e. (0, 0); the moment you set a preferred size, it assumes it is its preferred size (what else is it to do?). Your code is juggling with that preferred size and accidentally sets it to (0, 0); this is obviously not what you want; leave the preferred size alone and left those LayoutManagers deal with it.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    Join Date
    Dec 2013
    Posts
    8
    Rep Power
    0

    Default Re: Problems with using three panels

    Jos,
    Thank you for your reply. I am not sure I follow you. The required panels are shown below with the top two panels to be 40px high and the bottom (yellow) panel to be dynamic in size.
    Problems with using three panels-panels.jpg
    I appreciate the comments about preferred size, but don't know how to set the required sizes for the top and middle panel.
    Regards
    Peter

  6. #6
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default Re: Problems with using three panels

    What he is saying is don't do this

    Java Code:
    Dimension entryPanelSize = getPreferredSize();
    setPreferredSize(entryPanelSize);

  7. #7
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,519
    Blog Entries
    7
    Rep Power
    20

    Default Re: Problems with using three panels

    Use a GridBagLayout; this LayoutManager can be a bit intimidating at first: you add your components to it together with a GridBagConstraints object; it's the latter that specifies the size(s) of your components and whether or not they should fill up any remaining space in either direction. Read the API documentation for the relevant classes.

    kind regards,

    Jos
    masijade likes this.
    cenosillicaphobia: the fear for an empty beer glass

  8. #8
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,996
    Rep Power
    5

    Default Re: Problems with using three panels

    That's all fun and games, but the OP clearly states the limitation "Limited to border layout, flowlayout, and grid layout". That excludes GridBagLayout as a solution.

    (edit, well "clearly" - its not like there are neon arrows pointing to it)
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  9. #9
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,519
    Blog Entries
    7
    Rep Power
    20

    Default Re: Problems with using three panels

    Quote Originally Posted by gimbal2 View Post
    That's all fun and games, but the OP clearly states the limitation "Limited to border layout, flowlayout, and grid layout". That excludes GridBagLayout as a solution.

    (edit, well "clearly" - its not like there are neon arrows pointing to it)
    Dang, I always only see things with neon arrows pointing to it; here's an alternative: put the top two panels in another panel (panel #4) with a GridLayout. Stick that panel in a fifth panel with a BorderLayout in the NORTH part; the remaining panel (panel #3) ends up in the CENTER part of that fifth panel.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  10. #10
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default Re: Problems with using three panels

    Quote Originally Posted by Peter Peterson View Post
    Jos,
    Thank you for your reply. I am not sure I follow you. The required panels are shown below with the top two panels to be 40px high and the bottom (yellow) panel to be dynamic in size.
    Click image for larger version. 

Name:	Panels.jpg 
Views:	1 
Size:	49.6 KB 
ID:	5866
    I appreciate the comments about preferred size, but don't know how to set the required sizes for the top and middle panel.
    Regards
    Peter
    Use BorderLayout for the whole thing (root). Green and blue are on one JPanel (jp1) with FlowLayout. Yellow is another Jpanel(jp2). Now you add jp1 as NORTH and jp2 as CENTER to root.

    (posted just to give you an alternative to Jos' 5 panels ;) )
    gimbal2 likes this.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  11. #11
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,996
    Rep Power
    5

    Default Re: Problems with using three panels

    Yeah that's what I said in post #2, but you say it more clearly :)
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  12. #12
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default Re: Problems with using three panels

    Quote Originally Posted by gimbal2 View Post
    Yeah that's what I said in post #2, but you say it more clearly :)
    Oh, I obviously didn't understand you then ;)
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  13. #13
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,996
    Rep Power
    5

    Default Re: Problems with using three panels

    Meh, I probably should have spent 2 minutes more rewording it.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

Similar Threads

  1. panels, panels, and more panels
    By yankeetooter7 in forum AWT / Swing
    Replies: 8
    Last Post: 06-11-2012, 06:09 AM
  2. panels, panels, and more panels
    By yankeetooter7 in forum New To Java
    Replies: 2
    Last Post: 06-11-2012, 04:49 AM
  3. Gap Between Panels?
    By OllyHal in forum AWT / Swing
    Replies: 1
    Last Post: 03-13-2012, 08:36 PM
  4. Need some help with panels inside panels
    By kakefjes in forum AWT / Swing
    Replies: 0
    Last Post: 03-17-2011, 11:36 AM
  5. resizeable panels
    By simo_mon in forum AWT / Swing
    Replies: 4
    Last Post: 07-29-2009, 01:45 AM

Tags for this Thread

Posting Permissions

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