Results 1 to 2 of 2
  1. #1
    CodeMangler is offline Member
    Join Date
    Jun 2012
    Posts
    1
    Rep Power
    0

    Default JTabbedPane coinside w/ JPanel in JFrame help...

    Hey everyone. I'm new here so if I do something outside of scope please let me know. I'm in a bind, been struggling with this for a month now. Here's the situation:

    I'm creating a desktop application (without using GUI creater from Netbeans). I've created a JFrame which has two nested (child) JPanels inside. I'm trying to nest a JTabbedPane (with different JPanels already included) into JPanel1. Doing this creates no compilation error or API warnings but also does not show the JTabbedPane when compiled/ran. If I replace the JPanel1 with the JTabbedPane (so the JTabbedPane is directly on the JFrame), it is revealed, hogs the JFrame and I can no longer see JPanel2. It is critical I have a space left in the JFrame for JPanel2 but equally critical that JTabbedPane is also seen sharing the space on JFrame.

    Here's what I've done:

    I've Googled and found one similar question like mine as a result left unanswered.
    I've tried to review other peoples code to find a solution.
    Netbeans IDE can create what I'm looking for. I've created it, and reviewed the code but attempts to replicate have indifferent results from my problem.

    Here's the code. Maybe someone with more experience and a clean pair of eyes can see what I can't. Thanks for any help in advance.

    Java Code:
    //import java.awt.BorderLayout;
    import java.awt.*; //Replace when done
    //import java.awt.event.ActionEvent;
    //import java.awt.event.ActionListener;
    import java.awt.Color;
    //import java.awt.GridLayout;
    //import javax.swing.Timer;
    //import javax.swing.Icon;
    //import javax.swing.JDialog;
    //import javax.swing.JFrame;
    //import javax.swing.JPanel;
    //import javax.swing.JTabbedPane;
    //import javax.swing.JMenuBar;
    //import javax.swing.JButton;
    //import javax.swing.JMenu;
    //import javax.swing.JMenuItem;
    //import javax.swing.JSeparator;
    import javax.swing.*; //Replace when done
    
    
    public class ScribeInterface extends JPanel {
        
        //Main Window Declaration
        private JFrame mainWindowFrame = new JFrame();
        private JPanel tabbedPanel1 = new JPanel();
        private JPanel mainPanel1 = new JPanel();
        private JPanel mainPanel2 = new JPanel();
        private JTabbedPane tabbedPane = new JTabbedPane();
        private JPanel tabbedPanel2 = new JPanel();
        private GroupLayout mainPanel2Layout = new GroupLayout(mainPanel2);
        
        //Tabbed Panel Declaration
        private JButton button = new JButton("1"); 
        
        //Menu Bar Declaration
        private JMenuBar menuBar = new JMenuBar();
        private JMenu fileMenu = new JMenu("File");
        private JMenuItem menuItem1 = new JMenuItem("New");
        private JMenuItem menuItem2 = new JMenuItem("Save");
        
        //Button Declaration
        private JButton button1 = new JButton();
        private JButton button2 = new JButton();
    
        //Initiate interface.
        public void initComponents(){ 
            
            mainWindowFrame.setLayout(new BorderLayout());
            initMainWindowFrame();
            initMenuBar();
            initTabbedPanel1();
            initTabbedPanel2();
            initTabbedPane();        
            initMainPanel1();
            initMainPanel2();
            
            button1.setSize(50, 50);
            button1.setLocation(0, 0);
            button1.setName("Test Button 1");
            button1.setText("Test Button 1");
            button2.setSize(50, 50);
            button2.setLocation(0, 50);
            button2.setName("Test Button 2");
            button2.setText("Test Button 2");
    
            mainWindowFrame.setVisible(true);        
            
        }
        
        //The master container.
        private JFrame initMainWindowFrame(){
            mainWindowFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            mainWindowFrame.setSize(1400, 200);
            mainWindowFrame.setLocation(0, 0);
            mainWindowFrame.setTitle("MainProgram");
            mainWindowFrame.add(mainPanel1); 
                     
            return mainWindowFrame;
        }
        
        //A parent panel as child to the frame, that sits to the right of the frame container.
        private JPanel initMainPanel1(){
            
            mainPanel1.setSize(250, 0);
            mainPanel1.setLocation(800, 0);
            mainPanel1.setName("Main Panel 1");
            mainPanel1.setBackground(Color.red);
    
            return mainPanel1;
        }
        
        //A parent panel as child to the frame, that sits to the left of the frame container.
        private JPanel initMainPanel2(){
            
            
            mainPanel2.setSize(1150, 0);
            mainPanel2.setLocation(0, 0);
            mainPanel2.setName("Main Panel 2");
            mainPanel2.setBackground(Color.green);
            //mainPanel2.addComponent(tabbedPane); //***This needs to open in mainPanel2***
            mainPanel2.setLayout(mainPanel2Layout);
            
            /* Code imported from Netbeans to trial and error problem. */
            mainPanel2Layout.setHorizontalGroup(mainPanel2Layout.createParallelGroup(GroupLayout.Alignment.LEADING).
                    addGroup(mainPanel2Layout.createSequentialGroup().addContainerGap().
                    addComponent(tabbedPane, GroupLayout.PREFERRED_SIZE, 285, GroupLayout.PREFERRED_SIZE).
                    addContainerGap(24, Short.MAX_VALUE)));
            
            mainPanel2Layout.setVerticalGroup(mainPanel2Layout.createParallelGroup(GroupLayout.Alignment.LEADING).
                    addGroup(mainPanel2Layout.createSequentialGroup().addContainerGap().
                    addComponent(tabbedPane, GroupLayout.PREFERRED_SIZE, 285, GroupLayout.PREFERRED_SIZE).
                    addContainerGap(24, Short.MAX_VALUE)));
            /* End of import */
            
            return mainPanel2;        
        }
            
        
        //The menu bar in the frame container.
        private JMenuBar initMenuBar(){
            
            fileMenu.add(menuItem1); //Items in the menu, must be placed before the menuBar.
            menuBar.add(fileMenu); //MenuBar, must be placed before the setJMenuBar.
            mainWindowFrame.setJMenuBar(menuBar); //Add Menu Bar to window.
            
            fileMenu.add(menuItem1); //Items in the menu, must be placed before the menuBar.
            menuBar.add(fileMenu); //MenuBar, must be placed before the setJMenuBar.
            mainWindowFrame.setJMenuBar(menuBar); //Add Menu Bar to window.        
            
            return menuBar;
        }
        
        //A child tabbed pane nested within mainPanel1
        private JTabbedPane initTabbedPane(){
            
            tabbedPane.add("Tab 1", tabbedPanel1);
            tabbedPane.add("Tab 2", tabbedPanel2);
            tabbedPane.setSize(800, 100);
            
    
            return tabbedPane;
        }
        
        //A child panel nested within the tabbedPane1.
        private JPanel initTabbedPanel1(){
            
            tabbedPanel1.setSize(1150, 200);
            tabbedPanel1.setLocation(0, 0);
            tabbedPanel1.setBackground(Color.blue); 
            tabbedPanel1.add(button1);
            tabbedPanel1.add(button2);        
            
            return mainPanel1;
        }
        
        //A child panel nested within the tabbedPanel.
        private JPanel initTabbedPanel2(){
            tabbedPanel2.setSize(1150, 200);
            tabbedPanel2.setLocation(0, 0);
            tabbedPanel2.setBackground(Color.yellow);
            
            return tabbedPanel2;
        }
    }

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,193
    Rep Power
    19

    Default Re: JTabbedPane coinside w/ JPanel in JFrame help...

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Replies: 1
    Last Post: 12-13-2011, 02:38 AM
  2. Need help with JFrame and JPanel
    By mikhail.abraham in forum New To Java
    Replies: 12
    Last Post: 04-23-2011, 06:26 PM
  3. jpanel in jframe-pls help
    By archanaanbu in forum Java Applets
    Replies: 3
    Last Post: 02-14-2011, 05:01 AM
  4. JPanel/JFrame
    By Mayur in forum New To Java
    Replies: 2
    Last Post: 12-20-2009, 05:07 AM
  5. Problem extending JFrame with JTabbedPane in it
    By Cybex in forum AWT / Swing
    Replies: 9
    Last Post: 10-31-2009, 05:51 PM

Posting Permissions

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