Welcome to Dream.In.Code
Getting Java Help is Easy!

Join 136,047 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,518 people online right now. Registration is fast and FREE... Join Now!




Trouble adding a JPanel to a JPanel

 
Reply to this topicStart new topic

Trouble adding a JPanel to a JPanel, the panel doesnt show up

Noldona
3 Aug, 2008 - 03:44 PM
Post #1

Addicted to DIC
****

Joined: 2 Jul, 2002
Posts: 934



Thanked: 2 times
My Contributions
I am trying to add a JPanel (lets call it cleric) with a TitledBorder and several control to a JPanel (call this one page4). If I add the controls to page4 they show up. However, if I add the controls to cleric then add cleric to page4 it nothing shows up. I don't understand why is this isn't working.

CODE

        SpringLayout layout = new SpringLayout();
        JPanel page4 = new JPanel(layout);
        JPanel cleric = new JPanel(layout);

                // code here was omitted due to irrelevance with problem

        // Page 4 Items
        
        TitledBorder clericBorder = BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.BLACK), "Clerics");
        cleric.setBorder(clericBorder);
        
        txtDomain1 = new JTextField();
        txtDomain1.setPreferredSize(new Dimension(80, 20));
        cleric.add(txtDomain1);
        layout.putConstraint(SpringLayout.NORTH, txtDomain1, 5, SpringLayout.NORTH, cleric);
        layout.putConstraint(SpringLayout.WEST, txtDomain1, 5, SpringLayout.WEST, cleric);
        
        lblDomain1 = new JLabel(smallText("Domain Name"));
        cleric.add(lblDomain1);
        layout.putConstraint(SpringLayout.NORTH, lblDomain1, -3, SpringLayout.SOUTH, txtDomain1);
        layout.putConstraint(SpringLayout.WEST, lblDomain1, 0, SpringLayout.WEST, txtDomain1);
        
        txtGrantedPower1 = new JTextField();
        txtGrantedPower1.setPreferredSize(new Dimension(80,20));
        cleric.add(txtGrantedPower1);
        layout.putConstraint(SpringLayout.NORTH, txtGrantedPower1, 5, SpringLayout.NORTH, cleric);
        layout.putConstraint(SpringLayout.WEST, txtGrantedPower1, 5, SpringLayout.EAST, txtDomain1);
        
        lblGrantedPower1 = new JLabel(smallText("Granted Power"));
        cleric.add(lblGrantedPower1);
        layout.putConstraint(SpringLayout.NORTH, lblGrantedPower1, -3, SpringLayout.SOUTH, txtGrantedPower1);
        layout.putConstraint(SpringLayout.WEST, lblGrantedPower1, 0, SpringLayout.WEST, txtGrantedPower1);
        
        txtDomain2 = new JTextField();
        txtDomain2.setPreferredSize(new Dimension(80, 20));
        cleric.add(txtDomain2);
        layout.putConstraint(SpringLayout.NORTH, txtDomain2, 5, SpringLayout.SOUTH, lblDomain1);
        layout.putConstraint(SpringLayout.WEST, txtDomain2, 5, SpringLayout.WEST, cleric);
        
        lblDomain2 = new JLabel(smallText("Domain Name"));
        cleric.add(lblDomain2);
        layout.putConstraint(SpringLayout.NORTH, lblDomain2, -3, SpringLayout.SOUTH, txtDomain2);
        layout.putConstraint(SpringLayout.WEST, lblDomain2, 0, SpringLayout.WEST, txtDomain2);
        
        txtGrantedPower2 = new JTextField();
        txtGrantedPower2.setPreferredSize(new Dimension(80,20));
        cleric.add(txtGrantedPower2);
        layout.putConstraint(SpringLayout.NORTH, txtGrantedPower2, 5, SpringLayout.SOUTH, lblDomain1);
        layout.putConstraint(SpringLayout.WEST, txtGrantedPower2, 5, SpringLayout.EAST, txtDomain2);
        
        lblGrantedPower2 = new JLabel(smallText("Granted Power"));
        cleric.add(lblGrantedPower2);
        layout.putConstraint(SpringLayout.NORTH, lblGrantedPower2, -3, SpringLayout.SOUTH, txtGrantedPower2);
        layout.putConstraint(SpringLayout.WEST, lblGrantedPower2, 0, SpringLayout.WEST, txtGrantedPower2);
        
        cleric.setPreferredSize(new Dimension(300, 300));
        cleric.setBackground(Color.BLUE);        // added to see if the panel shows up or not
        page4.add(cleric);
        layout.putConstraint(SpringLayout.NORTH, cleric, 5, SpringLayout.NORTH, page4);
        layout.putConstraint(SpringLayout.WEST, cleric, 5, SpringLayout.WEST, page4);
        
        // Tabbed Pane
        tabbedPane.addTab("Page 1", null, page1);
        tabbedPane.addTab("Page 2", null, page2);
        tabbedPane.addTab("Page 3", null, page3);
        tabbedPane.addTab("Page 4", null, page4);
        this.add(tabbedPane, BorderLayout.CENTER);




User is offlineProfile CardPM
+Quote Post

pbl
RE: Trouble Adding A JPanel To A JPanel
3 Aug, 2008 - 04:53 PM
Post #2

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 3,093



Thanked: 201 times
Dream Kudos: 75
My Contributions
Noldona,

I am writing Java code for 6 years... never heard of SpringLayout
Went to the API and find it fascinating... this SpingLayout thing would have solved thousand of problems I had

Now, if you don't post all your code we cannot cut & paste it to try it and found where your problem is
Anyhow despite that, I see a lot of my old applications where "your" SpringLayout will replace old GridLayout with fake/empty JLabel

Thanks a lot

Paul-Benoit Larochelle
User is offlineProfile CardPM
+Quote Post

Noldona
RE: Trouble Adding A JPanel To A JPanel
3 Aug, 2008 - 05:45 PM
Post #3

Addicted to DIC
****

Joined: 2 Jul, 2002
Posts: 934



Thanked: 2 times
My Contributions
Well, the problem with posting all of my code is that the program this is from is a LARGE program. It uses the Java Plugin Framework and actually consists of a couple different plugins. The rest of the code works fine. It is when I got to this part that I had the problem. The class that code came from extends JPanel. That code is in a function that is called from the constructor. The instance of the class is set at the contentPane of an JInternalFrame. That JInternalFrame is added to a JDesktopPane which is set at the ViewportView of a JScrollPane. That JScrollPane is set to one side of a JSplitPane which is added to the contentPane of a JFrame. In the GUI creation function for the class that extends JPanel, there is a JTabbedPane which is added to it. The JTabbedPage has 4 tabs, the first 3 tabs of which is the omitted code and all work fine. Tab 4 is the one I am having problems with and the code for that tab is posted. When I run my code, tab 4 shows up but nothing shows on the tab.

As for the SpringLayout, I am new to Java. I have been working in it for about 2 months now. I have plenty of programming experience in other languages though. Most of my experience is with VB and C++ with the Visual Studio IDE. I always like the way that worked with designing the GUIs. So when I started working with Java and read about these layout managers I was annoyed. So I searched for better options and found the SpringLayout which I really like. It makes laying things out a lot easier.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/1/08 05:11PM

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month