Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 14 of 14

Thread: Adding Panels to JFrame

  1. #1
    Member
    Join Date
    Apr 2014
    Posts
    219
    Thanks
    8
    Thanked 2 Times in 2 Posts

    Default Adding Panels to JFrame

    I added a few JPanels to a JFrame. Want I wanted is the JTextFields to be on the top right, the buttons to be on the bottom and the JFileChooser to be on the top right. Lastly I wanted the JTextArea to be in the center. I will attach a picture of what the result was below. Here is the code:

    package addItemBtn.Home.DataBase;
     
    import java.awt.BorderLayout;
    import java.awt.FlowLayout;
    import java.awt.GridLayout;
     
    import javax.swing.*;
     
    public class AddItemView extends JFrame
    {
     
    	private static final long serialVersionUID = 1L;
    	JFrame frame;
    	JPanel btnPanel,descriptionPanel,picturePanel,textPanel;
    	JTextField nameBox,priceBox,locationBox;
    	JLabel nameLbl,descriptionLbl,priceLbl,locationLbl,pictureLbl;
    	JTextArea descriptionBox;
    	JButton submitBtn,cancelBtn,previewBtn,browseBtn;
     
    	JFileChooser fileopen;
     
    	public AddItemView()
    	{
    		//set up frame
    		frame = new JFrame("Add Item");
    		frame.setSize(750,500);
    		frame.setLayout(new BorderLayout());
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
    		//create text boxes and labels
    		nameLbl = new JLabel("Name:");
    		nameBox = new JTextField(15);
     
    		priceLbl = new JLabel("Price:");
    		priceBox = new JTextField(15);
     
    		locationLbl = new JLabel("Location:");
    		locationBox = new JTextField(15);
     
    		//add text fields to textPanel
    		textPanel = new JPanel();
    		textPanel.setLayout(new GridLayout(6,4));
    		textPanel.setSize(100,75);
     
    		textPanel.add(nameLbl);
    		textPanel.add(nameBox);
    		textPanel.add(priceLbl);
    		textPanel.add(nameBox);
    		textPanel.add(locationLbl);
    		textPanel.add(locationBox);
    		textPanel.setVisible(true);
     
    		//create file chooser upload a picture
    		fileopen = new JFileChooser();
    		browseBtn = new JButton("Browse");
     
    		//create and add file chooser to panel
    		picturePanel = new JPanel();
    		picturePanel.add(fileopen);
    		picturePanel.add(browseBtn);
     
    		//create buttons
    		btnPanel = new JPanel();
    		btnPanel.setLayout(new FlowLayout());
    		btnPanel.setSize(getPreferredSize());
    		submitBtn = new JButton("Submit");
    		cancelBtn = new JButton("Cancel");
    		previewBtn = new JButton("Preview");
     
    		//add buttons to panel
    		btnPanel.add(submitBtn);
    		btnPanel.add(cancelBtn);
    		btnPanel.add(previewBtn);
    		btnPanel.setVisible(true);
     
    		//create description text area
    		descriptionLbl = new JLabel("Description:");
    		descriptionBox = new JTextArea();
     
    		//add to description text area to panel
    		descriptionPanel = new JPanel();
    		descriptionPanel.setLayout(new FlowLayout());
    		descriptionPanel.setSize(150,100);
    		descriptionPanel.add(descriptionLbl);
    		descriptionPanel.add(descriptionBox);
     
    		//place textFields to top left of frame
    		frame.add(textPanel,BorderLayout.NORTH);
    		//place picture panel into top right of frame
    		frame.add(picturePanel,BorderLayout.NORTH);
    		//place descriptionPanel to the center of the frame
    		frame.add(descriptionPanel,BorderLayout.CENTER);
    		//place buttons on bottom of frame
    		frame.add(btnPanel,BorderLayout.SOUTH);
     
    		frame.setVisible(true);
    	}
     
    }
    Database.jpg

    The JFileChooser is huge, for some reason nameBox (its a JTextField) is not even displaying. Any advice?


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Adding Panels to JFrame

    The class you've created extends JFrame and the constructor creates another JFrame, frame, which is actually the preferred approach. I believe you can remove 'extends JFrame' and have the same result as you do now.

    As for your layout concerns. All containers have a default layout which can be changed to a desired layout if the default does not meet your needs. Refer to the Oracle documentation to learn about the various layout managers and how to use them, and read the API for each of the containers you use to learn which is their default layout manager, something you should have memorized for the containers you use often.

  3. #3
    Member
    Join Date
    Apr 2014
    Posts
    219
    Thanks
    8
    Thanked 2 Times in 2 Posts

    Default Re: Adding Panels to JFrame

    I was reading an found I could use setBounds. This is doing some WACKY things, is there that are doing something similar to what I am doing. Here is my updated code, but I will tell you what I changed, I stopped used BorderLayout and set the frame layout to null, then used setBounds some of the panels to see what it would do.

    package addItemBtn.Home.DataBase;
     
    import java.awt.BorderLayout;
    import java.awt.FlowLayout;
    import java.awt.GridLayout;
     
    import javax.swing.*;
     
    public class AddItemView
    {
     
    	JFrame frame;
    	JPanel btnPanel,descriptionPanel,picturePanel,textPanel;
    	JTextField nameBox,priceBox,locationBox;
    	JLabel nameLbl,descriptionLbl,priceLbl,locationLbl,pictureLbl;
    	JTextArea descriptionBox;
    	JButton submitBtn,cancelBtn,previewBtn,browseBtn;
     
    	JFileChooser fileopen;
     
    	public AddItemView()
    	{
    		//set up frame
    		frame = new JFrame("Add Item");
    		frame.setSize(750,500);
    		frame.setLayout(null);
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
    		//create text boxes and labels
    		nameLbl = new JLabel("Name:");
    		nameBox = new JTextField(15);
     
    		priceLbl = new JLabel("Price:");
    		priceBox = new JTextField(15);
     
    		locationLbl = new JLabel("Location:");
    		locationBox = new JTextField(15);
     
    		//add text fields to textPanel
    		textPanel = new JPanel();
    		textPanel.setLayout(new GridLayout(6,4));
    		textPanel.setSize(100,75);
    		textPanel.setVisible(true);
     
    		textPanel.add(nameLbl);
    		textPanel.add(nameBox);
    		textPanel.add(priceLbl);
    		textPanel.add(nameBox);
    		textPanel.add(locationLbl);
    		textPanel.add(locationBox);
    		textPanel.setBounds(0,50,100,100);
    		textPanel.setVisible(true);
     
    		//create file chooser upload a picture
    		fileopen = new JFileChooser();
    		browseBtn = new JButton("Browse");
     
    		//create and add file chooser to panel
    		picturePanel = new JPanel();
    		picturePanel.add(fileopen);
    		picturePanel.add(browseBtn);
    		picturePanel.setSize(75,75);
    		picturePanel.setBounds(120,10,100,100);
     
    		//create buttons
    		btnPanel = new JPanel();
    		btnPanel.setLayout(new FlowLayout());
    		//btnPanel.setSize(getPreferredSize());
    		submitBtn = new JButton("Submit");
    		cancelBtn = new JButton("Cancel");
    		previewBtn = new JButton("Preview");
     
    		//add buttons to panel
    		btnPanel.add(submitBtn);
    		btnPanel.add(cancelBtn);
    		btnPanel.add(previewBtn);
    		btnPanel.setBounds(50,98,25,25);
    		btnPanel.setVisible(true);
     
    		//create description text area
    		descriptionLbl = new JLabel("Description:");
    		descriptionBox = new JTextArea();
     
    		//add to description text area to panel
    		descriptionPanel = new JPanel();
    		descriptionPanel.setLayout(new FlowLayout());
    		descriptionPanel.setSize(150,100);
    		descriptionPanel.add(descriptionLbl);
    		descriptionPanel.add(descriptionBox);
     
    		//place textFields to top left of frame
    		frame.add(textPanel);
    		//place picture panel into top right of frame
    		frame.add(picturePanel);
    		//place descriptionPanel to the center of the frame
    		frame.add(descriptionPanel);
    		//place buttons on bottom of frame
    		frame.add(btnPanel);
     
    		frame.setVisible(true);
    	}
     
    }

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Adding Panels to JFrame

    Defining your UIs using null layouts and the setBounds() method is unnecessarily complicated and will eventually (if not immediately) result in the programmer going to bed with a bad headache. The reason you chose not to use the available layout managers is . . . ?

  5. #5
    Member
    Join Date
    Apr 2014
    Posts
    219
    Thanks
    8
    Thanked 2 Times in 2 Posts

    Default Re: Adding Panels to JFrame

    I might have been over thinking it. I am thinking I need GroupLayout() for the JFrame and for each panel I will use different layout managers... Am I on the right track now?

  6. #6
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Adding Panels to JFrame

    Definitely better. GroupLayout may be more complicated than you need, but I don't know. Give it a whirl and see.

  7. #7
    Member
    Join Date
    Apr 2014
    Posts
    219
    Thanks
    8
    Thanked 2 Times in 2 Posts

    Default Re: Adding Panels to JFrame

    I am trying a new approach, putting a panel inside a panel then adding then adding that panel to the JFrame using GridBagConstraints which I named c.

    //add buttons to panel
    		btnPanel.add(submitBtn);
    		btnPanel.add(cancelBtn);
    		btnPanel.add(previewBtn);
    		//panel within a panel to position it in correct area on JFrame
    		btnPanelBox = new JPanel();
    		c.gridx = 5;
    		c.gridy = 500;
    		btnPanelBox.add(btnPanel,c);

    	//place buttons on bottom of frame
    		frame.add(btnPanelBox);

    Not doing what I want, am I at least getting warmer, any advice?

  8. #8
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Re: Adding Panels to JFrame

    My advice usually is to look into the MigLayout because its very flexible and can easily get you great results.
    The BorderLayout is also useful, but not as often. Never fear to use several panels each with different layouts to get exactly what you want.

  9. #9
    Member
    Join Date
    Apr 2014
    Posts
    219
    Thanks
    8
    Thanked 2 Times in 2 Posts

    Default Re: Adding Panels to JFrame

    MigLayout? I will look more into it. I started to use GredBagConstraints and it is not doing what I want either, this is that I have:

    package addItemBtn.Home.DataBase;
     
    import java.awt.BorderLayout;
    import java.awt.FlowLayout;
    import java.awt.GridBagConstraints;
    import java.awt.GridLayout;
     
    import javax.swing.*;
     
    public class AddItemView
    {
     
    	JFrame frame;
    	JPanel btnPanel,descriptionPanel,picturePanel,textPanel;
    	JPanel btnPanelBox,textPanelBox,descriptionPanelBox,picturePanelBox;
    	JTextField nameBox,priceBox,locationBox;
    	JLabel nameLbl,descriptionLbl,priceLbl,locationLbl,pictureLbl;
    	JTextArea descriptionBox;
    	JButton submitBtn,cancelBtn,previewBtn,browseBtn;
     
    	JFileChooser fileopen;
     
    	public AddItemView()
    	{
    		//set up frame
    		frame = new JFrame("Add Item");
    		frame.setSize(750,500);
    		//frame.setLayout(null);
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
    		//to position panels in correct positions
    		GridBagConstraints c = new GridBagConstraints();
     
    		//create text boxes and labels
    		nameLbl = new JLabel("Name:");
    		nameBox = new JTextField(15);
     
    		priceLbl = new JLabel("Price:");
    		priceBox = new JTextField(15);
     
    		locationLbl = new JLabel("Location:");
    		locationBox = new JTextField(15);
     
    		//add text fields to textPanel
    		textPanel = new JPanel();
    		textPanel.setLayout(new GridLayout(6,4));
    		textPanel.setSize(100,75);
    		//panel within a panel to position it in correct area on JFrame
    		textPanelBox = new JPanel(); 
     
     
    		textPanel.add(nameLbl);
    		textPanel.add(nameBox);
    		textPanel.add(priceLbl);
    		textPanel.add(nameBox);
    		textPanel.add(locationLbl);
    		textPanel.add(locationBox);
    		c.gridx = 1;
    		c.gridy = 1;
    		textPanelBox.add(textPanel,c);
     
    		//create file chooser upload a picture
    		fileopen = new JFileChooser();
    		browseBtn = new JButton("Browse");
     
    		//create and add file chooser to panel
    		picturePanel = new JPanel();
    		picturePanel.add(fileopen);
    		picturePanel.add(browseBtn);
    		//adding a panel to a panel so that it position right on the JFrame
    		picturePanelBox = new JPanel();
    		c.gridx = 100;
    		c.gridy = 3;
    		picturePanelBox.add(picturePanel);
     
    		//create buttons
    		btnPanel = new JPanel();
    		btnPanel.setLayout(new FlowLayout());
    		//btnPanel.setSize(getPreferredSize());
    		submitBtn = new JButton("Submit");
    		cancelBtn = new JButton("Cancel");
    		previewBtn = new JButton("Preview");
     
    		//add buttons to panel
    		btnPanel.add(submitBtn);
    		btnPanel.add(cancelBtn);
    		btnPanel.add(previewBtn);
    		//panel within a panel to position it in correct area on JFrame
    		btnPanelBox = new JPanel();
    		c.gridx = 5;
    		c.gridy = 500;
    		btnPanelBox.add(btnPanel,c);
     
    		//create description text area
    		descriptionLbl = new JLabel("Description:");
    		descriptionBox = new JTextArea();
     
    		//add to description text area to panel
    		descriptionPanel = new JPanel();
    		descriptionPanel.setLayout(new FlowLayout());
    		descriptionPanel.setSize(150,100);
    		descriptionPanel.add(descriptionLbl);
    		descriptionPanel.add(descriptionBox);
     
    		descriptionPanelBox = new JPanel();
    		c.gridx = 5;
    		c.gridy = 50;
    		descriptionPanelBox.add(descriptionPanel);
     
    		//place textFields to top left of frame
    		frame.getContentPane().add(textPanelBox,BorderLayout.NORTH);
    		//place picture panel into top right of frame
    		frame.getContentPane().add(picturePanelBox,BorderLayout.NORTH);
    		//place descriptionPanel to the center of the frame
    		frame.getContentPane().add(descriptionPanelBox,BorderLayout.CENTER);
    		//place buttons on bottom of frame
    		frame.getContentPane().add(btnPanelBox,BorderLayout.SOUTH);
     
    		frame.setVisible(true);
    	}
     
    }

  10. #10
    Member
    Join Date
    Apr 2014
    Posts
    219
    Thanks
    8
    Thanked 2 Times in 2 Posts

    Default Re: Adding Panels to JFrame

    I looked into MigLayout and got pretty excited. However I am getting some funny errors.

    There was an error java.awt.event.ActionEvent[ACTION_PERFORMED,cmd=Add Item,when=1411410455116,modifiers=Button1] on javax.swing.JButton[,0,29,200x29,alignmentX=0.0,alignmentY=0.5,border=com.apple.laf.AquaButtonBorder$Dynamic@10bc5c9,flags=288,maximumSize=,minimumSize=,preferredSize=,defaultIcon=,disabledIcon=,disabledSelectedIcon=,margin=javax.swing.plaf.InsetsUIResource[top=0,left=2,bottom=0,right=2],paintBorder=true,paintFocus=true,pressedIcon=,rolloverEnabled=false,rolloverIcon=,rolloverSelectedIcon=,selectedIcon=,text=Add Item,defaultCapable=true]

    Here is my code
    package addItemBtn.Home.DataBase;
     
    import java.awt.FlowLayout;
    import java.awt.GridLayout;
    import java.awt.ScrollPane;
     
    import javax.swing.*;
     
    import net.miginfocom.swing.MigLayout;
     
    public class AddItemView
    {
     
    	JFrame frame;
    	JPanel btnPanel,descriptionPanel,picturePanel,textPanel;
    	JTextField nameBox,priceBox,locationBox;
    	JLabel nameLbl,descriptionLbl,priceLbl,locationLbl,pictureLbl;
    	JTextArea descriptionBox;
    	JButton submitBtn,cancelBtn,previewBtn,browseBtn;
     
    	JFileChooser fileopen;
     
    	public AddItemView()
    	{
    		//set up frame
    		frame = new JFrame("Add Item");
    		frame.setSize(750,500);
    		frame.setLayout(new MigLayout());
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
    		//create text boxes and labels
    		nameLbl = new JLabel("Name:");
    		nameBox = new JTextField(15);
     
    		priceLbl = new JLabel("Price:");
    		priceBox = new JTextField(15);
     
    		locationLbl = new JLabel("Location:");
    		locationBox = new JTextField(15);
     
    		//add text fields to textPanel
    		textPanel = new JPanel();
    		textPanel.setLayout(new MigLayout());
    		textPanel.setSize(100,75);
     
    		textPanel.add(nameLbl);
    		textPanel.add(nameBox);
    		textPanel.add(priceLbl);
    		textPanel.add(nameBox);
    		textPanel.add(locationLbl);
    		textPanel.add(locationBox,"Wrap");
     
     
    		//create file chooser upload a picture
    		fileopen = new JFileChooser();
    		//browseBtn = new JButton("Browse");
     
    		//create and add file chooser to panel
    		picturePanel = new JPanel();
    		picturePanel.add(fileopen);
    		//picturePanel.add(browseBtn);
     
     
    		//create buttons
    		btnPanel = new JPanel();
    		btnPanel.setLayout(new MigLayout());
    		submitBtn = new JButton("Submit");
    		cancelBtn = new JButton("Cancel");
    		previewBtn = new JButton("Preview");
     
    		//add buttons to panel
    		btnPanel.add(submitBtn);
    		btnPanel.add(cancelBtn);
    		btnPanel.add(previewBtn,"Wrap");
     
    		//create description text area
    		descriptionLbl = new JLabel("Description:");
     
    		//add to description text area to panel
    		descriptionPanel = new JPanel();
    		descriptionPanel.setSize(150,100);
    		descriptionPanel.add(descriptionLbl);
     
    		//place textFields to top left of frame
    		frame.add(textPanel,"cell 0 1");
    		//place picture panel into top right of frame
    		frame.add(picturePanel,"cell 7,1");
    		//place descriptionPanel to the center of the frame
    		frame.add(descriptionPanel,"cell 5,7");
    		//place buttons on bottom of frame
    		frame.add(btnPanel,"dock north");
     
    		frame.setVisible(true);
    	}
     
    }

    Also is it smart to put everything in the constructor like this? I seen some examples where a guy made each panel its own method and just returned the panel.

  11. #11
    Member
    Join Date
    Apr 2014
    Posts
    219
    Thanks
    8
    Thanked 2 Times in 2 Posts

    Default Re: Adding Panels to JFrame

    also posted at MigLayout not working

  12. #12
    Member
    Join Date
    Apr 2014
    Posts
    219
    Thanks
    8
    Thanked 2 Times in 2 Posts

    Default Re: Adding Panels to JFrame

    Ok I got it working and greatly improved my code style. I will make a new thread because I have a different issue.

  13. #13

    Default Re: Adding Panels to JFrame

    Here in your main method, you're adding an instance of the Customer class to a JTabbedPane and then adding that JTabbedPane to the JFrame:

    Java Code:

    public static void main(String[] args)
    {
    Customer cust = new Customer(); // create Customer instance
    JTabbedPane tab = new JTabbedPane();
    JFrame frame = new JFrame("Airline");
    tab.add("Customer", cust); // add Customer instance to JTabbedPane
    tab.add("Flight", null);
    tab.add("Reservations", null);
    frame.add(tab, BorderLayout.CENTER); // add the JTabbedPane to the JFrame
    So therefore your components must be somehow added to the Customer JPanel itself. It's fine to add them to a JPanel called panel from within Customer if you eventually add that JPanel to customer via:

    Java Code:
    public Customer()
    {
    // Instantiate labels
    label1 = new JLabel("First Name");
    label2 = new JLabel("Last Name");
    label3 = new JLabel("Customer Status");
    label4 = new JLabel("Dependendt Age");
    label5 = new JLabel("Customer ID");

    // Instantiate text boxes
    text1 = new JTextField(10);
    text2 = new JTextField(10);

    // Set the Grid Layout
    setLayout( new GridLayout(5,2));

    // Add items to grid
    panel.add(label1);
    panel.add(text1);

    add(panel); // ****** here ***** add panel to Customer
    }

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Adding Panels to JFrame

    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Replies: 0
    Last Post: February 15th, 2013, 05:24 PM
  2. Replies: 6
    Last Post: December 17th, 2012, 07:54 AM
  3. adding or removing panels to panels
    By luisp88 in forum AWT / Java Swing
    Replies: 3
    Last Post: November 1st, 2011, 04:37 PM
  4. [SOLVED] What is not right here? adding JPanel in JFrame
    By Asido in forum AWT / Java Swing
    Replies: 2
    Last Post: August 23rd, 2010, 08:16 AM
  5. Adding panels to a central panel.
    By Johannes in forum AWT / Java Swing
    Replies: 3
    Last Post: July 4th, 2010, 05:31 PM