Results 1 to 4 of 4
  1. #1
    Jose Antonio is offline Member
    Join Date
    Oct 2013
    Rep Power

    Default How to add myself Panel in other JPanel

    Hi everybody:

    Before to start, this is my first post here, so i hope if i break the rules of this forum u can forgive and correct me.

    Well i have a problem when i try to add a JPanel class in other JPanel class.

    This is the code of the class i want to add:

    Java Code:
    package visual;
    import javax.swing.JPanel;
    import javax.swing.JLabel;
    import javax.swing.SwingConstants;
    public class PanelCabecera extends JPanel {
    	 * Create the panel.
    	public PanelCabecera() {
    		JLabel lbltitle = new JLabel("HERE IS THE TITLE");
    		lbltitle.setBounds(215, 28, 273, 41);
    You can see there is nothing especial. Now i show the container class of this last:

    Java Code:
    package visual;
    import java.awt.Dimension;
    import java.awt.GridBagConstraints;
    import java.awt.GridBagLayout;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    public class PanelDirector extends JPanel {
    	private JLabel jLabelNORTE = null;
    	private JLabel jLabelOESTE = null;
    	private JLabel jLabelBLANCO1 = null;
    	private JLabel jLabelCABECERA = null; 
    	private JLabel jLabelBLANCO2 = null;
    	private JLabel jLabelTEXTFIELDS = null;
    	private JLabel jLabelBLANCO3 = null;
    	private JLabel jLabelBOTONERA = null;
    	private JLabel jLabelSUR = null;
    	private JLabel jLabelESTE = null;
    	private JLabel jLabelCONTRO = null;
            //TWO IMPORTANT ATTRIBUTES STATEMENS----------------------------------------------------------------------------
    	private PanelCabecera cabecera=null;
    	private JPanel jPanelPrueba= null;
    	 * Create the panel.
    	public PanelDirector() {
            //HERE I AM MAKING THE GRIDBAG and SETTING DIFFERENTS JLABELS (NOT IMPORTANT, I GUESS)---------------------------------
            GridBagConstraints gridBagConstraints19 = new GridBagConstraints();
            gridBagConstraints19.gridx = 3;
            gridBagConstraints19.gridy = 5;
            jLabelCONTRO = new JLabel();
            GridBagConstraints gridBagConstraints18 = new GridBagConstraints();
            gridBagConstraints18.gridx = 6;
            gridBagConstraints18.gridy = 3;
            jLabelESTE = new JLabel();
            GridBagConstraints gridBagConstraints17 = new GridBagConstraints();
            gridBagConstraints17.gridx = 0;
            gridBagConstraints17.gridy = 6;
            jLabelSUR = new JLabel();
            GridBagConstraints gridBagConstraints16 = new GridBagConstraints();
            gridBagConstraints16.gridx = 4;
            gridBagConstraints16.gridy = 4;
            jLabelBOTONERA = new JLabel();
            GridBagConstraints gridBagConstraints15 = new GridBagConstraints();
            gridBagConstraints15.gridx = 4;
            gridBagConstraints15.gridy = 2;
            jLabelBLANCO3 = new JLabel();
            GridBagConstraints gridBagConstraints14 = new GridBagConstraints();
            gridBagConstraints14.gridx = 1;
            gridBagConstraints14.gridy = 3;
            jLabelTEXTFIELDS = new JLabel();
            jLabelTEXTFIELDS.setText("<--CUADROS DE TEXTO-->");
            GridBagConstraints gridBagConstraints13 = new GridBagConstraints();
            gridBagConstraints13.gridx = 1;
            gridBagConstraints13.gridy = 2;
            jLabelBLANCO2 = new JLabel();
            GridBagConstraints gridBagConstraints12 = new GridBagConstraints();
            gridBagConstraints12.gridx = 2;
            gridBagConstraints12.gridy = 1;
            jLabelCABECERA = new JLabel();
            GridBagConstraints gridBagConstraints11 = new GridBagConstraints();
            gridBagConstraints11.gridx = 1;
            gridBagConstraints11.gridy = 1;
            jLabelBLANCO1 = new JLabel();
            GridBagConstraints gridBagConstraints10 = new GridBagConstraints();
            gridBagConstraints10.gridx = 0;
            gridBagConstraints10.gridy = 1;
            jLabelOESTE = new JLabel();
            GridBagConstraints gridBagConstraints = new GridBagConstraints();
            gridBagConstraints.gridx = 0;
            gridBagConstraints.gridy = 0;
            jLabelNORTE = new JLabel();
            GridBagLayout gridBagLayout = new GridBagLayout();
            gridBagLayout.columnWidths = new int[]{0, 0, 114, 0, 0, 0, 0};
            this.setSize(new Dimension(811, 305));
            //HERE I ADD THE LABELS IN THE DIFFERENTS gridBagConstraints-------------------------------------------------------
            this.add(jLabelNORTE, gridBagConstraints);
            this.add(jLabelOESTE, gridBagConstraints10);
            this.add(jLabelBLANCO1, gridBagConstraints11);
            this.add(jLabelBLANCO2, gridBagConstraints13);
            this.add(jLabelTEXTFIELDS, gridBagConstraints14);
            this.add(jLabelBLANCO3, gridBagConstraints15);
            this.add(jLabelBOTONERA, gridBagConstraints16);
            this.add(jLabelSUR, gridBagConstraints17);
            this.add(jLabelESTE, gridBagConstraints18);
            this.add(jLabelCONTRO, gridBagConstraints19);
            //HERE IS THE PROBLEM----------------------------------------------------------------------------------------------
            cabecera=new PanelCabecera();
            this.add(cabecera, gridBagConstraints12);
    My problem is with this segment of code:
    Java Code:
            cabecera=new PanelCabecera();
            this.add(cabecera, gridBagConstraints12);
    The variable "cabecera" type new PanelCabecera() is not show when i run the code. However when i swap this part for:

    Java Code:
          jPanelPrueba=new JPanel();
          jPanelPrueba.add(new JLabel("EXAMPLE");
          this.add(jPanelPrueba, gridBagConstraints12);
    it's work, but.... why?? why the first way doesn't work?? I am going crazy trying to make a sourcea that reuses other JPpanels class created for myself, but i cant show them.

    Please help me and thanks in advance.


  2. #2
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Rep Power

    Default Re: How to add myself Panel in other JPanel

    Probably because you are setting a null-layout manager. Remove this line

    Java Code:
    and try again. And word of advice: ALWAYS use layout managers. It will make your life a lot easier.

    And btw: if you're afraid to break forum rules (since you already managed to post code with formatting I don't think you will), read this:
    Last edited by gimbal2; 10-23-2013 at 11:54 AM.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  3. #3
    Jose Antonio is offline Member
    Join Date
    Oct 2013
    Rep Power

    Default Re: How to add myself Panel in other JPanel

    It's works!!!!!!

    Thanks gimbal2.

    We are learning to use visuals components but we use a palette to drag the components directly on a JPanel, and null layout is the easiest way to position. There are other layouts with this easy way to position the components?? I think gridBadLayout is fine, but hard for a class like my first class.

  4. #4
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Rep Power

    Default Re: How to add myself Panel in other JPanel

    Don't do that, learn to build the GUI through code. When you learn how to do that, positioning components IS easy and 100% under your control. That 'palette' you are talking about is for experienced Swing programmers who know what they are doing; only people with experience can make tools help them do what they want; you are still in the position where you are at the tool's mercy and you do what IT wants.

    Lesson: Laying Out Components Within a Container (The Java™ Tutorials > Creating a GUI With JFC/Swing)
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

Similar Threads

  1. Replies: 3
    Last Post: 01-19-2013, 06:53 AM
  2. Jpanel in layered panel.
    By dd2308 in forum New To Java
    Replies: 1
    Last Post: 10-09-2012, 12:41 AM
  3. panel on a panel not visible
    By warchieflll in forum Advanced Java
    Replies: 2
    Last Post: 01-29-2011, 09:29 PM
  4. Replies: 1
    Last Post: 03-22-2010, 10:03 AM
  5. Adding a panel to a panel
    By rclausing in forum New To Java
    Replies: 7
    Last Post: 02-02-2010, 06:56 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