6 Replies Latest reply: Jun 7, 2010 4:49 PM by 843807 RSS

    gridbag layout problem on a tabbedpane

    843807
      I am not sure whether we can do this or not but, I am trying to have a gridbag layout on a TabbedPane object.

      I have a JFrame on which I am adding a TabbedPane object called "t" and on this TabbedPane I am adding a tab called "Insert" which is an object of class "Insert Data". Then, I add the TabbedPane t on the Container cp, which is inside the JFrame.

      In the InsertData Class (a JPanel), I need to have the gridbag layout. With this gridbag layout object, I am trying to place different objects like buttons, at various places, on this JPanel. But nothing moves on this panel.

      In short, please let me know how can I have a gridbag layout on a Tabbedpane.

      The Main Class is as follows:
      import java.awt.Color;
      import java.awt.Container;
      import java.awt.Dimension;
      import java.awt.FlowLayout;
      
      import javax.swing.JFrame;
      import javax.swing.JTabbedPane;
      import javax.swing.event.ChangeEvent;
      import javax.swing.event.ChangeListener;
      
      import java.awt.BorderLayout;
      import java.awt.event.ActionEvent;
      import java.awt.event.ActionListener;
      
      public class Main extends JFrame implements ActionListener, ChangeListener{
                     
           Main(){
                     setPreferredSize(new Dimension(1200,600));
                     
                     
                     Container cp = getContentPane();
                     
                     JTabbedPane t = new JTabbedPane();
                     
                     
                     // insert
                     InsertData insertOptions = new InsertData();
                
                     t.addTab("Insert",insertOptions);
                     
                     cp.add(t);
                     
                     this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                     pack();
                     setVisible(true);
                }
      
                
                @Override
                public void actionPerformed(ActionEvent arg0) {
                     // TODO Auto-generated method stub
                     
                }
      
      
                @Override
                public void stateChanged(ChangeEvent arg0) {
                     // TODO Auto-generated method stub
                     
                }
                
                public static void main(String args[]){
                     new Main();
                }
                     
           }
      The InsertDataClass is:
      import java.awt.BorderLayout;
      import java.awt.Color;
      import java.awt.Dimension;
      import java.awt.FlowLayout;
      import java.awt.GridBagConstraints;
      import java.awt.GridBagLayout;
      import java.awt.Insets;
      
      import javax.swing.JButton;
      import javax.swing.JFrame;
      import javax.swing.JPanel;
      
      
      public class InsertData extends JPanel{
           InsertData(){
                setPreferredSize(new Dimension(1200,600));
                setBackground(Color.blue);
                
                //setLayout(new GridBagLayout());
                GridBagLayout gb = new GridBagLayout();
                setLayout(gb);
                GridBagConstraints c = new GridBagConstraints();
                //c.insets = new Insets(2, 2, 2, 2);
      
                //JPanel p1= new JPanel();
                //p1.setPreferredSize(new Dimension(200,200));
                JButton b1 = new JButton("here i am!!!");
                //p1.add(b1);
                //c.fill = GridBagConstraints.HORIZONTAL;
                //c.anchor = GridBagConstraints.WEST;
                c.gridx=0;
                c.gridy=1;
                add(b1,c);
                
           }
      }
        • 1. Re: gridbag layout problem on a tabbedpane
          DarrylBurke
          how can I have a gridbag layout on a Tabbedpane.
          Huh? You post an example with just one JButton and no weightx / weighty set and you expect others here to be able to see a problem with your layout?

          Also, there's needless, unused code that is just clutter -- like the unimplemented ActionListener and ChaneListener. Recommended reading: [SSCCE (Short, Self Contained, Compilable and Executable, Example Program)|http://mindprod.com/jgloss/sscce.html].

          Finally, I don't see any need whatsoever to extend JFrame and JPanel as you are not introducing any new behavior of either. Always favor composition over inheritance.

          I plugged in some code I had used for someone else's GridBagLayout problems and this will show you that there's no difference in using GridBagLayout in a tabbed pane component or anywhere else.
          import java.awt.Dimension;
          import java.awt.Font;
          import java.awt.GridBagConstraints;
          import java.awt.GridBagLayout;
          import java.awt.Insets;
          import javax.swing.*;
          
          public class GridBagInTabbedPane {
          
            public static void main(String[] args) {
              SwingUtilities.invokeLater(new Runnable() {
          
                @Override
                public void run() {
                  new GridBagInTabbedPane().makeUI();
                }
              });
            }
          
            public void makeUI() {
              Font fontButton = new Font("Arial", Font.BOLD, 10);
              Font fontLabel = new Font("Arial", Font.BOLD, 15);
          
              JLabel labelEnter = new JLabel("Enter sentences in the text area ");
              labelEnter.setFont(fontLabel);
          
              JTextArea textArea = new JTextArea();
              textArea.setPreferredSize(new Dimension(630, 280));
              JScrollPane scrollPane = new JScrollPane(textArea);
              scrollPane.setMinimumSize(new Dimension(630, 280));
          
              JLabel labelDuplicates = new JLabel("Duplicates will appear here");
              labelDuplicates.setMinimumSize(new Dimension(650, 30));
              labelDuplicates.setFont(fontLabel);
          
              JButton buttonDisplay = new JButton("Display Map");
              buttonDisplay.setFont(fontButton);
          
              JButton buttonClear = new JButton("Clear");
              buttonClear.setFont(fontButton);
              JPanel panel = new JPanel(new GridBagLayout());
              GridBagConstraints gbc = new GridBagConstraints();
              gbc.gridx = 0;
              gbc.gridy = 0;
              gbc.gridwidth = 2;
              gbc.fill = GridBagConstraints.NONE;
              gbc.anchor = GridBagConstraints.CENTER;
              gbc.insets = new Insets(5, 5, 5, 5);
              gbc.weightx = 0.5;
              panel.add(labelEnter, gbc);
          
              gbc.gridy = 1;
              gbc.fill = GridBagConstraints.BOTH;
              gbc.weighty = 0.5;
              panel.add(scrollPane, gbc);
          
              gbc.gridy = 2;
              gbc.fill = GridBagConstraints.NONE;
              gbc.weighty = 0.0;
              panel.add(labelDuplicates, gbc);
          
              gbc.gridy = 3;
              gbc.gridwidth = 1;
              gbc.anchor = GridBagConstraints.EAST;
              panel.add(buttonDisplay, gbc);
          
              gbc.gridx = 1;
              gbc.anchor = GridBagConstraints.WEST;
              panel.add(buttonClear, gbc);
          
              JTabbedPane tabbedPane = new JTabbedPane();
              tabbedPane.add(panel);
          
              JFrame frame = new JFrame();
              frame.add(tabbedPane);
              frame.pack();
              frame.setLocationRelativeTo(null);
              frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          
              frame.setVisible(true);
            }
          }
          db
          • 2. Re: gridbag layout problem on a tabbedpane
            843807
            Dear Darryl,
            Thanks for your reply. What I have posted is just a part of my project. I was just testing, how GridbagLayout would fit in my code. Yes, it is scrappy, because it is just another program which I created for testing. I do need JFrame class and I do a separate JPanel class and I cannot have everything in just on method (check MVC). I was trying different things and that is why the given test code looks scrappy. The reason why I was using just one button is that even that was not moving using GridbagLayout.
            • 3. Re: gridbag layout problem on a tabbedpane
              DarrylBurke
              The reason why I was using just one button is that even that was not moving using GridbagLayout.
              Why should it move?

              db
              • 4. Re: gridbag layout problem on a tabbedpane
                843807
                Move as in place it at a different positions on the Pane. No matter what I try to do it always shows up on the center. Also, I want to have different objects (like text boxes, labels, etc) at different positions and that is the reason why I am using Gridbag Layout for more flexibility.
                • 5. Re: gridbag layout problem on a tabbedpane
                  DarrylBurke
                  Looks to me like you don't understand GridBagLayout and have unrealistic expectations. Recommended reading: [http://java.sun.com/docs/books/tutorial/uiswing/layout/gridbag.html]

                  db
                  • 6. Re: gridbag layout problem on a tabbedpane
                    843807
                    Thanks Darryl :) It seems that there is no problem with the way I am using the GridbagLayout in my panel, but the problem is that I cannot shift my axis from center to the top left of the panel. There is a video on the youtube: [http://www.youtube.com/watch?v=FB_wJpIdA8k] which tries to show how we can change the axis, but, it is hard to fit it in, into my module.