Shift to another JPanel in an JTabbedPane

Hi again,
As I already told you ppl, I'm quite new to java and more importantly Swing. I use the NetBeans IDE to make all GUI i require. I have multiple Tabs and i want to shift to another tab by code. i.e. when i press a button, it should automatically shift to the other tab (i have tried grabFocus() but nothing happens).
If, this is possible, then those of you using Firefox, might know what pressing Ctrl+Tab you can shift between the tabs, i want to be able to do this in my java app too. So... please help me!!!
Thanks a lot for your time (which is money)!

Hi again!
Well, a mixture of everything finally did the work! Here's the code i'm using finally:
Set tabSet = Collections.singleton(KeyStroke.getKeyStroke(KeyEvent.VK_TAB, 0));
        KeyStroke ctrlTab = KeyStroke.getKeyStroke(KeyEvent.VK_TAB, InputEvent.CTRL_DOWN_MASK);
        final int FORWARD = KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS;
        jTP_IR.setFocusTraversalKeys(FORWARD, tabSet);
        Action nextTab = new AbstractAction("nextTab") {
            public void actionPerformed(ActionEvent evt) {
                int idx = jTP_IR.getSelectedIndex();
                if (idx != -1) {
                idx = (idx + 1) % jTP_IR.getTabCount();
                jTP_IR.requestFocusInWindow();
                jTP_IR.setSelectedIndex(idx);
        InputMap im = jTP_IR.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
        im.put(ctrlTab, nextTab.getValue(Action.NAME));
        jTP_IR.getActionMap().put(nextTab.getValue(Action.NAME), nextTab);jTP_IR is the JTabbedPane
Thanks a lot everybody for your help!!

Similar Messages

  • Friends,  i  have a problem with safari . Whenever i am typing in the safari main bar , so in the halfway through the typing my sentence/words are shifting to another main google search and in the end i have to type again in google

    Friends, 
    i  have a problem with safari . Whenever i am typing in the safari main bar , so in the halfway through the typing my sentence/words are shifting to another main google search and in the end i have to type again in google. I have attached a sample screenshot of the safari problem which i am facing. In this particular example i wanted to write Print Screen, however halfway,my typing automatically shifted to google bar?? can you guys help me.

    Please read this whole message before doing anything.
    This procedure is a diagnostic test. It won’t solve your problem. Don’t be disappointed when you find that nothing has changed after you complete it.
    The purpose of this test is to determine whether the problem is localized to your user account. Enable guest logins* and log in as Guest. Don't use the Safari-only “Guest User” login created by “Find My Mac.”
    While logged in as Guest, you won’t have access to any of your personal files or settings. Applications will behave as if you were running them for the first time. Don’t be alarmed by this; it’s normal. If you need any passwords or other personal data in order to complete the test, memorize, print, or write them down before you begin.
    Test while logged in as Guest. Same problem?
    After testing, log out of the guest account and, in your own account, disable it if you wish. Any files you created in the guest account will be deleted automatically when you log out of it.
    *Note: If you’ve activated “Find My Mac” or FileVault in OS X 10.7 or later, then you can’t enable the Guest account. The "Guest User" login created by "Find My Mac" is not the same. Create a new account in which to test, and delete it, including its home folder, after testing.

  • How to place a JPanel at the center of another JPanel?

    Can anyone tell me how to place a JPanel inside another JPanel? The first JPanel consists of three JPanels each consisting of a label&textfield and are placed using BoxLayout. The second JPanel is a big blank screen. So, i would like to put the first JPanel at the center of second JPanel(horizontally & vertically). The problem is that i don't have any other component. So,if i try to use FlowLayout, i am able to put it at the center(horizontally, not vertically) only on the top edge. I tried to use BoxLayout. But, i couldn't(as i don't have any other elements). I tried to create some blank elements. But, they are messing up the elements of my JPanel. Any cluesssssssss??????????

    import java.awt.*;
    import javax.swing.*;
    public class CenteredLayout
        private JPanel getPanel()
            GridBagLayout gridbag = new GridBagLayout();
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.insets = new Insets(2,2,2,2);
            JPanel p = new JPanel(gridbag);
            addComponents(new JLabel("label 1"), new JTextField(8), p, gbc);
            addComponents(new JLabel("label 2"), new JTextField(8), p, gbc);
            addComponents(new JLabel("label 3"), new JTextField(8), p, gbc);
            JPanel panel = new JPanel(gridbag);
            panel.setBackground(Color.pink);
            gbc.anchor = GridBagConstraints.CENTER;
            gbc.insets = new Insets(0,0,0,0);
            panel.add(p, gbc);
            return panel;
        private void addComponents(Component c1, Component c2, Container c,
                                   GridBagConstraints gbc)
            gbc.anchor = GridBagConstraints.EAST;
            gbc.gridwidth = GridBagConstraints.RELATIVE;
            c.add(c1, gbc);
            gbc.anchor = GridBagConstraints.WEST;
            gbc.gridwidth = GridBagConstraints.REMAINDER;
            c.add(c2, gbc);
        public static void main(String[] args)
            JFrame f = new JFrame();
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.getContentPane().add(new CenteredLayout().getPanel());
            f.setSize(400,400);
            f.setLocation(200,200);
            f.setVisible(true);
    }

  • Set Location of JPanel in another JPanel

    Hello, i want to set the location of a JPanel in another JPanel.
    This is my code:
    * To change this template, choose Tools | Templates
    * and open the template in the editor.
    package nemo;
    * @author Administrator
    import java.awt.event.*;
    import javax.swing.*;
    import java.awt.*;
    public class BackgroundImage extends JFrame
         JScrollPane scrollPane;
         ImageIcon icon;
            ImageIcon iconDrinken;
         Image image;
         public BackgroundImage() {
              icon = new ImageIcon("startpage.JPG");
                    iconDrinken = new ImageIcon("drankje.JPG");
              JPanel panel = new JPanel() {
                   protected void paintComponent(Graphics g) {
                        //  Dispaly image at at full size
                        g.drawImage(icon.getImage(), 0, 0, null);
                        super.paintComponent(g);
                    JPanel panelDrinks = new JPanel() {
                        protected void paintComponent(Graphics g) {
                            g.drawImage(iconDrinken.getImage(), 0, 0, null);
                            super.paintComponent(g);
                    panelDrinks.setOpaque( false );
              panelDrinks.setPreferredSize( new Dimension(200, 215) );
              panel.setOpaque( false );
              panel.setPreferredSize( new Dimension(400, 400) );
              scrollPane = new JScrollPane( panel );
              getContentPane().add( scrollPane );
                    panel.add(panelDrinks);
         public static void main(String [] args)
              BackgroundImage frame = new BackgroundImage();
              frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
              frame.setSize(800, 600);
              frame.setLocationRelativeTo( null );
              frame.setVisible(true);
    }When i execute this i get this:
    [http://img208.imageshack.us/img208/738/nowml5.jpg]
    And i want the image in the first white box from the left.
    But how to do it???
    I've tried .setLocation and .setBounds
    Edited by: Sven.nl on Apr 17, 2008 2:42 AM
    Edited by: Sven.nl on Apr 17, 2008 2:43 AM

    I already found it. Thanks anyway.
                    JPanel panelDrinks = new JPanel() {
                        protected void paintComponent(Graphics g) {
                            g.drawImage(iconDrinken.getImage(), 100, 200, 200, 215, null);
                            super.paintComponent(g);
                    panelDrinks.setOpaque( false );
              panelDrinks.setPreferredSize( new Dimension(800, 600) );
                    panelDrinks.setLocation(300, 300);
                      Edited by: Sven.nl on Apr 17, 2008 3:05 AM

  • How to generate button action in another JPanel?

    I want to get a line draw in another JPanel 2 when I click the button in JPanel 1.
    Can I acheive it in any way?

    give JPanel 1 a pointer to JPanel 2
    add an actionlistener to the button in JPanel 1
    with some code like
    JPanel2.addLine();

  • Spaces: Word  windows freeze when shifted to another space.

    Having used Desktop Manager on Tiger for a couple of years, I was expecting a lot from Spaces.
    I frequently want to shift windows from one space to another. Instead of ApplealtO (in DTM), I now have to open all spaces using F13 and drag and drop the windows, which would be acceptable, if it worked flawlessly. Problem is that if I drag multiple Word documents to a different space, some of the windows freeze, while others don't. I never had this problem with Desktop Manager.
    Has anyone discovered a fix for this?

    I have not uses Desktop Manager but I have had discussions with other users on this forum about two different ways of viewing how a virtual desktop should work.
    The situation is that some people look at virtual desktops as being Workspace-oriented and some view them as Application-oriented, which is how Apple’s Spaces works. I would guess that Desktop Manager was Workspace-oriented and it will take some rethinking to adapt to how Spaces works.
    Applications get assigned to specific Spaces and moving windows out of that specific Space to create a workspace is different from how Spaces was designed, and can result in unwanted behavior.
    One way to get more workspace-oriented desktops is to assign the most frequently used applications to all Spaces. I have it set that way for the Finder and Stickies.
    Here is a discussion about the different orientations for virtual desktops:
    http://blogs.sun.com/bblfish/entry/whyapple_spaces_isbroken
    The best solution would be for Apple, in a future revision, to allow the user to select between the Workspace- or Application-oriented desktops by providing a button in the Spaces pane to choose between them. This might entail a lot of reprogramming, however.

  • Adding a jPanel to another jPanel help

    I have a class NavigationButtonsClass which has the navigationalPanel which in turn has buttons back, next ... etc.
    public JPanel navigationalPanel = new JPanel();
    I have another class ProjectClass in which I gave the following
    NavigationButtonsClass newNavigationButtonsClass = new NavigationButtonsClass();
    jPanel1.add(newNavigationButtonsClass.navigationalPanel, new XYConstraints(0, 0, 795, -1));
    this.getContentPane().add(jPanel1, new XYConstraints(1, 420, 798, 45));
    Is something wrong here. I am not able to see the navigationalPanel.
    Thanks.

    Modify ur NavigationButtonsClass like,
    public class NavigationButtonsClass extends JPanel
       add(backButton);
       add(cancelButton);
    }Now in ur ProjectClass
    this.getContentPane().add(new  NavigationButtonsClass(),BorderLayout.SOUTH);I have assumed ur Project class extends JFrame...

  • How to resize a JPanel within another JPanel

    I have a JApplet that contains contains a JPanel which has several other components on the JPanel.
    The JApplet is an image query front-end which can be used to find images based on a single geographic point, a geographic rectangle or a geographic point and a radius (in meters). I have a JComboBox that the user uses to select which type of query they would like to perform. To enter the query information, I have a separate JPanel that contains the query parameters.
    For the single geographic point, it is just a simple row with JLabels and JTextFields for the latitude and longitude.
    For the rectangle, it is essentially a 3x3 grid to handle the JLabels and JTextFileds for the latitude and longitude of the upper left and lower right corners.
    For the geographic point and radius, I reuse the JPanel for the single geographic point and add a new row for the radius.
    In my ChangeListener for the JComboBox, I first call parentPanel.remove (queryPanel) then call a method to create the specific queryPanel and call parentPanel.add (queryPanel). The only thing that happens when I change the JComboBox to a different query type is the portion of the queryPanel that was covered by the selection of the JComboBox never gets repainted and doesn't change to the new queryPanel.
    I wish I could provide code examples, but the computer that I am developing on is not on the Internet and it is extremely difficult to get anything off of it to put out on the Internet.
    Hopefully, someone can help me figure out what is going on and why my parentPanel is not updating to reflect the new queryPanel.

    GoDonkeys wrote:
    Sorry, but what is the EDT?Please start here: [Concurrency in Swing|http://java.sun.com/docs/books/tutorial/uiswing/concurrency/index.html]

  • By clicking the JButton getting another JPanel in place of before JPanel in

    my program having two JButtons ,Two JPanels,one JFrame.when i click on JButton1 ,JPanel1 with some content will be opened in the JFrame.After clicking on JButton2 ,JPanel2 with some cotent will be opened on the same JFrame at the same place before where the JPanel1 will be displayed.
    JButtons will be at the top.Below that in the same JFrame JPanels will be displayed
    plz help me..................

    plz help ..how to see the code that you sent

  • How can i use an inner Jpanel in another JPanel

    Hi, i need to put an inner panel in a container panel.I have been trying for hours but I couldn't make it work. Please waiting for help...
    public class Gui extends JFrame{
         public Gui() {
              setDefaultCloseOperation(EXIT_ON_CLOSE);
              setEnabled(true);
              setSize(500, 500);
              JPanel mainPanel=new JPanel();
              int v=ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS;
              int h=ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS;
              JScrollPane jsp=new JScrollPane(mainPanel,v,h);
              this.add(mainPanel);
              setContentPane(jsp);
                    //this part doesn't works
              JPanel innerPanel=new JPanel();
              innerPanel.setSize(50, 50);
              innerPanel.setLocation(100, 100);
              innerPanel.setBorder(BorderFactory.createLineBorder(Color.black));
              innerPanel.setBackground(Color.red);
                   //this part doesn't works
         public static void main(String[] args) {
              Gui gui= new Gui();
              gui.setVisible(true);
    }

    I'm sorry it's my bad that i have forgotten to add the innerPanel to mainPannel (But of course i tried it in the original code :) ). Now i made the changes you suggested but there are still problems.
    I can see the innerPanel now, but the size and the position of the innerPanel doesn't works.
    public Gui() {
              setDefaultCloseOperation(EXIT_ON_CLOSE);
              setEnabled(true);
              setSize(500, 500);
              JPanel mainPanel=new JPanel();
              int v=ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS;
              int h=ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS;
              JScrollPane jsp=new JScrollPane(mainPanel,v,h);
              getContentPane().add(jsp);
              JPanel innerPanel=new JPanel();
              innerPanel.setSize(500, 500);
              innerPanel.setLocation(400, 400);
              innerPanel.setBorder(BorderFactory.createLineBorder(Color.black));
              innerPanel.setBackground(Color.red);
                    mainPanel.add(innerPanel);
                    //also i tried
                    //getContentPane().add(innerPanel);
                    //or
                    //jsp.add(innerPanel);        
    ...

  • How can I change size rect in JPanel from another JPanel?

    I have code:
    import java.awt.*;
    import javax.swing.*;
    public class ProgramD {
    public static void main(String[] arg) {
    JFrame f=new JFrame();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setSize(840,440);
    f.setLayout(null);
    PanelLeft p1=new PanelLeft();
    p1.setSize(400,400);
    p1.setLocation(0,0);
    f.add(p1);
    PanelRight p2=new PanelRight();
    p2.setSize(400,400);
    p2.setLocation(400,0);
    f.add(p2);
    f.setVisible(true);
    class PanelLeft extends JPanel {
    Shape shape1;
    PanelLeft() {
    shape1=new Rectangle(30,30,100,50);
    public void paintComponent(Graphics g) {
    Graphics2D g2=(Graphics2D) g;
    g2.setColor(Color.red);
    g2.fill(shape1);
    class PanelRight extends JPanel {
    PanelRight() {
    setBackground(Color.green);
    JTextField field1=new JTextField(4);
    JTextField field2=new JTextField(4);
    JButton b=new JButton("resize");
    add(field1);
    add(field2);
    add(b);
    }I would like to wrote width and height size of rect and click resize button, rect must change its size. I may add ActionListener, but I don't know what I may get size from PanelLeft and set size to PanelLeft. How may I solve this problem?

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    public class ProgramDada{
      public static void main(String[] arg) {
        JFrame f=new JFrame();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        Container con = f.getContentPane();
        con.setLayout(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        PanelLeft p1 = new PanelLeft();
        p1.setPreferredSize(new Dimension(400, 400));
        gbc.gridx = 0;
        gbc.gridy = 0;
        con.add(p1, gbc);
        PanelRight p2 = new PanelRight(p1);
        p2.setPreferredSize(new Dimension(400,400));
        gbc.gridx = 1;
        con.add(p2, gbc);
        f.pack();
        f.setVisible(true);
    class PanelLeft extends JPanel {
      int x, y, w, h;
      Shape shape1;
      PanelLeft() {
        x = 30;
        y = 30;
        w = 100;
        h = 50;
      public void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2=(Graphics2D) g;
        shape1 = new Rectangle(x, y, w, h);
        g2.setColor(Color.red);
        g2.fill(shape1);
    class PanelRight extends JPanel implements ActionListener{
      PanelLeft pt;
      JTextField field1, field2;
      JButton b;
      PanelRight(PanelLeft p) {
        pt = p;
        setBackground(Color.green);
        field1 = new JTextField(4);
        field2 = new JTextField(4);
        b = new JButton("resize");
        add(field1);
        add(field2);
        add(b);
        b.addActionListener(this);
      public void actionPerformed(ActionEvent e){
        pt.w = Integer.parseInt(field1.getText());
        pt.h = Integer.parseInt(field2.getText());
        pt.repaint();
    }

  • Updating to another JPanel

    I am making a program that holds values for items that you sell and other info. The problem I am have is when I try to click on my button to switch to the next Panel it doesnt update the current one to change.
    In other words I want the destroy the panel I have up when I click a button and then create a new one in its place.
    Such as
    a Panel where you input the name and type of Item then go to setting the Cost and Sold for values

    in the future, Swing related questions should be posted in the Swing forum.
    Sounds like maybe you should be using a [url http://java.sun.com/docs/books/tutorial/uiswing/layout/visual.html]Card Layout.

  • JTabbedPane

    Hi,
    i created JTabbed Pane with 4 tabs. After that i added JPanel in each tab. Now i cannot add another JPanel in the JtabbedPane.
    Questions: Is it possible to add more than one JPanel inside the JtabbedPane?.
    I added but it doesn't worked out!!.
    some body help me out
    Thanks,

    PositiveLife wrote:
    ..Questions: Is it possible to add more than one JPanel inside the JtabbedPane?.
    ????Yes.
    Is it possible for you to:
    1) Fix that sticky '?' key?
    2) Think up more specific post titles than 'ComponentName'? "Adding 2 panels to 1 tab of JTabbedPane" would be a good subject line.
    I added but it doesn't worked out!!.3) And that sticky '!' key.
    Maybe it is not working because it is tired. You might in that case, try giving the JTabbedPane a warm glass of milk and a good night's rest. Try it in the morning.
    (Or to put that another way.)
    What exactly do you mean by 'not working'? The first panel appears but not the second? The 2nd but not the first 1st appears? Neither appears?

  • Resize Jpanel and JtabbedPane

    Hi All,
    i implement a resize process on jpanel that include JtabbedPane inside.
    it working fine except one thing.
    when i resize my panel it work fine, until some point it start to shrink and get smaller.
    i think it getting shrink because im getting to the end of my dialog.
    i want to avoid this beheviour and let the tabbedpanel extend as much as possible:
    this is my code:
    package test;
    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.GraphicsConfiguration;
    import java.awt.GridBagConstraints;
    import java.awt.GridBagLayout;
    import java.awt.HeadlessException;
    import java.awt.Point;
    import javax.swing.BorderFactory;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JTabbedPane;
    import javax.swing.SwingUtilities;
    public class BaseMain extends JFrame {
         Point sp;
         int     jPanelStartHeight;
         int     jPanelStartWidth;
         int     jTabPaneStartHeight;
         int     jTabPaneStartWidth;
         Point offset;
         private static final long serialVersionUID = 1L;
         private JPanel jContentPane = null;
         private JPanel jPanel = null;
         private JTabbedPane jTabbedPane = null;
         private JPanel jPanel1 = null;
         private JTabbedPane jTabbedPane1 = null;
         private JPanel jPanel2 = null;
         private JTabbedPane jTabbedPane2 = null;
         public BaseMain() throws HeadlessException {
              // TODO Auto-generated constructor stub
              super();
              initialize();
         public BaseMain(GraphicsConfiguration arg0) {
              super(arg0);
              // TODO Auto-generated constructor stub
              initialize();
         public BaseMain(String arg0) throws HeadlessException {
              super(arg0);
              // TODO Auto-generated constructor stub
              initialize();
         public BaseMain(String arg0, GraphicsConfiguration arg1) {
              super(arg0, arg1);
              // TODO Auto-generated constructor stub
              initialize();
          * This method initializes jPanel     
          * @return javax.swing.JPanel     
         private JPanel getJPanel() {
              if (jPanel == null) {
                   GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
                   gridBagConstraints1.fill = GridBagConstraints.BOTH;
                   gridBagConstraints1.weighty = 1.0;
                   gridBagConstraints1.weightx = 1.0;
                   gridBagConstraints1.gridheight=3;
                   jPanel = new JPanel();
                   jPanel.setLayout(new BorderLayout());
                   jPanel.setBorder(BorderFactory.createLineBorder(Color.black, 2));
                   jPanel.add(getJTabbedPane(),BorderLayout.SOUTH/* gridBagConstraints1*/);
                   jPanel.addMouseListener(new java.awt.event.MouseAdapter() {
                        public void mousePressed(java.awt.event.MouseEvent e) {
                             System.out.println("mousePressed()"); // TODO Auto-generated Event stub mousePressed()
                             //original size
                             jPanelStartHeight = jPanel.getHeight();
                             jPanelStartWidth  = jPanel.getWidth();
                             jTabPaneStartHeight = jTabbedPane.getHeight();
                             jTabPaneStartWidth  = jTabbedPane.getWidth();
                              offset = SwingUtilities.convertPoint(e.getComponent(),e.getPoint(),jPanel);
                   jPanel.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
                        public void mouseDragged(java.awt.event.MouseEvent e) {
                             System.out.println("mouseDragged()"); // TODO Auto-generated Event stub mouseDragged()
                             Point p = SwingUtilities.convertPoint(e.getComponent(),e.getPoint(), jContentPane);
                             jPanel.setSize(jPanelStartWidth,p.y+jPanelStartHeight);
                             jTabbedPane.setSize(jTabPaneStartWidth,p.y+jTabPaneStartHeight);
                             jPanel.setLocation(0/*p.x-offset.x*/,p.y-offset.y);
              return jPanel;
          * This method initializes jTabbedPane     
          * @return javax.swing.JTabbedPane     
         private JTabbedPane getJTabbedPane() {
              if (jTabbedPane == null) {
                   jTabbedPane = new JTabbedPane();
                   jTabbedPane.addTab(null, null, getJPanel1(), null);
                   jTabbedPane.addTab(null, null, getJPanel2(), null);
              return jTabbedPane;
          * This method initializes jPanel1     
          * @return javax.swing.JPanel     
         private JPanel getJPanel1() {
              if (jPanel1 == null) {
                   GridBagConstraints gridBagConstraints = new GridBagConstraints();
                   gridBagConstraints.fill = GridBagConstraints.BOTH;
                   gridBagConstraints.weighty = 1.0;
                   gridBagConstraints.weightx = 1.0;
                   jPanel1 = new JPanel();
                   jPanel1.setLayout(new GridBagLayout());
                   jPanel1.add(getJTabbedPane1(), gridBagConstraints);
              return jPanel1;
          * This method initializes jTabbedPane1     
          * @return javax.swing.JTabbedPane     
         private JTabbedPane getJTabbedPane1() {
              if (jTabbedPane1 == null) {
                   jTabbedPane1 = new JTabbedPane();
              return jTabbedPane1;
          * This method initializes jPanel2     
          * @return javax.swing.JPanel     
         private JPanel getJPanel2() {
              if (jPanel2 == null) {
                   GridBagConstraints gridBagConstraints2 = new GridBagConstraints();
                   gridBagConstraints2.fill = GridBagConstraints.BOTH;
                   gridBagConstraints2.weighty = 1.0;
                   gridBagConstraints2.weightx = 1.0;
                   jPanel2 = new JPanel();
                   jPanel2.setLayout(new GridBagLayout());
                   jPanel2.add(getJTabbedPane2(), gridBagConstraints2);
              return jPanel2;
          * This method initializes jTabbedPane2     
          * @return javax.swing.JTabbedPane     
         private JTabbedPane getJTabbedPane2() {
              if (jTabbedPane2 == null) {
                   jTabbedPane2 = new JTabbedPane();
              return jTabbedPane2;
          * @param args
         public static void main(String[] args) {
              // TODO Auto-generated method stub
              SwingUtilities.invokeLater(new Runnable() {
                   public void run() {
                        BaseMain thisClass = new BaseMain();
                        thisClass.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                        thisClass.setVisible(true);
          * This method initializes this
          * @return void
         private void initialize() {
              this.setSize(300, 200);
              this.setContentPane(getJContentPane());
              this.setTitle("JFrame");
          * This method initializes jContentPane
          * @return javax.swing.JPanel
         private JPanel getJContentPane() {
              if (jContentPane == null) {
                   jContentPane = new JPanel();
                   jContentPane.setLayout(new BorderLayout());
                   jContentPane.add(getJPanel(), BorderLayout.SOUTH);
              return jContentPane;
    }

    hey gabi,
    i tried your code and it works just fine.... there is no shrinking or any resizing problem?! i don't know... maybe i just don't get it? so maybe u could describe your problem a little more?!
    :)

  • GridBagLayout not working inside jpanel in jtabbedpane

    Have any of you experienced similar behaviour before? Any ideas what could cause this? It seems that any other layout is working except the gridbaglayout. All the components I add will be in the center of the jpanel and on top of each other no matter what gridbagconstraints I use.

    import java.awt.*;
    import javax.swing.*;
    public class GridBag_Demo extends JFrame {
        public GridBag_Demo() {
            initComponents();
        private void initComponents() {
            GridBagConstraints gridBagConstraints;
            tabbedpane = new JTabbedPane();
            panel = new JPanel();
            button1 = new JButton();
            button2 = new JButton();
            button3 = new JButton();
            label1 = new JLabel();
            label2 = new JLabel();
            label3 = new JLabel();
            setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
            setTitle("GridBag Demo");
            panel.setLayout(new GridBagLayout());
            button1.setText("button1");
            gridBagConstraints = new GridBagConstraints();
            gridBagConstraints.weightx = 1.0;
            gridBagConstraints.weighty = 1.0;
            panel.add(button1, gridBagConstraints);
            button2.setText("button2");
            gridBagConstraints.gridy = 1;
            panel.add(button2, gridBagConstraints);
            button3.setText("button3");
            gridBagConstraints.gridy = 2;
            panel.add(button3, gridBagConstraints);
            label1.setText("label1");
            gridBagConstraints.gridx = 2;
            gridBagConstraints.gridy = 0;
            panel.add(label1, gridBagConstraints);
            label2.setText("label2");
            gridBagConstraints.gridy = 1;
            panel.add(label2, gridBagConstraints);
            label3.setText("label2");
            gridBagConstraints.gridy = 2;
            panel.add(label3, gridBagConstraints);
            tabbedpane.addTab("tab1", panel);
            getContentPane().add(tabbedpane, BorderLayout.CENTER);
            Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
            setBounds((screenSize.width-400)/2, (screenSize.height-300)/2, 400, 300);
        public static void main(String args[]) {
            new GridBag_Demo().setVisible(true);
        private JButton button1;
        private JButton button2;
        private JButton button3;
        private JLabel label1;
        private JLabel label2;
        private JLabel label3;
        private JPanel panel;
        private JTabbedPane tabbedpane;
    }

Maybe you are looking for