Results 1 to 3 of 3
  1. #1
    atrodeyo is offline Member
    Join Date
    Apr 2012
    Posts
    41
    Rep Power
    0

    Default Creating Tabs with GridbagLayout

    Could anyone help me with creating tabs with GridBagLayout? Attached are my Tabs file and GridBagLayout file. Both of them works fine individually. I need help connecting them together. Please help. ( sorry I I couldnt attach so I copy pasted my code below.)
    Java Code:
     
    // For Creating tabs
    public class tabPanels extends JFrame{ 
    
    	JFrame myFrame = null;
    	// main to run and test this part of the program
    /**	
    public static void main(String [] args)  {  
        new tabPanels(); 
    } 
    */
    	public tabPanels() { 
    	    JTabbedPane tp = new JTabbedPane(); 
    	    add(tp); 
    	 
    	    tp.addTab("Tab1",new JPanel()); 
    	    tp.addTab("Tab2", new JPanel()); 
    	    tp.addTab("Tab3", new JPanel()); 
    	 
    	    setPreferredSize(new Dimension(1360,768));
    	    //   myFrame.setPreferredSize(new Dimension(1360,768)); 
    	    pack();
    	    setVisible(true);
    	} 
    
    }
    
    
    // GridBagLayout where I am trying to call TabClass and this is where I need help
    
    public class layerTwoGui extends JPanel{
    	
    	JFrame myFrame;
    	tabPanels tabs;
    	
    	public layerTwoGui () {
    		
    		myFrame = null;
    		tabs = new tabPanels (); 
    		
    		JTabbedPane tabbedPane = new JTabbedPane();
    		JPanel tabPanel1 = new JPanel();
        	tabbedPane.addTab("Tab1", tabPanel1);
        //	add(tabbedPane, GridBagLayout);
    	}
    	
    
    	
        public static void addComponentsToPane(Container pane) {
       
        	JPanel newPanel = new JPanel ();
            JButton button;
    		pane.setLayout(new GridBagLayout());
    		GridBagConstraints c = new GridBagConstraints();
    	
    		c.gridx = 0; // column
    		c.gridy = 0; // row
    	
    		JLabel label1 = new JLabel ("");
    		JTextField title;
    	
    		c.weightx = 0.5;
    	
    		c.gridx = 2; // column 
    		c.gridy = 0; // row
    		pane.add(label1, c);
    	
    		Font font = new Font ("Ariel", Font.BOLD,12); // To set up font 
    		title = new JTextField (15);
    		title.setColumns(5);
    		title.setText("My Text");
    		title.setHorizontalAlignment(JTextField.CENTER);  
    		title.setFont(font);
    		title.setColumns(7);
    		title.setEditable(false);
    		title.setBackground(Color.WHITE);
    	//	c.fill = GridBagConstraints.HORIZONTAL;
    		c.weightx = 0.5;
    		c.gridx = 1; //column
    		c.gridy = 0; // row
    		c.insets = new Insets(50,150,0,150);  //top padding
    		pane.add(title, c);
    	
    		button = new JButton("Button One");
    		c.fill = GridBagConstraints.HORIZONTAL;
    		c.ipady = 150;      //make this component tall
    		c.weightx = 0.0;
    		c.gridwidth = 1;
    		c.insets = new Insets(50,500,0,500);
    		c.gridx = 1; //column
    		c.gridy = 1; // row
    		pane.add(button, c);
    		
    		button = new JButton("Button two");
    		button.disable();
    		c.fill = GridBagConstraints.HORIZONTAL;
    		c.ipady = 100;      //make this component tall
    		c.weightx = 0.0;
    		c.gridwidth = 1;
    		c.insets = new Insets(0,550,0,550);
    		c.gridx = 1; //column
    		c.gridy = 2; // row
    		pane.add(button, c);
    
    		button = new JButton("Previous Screen");
    		c.ipady = 0;       //reset to default
    		c.weighty = 1.0;   //request any extra vertical space
    		c.anchor = GridBagConstraints.LAST_LINE_START; //bottom of space
    		c.insets = new Insets(50,550,0,550);  //top padding
    		c.gridx = 1;       //aligned with button 2
    		pane.add(button, c);
    			
    	    }
    
        public Insets insets () {
        	return new Insets (20, 20, 20, 20);
        }
        /**
         * Create the GUI and show it.  For thread safety,
         * this method should be invoked from the
         * event-dispatching thread.
         */
        private static void createAndShowGUI() {
            //Create and set up the window.
            JFrame frame = new JFrame("Test Layout");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
            //Set up the content pane.
            addComponentsToPane(frame.getContentPane());
    
            //Display the window.
               frame.setSize(1360,768);
            frame.setVisible(true);
        }
    
        public static void main(String[] args) {
            javax.swing.SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    createAndShowGUI();
                }
            });
        }
        
    }
    Last edited by DarrylBurke; 04-25-2012 at 08:17 AM. Reason: Corrected comment opening *

  2. #2
    atrodeyo is offline Member
    Join Date
    Apr 2012
    Posts
    41
    Rep Power
    0

    Default Re: Creating Tabs with GridbagLayout

    when I said TabClass I meant to say tabPanels class

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Creating Tabs with GridbagLayout

    To help us understand your code better, please edit your post above so that you use [code] [/code] tags around your code so that it retains its formatting and is readable. Also, please adhere to Java naming standards including capitalizing the first letter of classes. This will also make it much easier to help you.

Similar Threads

  1. SWT tabs with Draw2d
    By garyscarter in forum SWT / JFace
    Replies: 0
    Last Post: 07-07-2009, 04:31 PM
  2. Adding tabs from classes
    By bubbless in forum AWT / Swing
    Replies: 13
    Last Post: 03-08-2009, 01:52 AM
  3. JMenu with Tabs using swing
    By phil128 in forum AWT / Swing
    Replies: 1
    Last Post: 03-03-2009, 04:29 PM
  4. Replies: 0
    Last Post: 07-07-2008, 05:43 PM
  5. resize tabs in jtabbedpane
    By osval in forum New To Java
    Replies: 1
    Last Post: 08-02-2007, 04:02 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
  •