Gridbag layout resize problem

I have some JLabel, as statusBar, when user diminishes the width of jframe the text of the jlabel is overlapped to the text of other jlabel, therefore the text becomes not leggibile
I would want that when the user diminishes the width of the frame, the labels disappear one to the time
I have this code:
import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
public class StatusBar extends JPanel {
     private JLabel userJLabel;
     private JLabel groupJLabel;
     private JLabel serverJLabel;
     private JLabel statusJLabel;
     private JLabel versionJLabel;
     public StatusBar() {
          userJLabel = new JLabel("TEXT ON LABEL ONE");     
          groupJLabel = new JLabel("TEXT ON LABEL TWO");
          serverJLabel = new JLabel("LABEL THREE");     
          statusJLabel = new JLabel("LABEL FOUR");
          versionJLabel = new JLabel("LABEL FIVE");
          //create border
          BevelBorder loweredBevelBorder1 = (BevelBorder) BorderFactory.createLoweredBevelBorder();
          //set border
          userJLabel.setBorder(loweredBevelBorder1);          
          groupJLabel.setBorder(loweredBevelBorder1);          
          serverJLabel.setBorder(loweredBevelBorder1);          
          statusJLabel.setBorder(loweredBevelBorder1);          
          versionJLabel.setBorder(loweredBevelBorder1);          
          //set statusBar layout
          this.setLayout(new GridBagLayout());
          GridBagConstraints c = new GridBagConstraints();
          c.weightx = 1;
          c.gridx = 0;
          c.gridy = 0;
          c.fill = GridBagConstraints.HORIZONTAL;          
          this.add(userJLabel, c);
          c.gridx = 1;
          c.gridy = 0;
          c.fill = GridBagConstraints.HORIZONTAL;
          this.add(groupJLabel, c);
          c.gridx = 2;
          c.gridy = 0;
          c.fill = GridBagConstraints.HORIZONTAL;
          this.add(serverJLabel, c);
          //non rimuovere il carattere di spazio altrimenti la statusBar non viene visualizzata correttamente
          //se l'utente diminuisce le dimensioni del JFrame dello studio
          JLabel emptyLabel = new JLabel(" ");     
          //emptyLabel.setPreferredSize(new Dimension(30, 23));     
          emptyLabel.setBorder(loweredBevelBorder1);
          //c.weightx = 1;
          c.gridx = 3;
          c.gridy = 0;
          c.fill = GridBagConstraints.HORIZONTAL;
          this.add(emptyLabel, c);
          //c.weightx = 0;
          c.gridx = 4;
          c.gridy = 0;
          c.fill = GridBagConstraints.HORIZONTAL;
          this.add(statusJLabel, c);
          //c.weightx = 0;
          c.gridx = 5;
          c.gridy = 0;
          c.fill = GridBagConstraints.HORIZONTAL;
          this.add(versionJLabel, c);          
     public static void main (String[] args) {
          JFrame frame = new JFrame("PROVA");
          frame.getContentPane().setLayout(new BorderLayout());
          frame.getContentPane().add(new JTextArea("bla bla bla"), BorderLayout.CENTER);
          frame.getContentPane().add(new StatusBar(), BorderLayout.SOUTH);
          frame.setSize(800, 600);
          frame.setVisible(true);
}

you could tryimport java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
public class StatusBar extends JPanel {
   private JLabel userJLabel;
   private JLabel groupJLabel;
   private JLabel serverJLabel;
   private JLabel statusJLabel;
   private JLabel versionJLabel;
   public StatusBar() {
      super(new FlowLayout(FlowLayout.RIGHT));
      userJLabel = new JLabel("TEXT ON LABEL ONE");
      groupJLabel = new JLabel("TEXT ON LABEL TWO");
      serverJLabel = new JLabel("LABEL THREE");
      statusJLabel = new JLabel("LABEL FOUR");
      versionJLabel = new JLabel("LABEL FIVE");
      //create border
      BevelBorder loweredBevelBorder1 = (BevelBorder) BorderFactory.createLoweredBevelBorder();
      //set border
      userJLabel.setBorder(loweredBevelBorder1);
      groupJLabel.setBorder(loweredBevelBorder1);
      serverJLabel.setBorder(loweredBevelBorder1);
      statusJLabel.setBorder(loweredBevelBorder1);
      versionJLabel.setBorder(loweredBevelBorder1);
      add(userJLabel);
      add(groupJLabel);
      add(serverJLabel);
      add(statusJLabel);
      add(versionJLabel);
   public static void main (String[] args) {
      JFrame frame = new JFrame("PROVA");
      frame.getContentPane().setLayout(new BorderLayout());
      frame.getContentPane().add(new JTextArea("bla bla bla"), BorderLayout.CENTER);
      frame.getContentPane().add(new StatusBar(), BorderLayout.SOUTH);
      frame.setSize(800, 600);
      frame.setVisible(true);
}but this doesn't solve your second question..
you could try FormLayout...
http://www.jgoodies.com/freeware/forms/
its very nice, i think it can do what you want too
asjf

Similar Messages

  • Possible Problem/Bug In GridBag Layout ????

    I've done hundreds of Gridbag layouts in the past 2 years and just noticed this weird little behavior today.....
    Basically what I'm trying to do is arrange panels like this.....
    +--------+--------+--------+
    |        |        |        |
    |   A    |    B   |   C    |
    |        |        |        |
    +--------+----+---+--------+
    |             |            |
    |     D       |      E     |
    |             |            |
    +-------------+------------+The obvious thing to do is to
    o make the gridwidths of
    B, D, E = 2
    A, C = 1
    D and E should each share one unit of
    B's 2 width.
    Tried it in my code with my real UI and it didn't work, so then I tested it
    out using a Gridbag tool where you can set parameters on the fly quickly and
    surely enough it did not work there either.
    The GridBagLayout is refusing to split B into 2 portions for the 2nd row
    to use in an unalligned fashion. I either get
    +--------+--------+--------+
    |        |        |        |
    |   A    |    B   |   C    |
    |        |        |        |
    +--------+----+---+--------+
    |        |                 |
    |     D  |           E     |
    |        |                 |
    +--------+-----------------+  or
    +--------+--------+--------+
    |        |        |        |
    |   A    |    B   |   C    |
    |        |        |        |
    +--------+----+---+--------+
    |                 |        |
    |     D           |  E     |
    |                 |        |
    +--------+--------+--------+ depending on the order in which I add my panels to the layout.
    even though my gridx, gridy and widths and heights are properly set for
    obtaining the result I want.
    Can someone confirm that this is a bug in the GridBagLayout or share the trick for
    getting around this?

    Thanks for the reply - I can set the weights however
    I wish and I never see box D extending even one pixel
    into the A-B boundary.This does what you say...
    import java.awt.*;
    import javax.swing.*;
    public class TestFrame extends JFrame {
      public TestFrame() {
        JPanel panel = new JPanel(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.fill = GridBagConstraints.BOTH;
        gbc.weighty = 0.5; 
        panel.add(new JButton("A"), gbc);
        gbc.gridx = 1;   
        gbc.gridwidth = 2; 
        panel.add(new JButton("B"), gbc);
        gbc.gridx = 3;   
        gbc.gridwidth = 1;
        panel.add(new JButton("C"), gbc);
        gbc.gridx = 0;
        gbc.gridy = 1;
        gbc.gridwidth = 2;
        gbc.weightx = 0.5;
        panel.add(new JButton("D"), gbc);
        gbc.gridx = 2;
        panel.add(new JButton("E"), gbc);
        getContentPane().add(panel);
        setSize(400, 300);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
      public static void main(String[] args) {
        TestFrame frame = new TestFrame();
        frame.setVisible(true);
    }But i'm sure it's not a satisfactory layout because button A does not want to resize horizontally at all. But of course, a mix of grid layouts does the trick:
    import java.awt.*;
    import javax.swing.*;
    public class TestFrame extends JFrame {
      public TestFrame() {
        JPanel panel = new JPanel(new GridLayout(2, 1));
        JPanel top = new JPanel(new GridLayout(1, 3));
        top.add(new JButton("A"));
        top.add(new JButton("B"));
        top.add(new JButton("C"));
        panel.add(top);
        JPanel buttom = new JPanel(new GridLayout(1, 2));
        buttom.add(new JButton("D"));
        buttom.add(new JButton("E"));
        panel.add(buttom);
        getContentPane().add(panel);
        setSize(400, 300);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
      public static void main(String[] args) {
        TestFrame frame = new TestFrame();
        frame.setVisible(true);
    }

  • JPanel Positiong with Gridbag Layout Problem

    Hi,
    I'm desingning an UI in which I made a JFrame and set it Layout as gridBag layout. Now I have added Three JPanel in this JFrame.
    The problem is that All the three JPanels are shown in center While I want to palce them in top left corner.
    I have done this through NetBeans 4.0 in the layout coustomize option I've set them in first in the left top most, second in below of the first and so on but when I run the application The JPanel are shown in the center in this Particular position i.e first at the top in center second below to the fist and so on.
    Kindly solve my this problem.
    Regards,
    Danish Kamran.

    To get better help sooner, post a SSCCE that clearly demonstrates your problem.
    Use code tags to post codes -- [code]CODE[/code] will display asCODEOr click the CODE button and paste your code between the {code} tags that appear.
    db

  • Gridbag layout problem on a tabbedpane

    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);
    }

    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

  • JTextArea & Gridbag layout - max size of control

    Hello everyone:
    I am currnetly using a JTextArea with the Gridbag layout. I want to limit the size of the JTextArea. Found an article when I did a "search" that says the size is actually controlled by the layout manager. Have tried setMaximunSize(), no luck. It appears the layout manager ignores this. The problem I am having is that I can set the size I want, however if the user types a lot of text in to the JTextArea, the control expands and over and "pushes" the controls below it down. How do I keep the layout manager from allowing this to happen?
    Thanks in advance, Bart

    Hello everyone:
    I am currnetly using a JTextArea with the Gridbag
    layout. I want to limit the size of the JTextArea.
    Found an article when I did a "search" that says the
    size is actually controlled by the layout manager.
    Have tried setMaximunSize(), no luck. It appears the
    layout manager ignores this. The problem I am having
    is that I can set the size I want, however if the user
    types a lot of text in to the JTextArea, the control
    expands and over and "pushes" the controls below it
    down. How do I keep the layout manager from allowing
    this to happen?
    Thanks in advance, Bart Do you wish to allow the user to enter as much text as he or she likes? If so, wrap the JTextArea in a JScrollPane and set the preferred size of the JScrollPange to the area you'd like the pane to consume. It'll listen, no doubt. :)

  • [SOLVED] GNOME 3.8 annoying window resize problem

    Hello all fellow early-adopters :-)
    A very annoying quirk I've noticed, moving from GNOME 3.6 to 3.8, is that it no longer remembers, nor retains, the size/positions I set for many of my application windows. For example, my mail client (Evolution) is sized to fill most of my screen (centrally), with Empathy and Skype as long narrow windows on both sides - giving me a nice, broad "communications" overview on that particular desktop (with dev tools etc open on others).
    On 3.6, this layout was retained during my session, as well as next time I started these apps up.
    In GNOME 3.8, not only does it insist that these windows are always started as small little bunched-up windows that I need to resize, but every time a window displays a notification/warning (message in internal yellow bar inside the window - such as loss of internet connection, mail retrieval failure etc) it resizes the windows spontaneously to a stupid, small size that overlays the other windows. This is driving me crazy!
    Where can I learn a bit more about how window sizing / positioning works in GNOME 3.8, or is it finally time to switch to awesome wm? I want to love GNOME 3.8, I really do. It's so slick, but so... unpolished.
    I want to dig in an assist with problems like these, but I need some pointers to some background material first to understand the problem. Is it the window manager? Is it the app?
    ** UPDATE: Doing a full system upgrade, as of May 18, 2013, has resolved this annoying problem. My windows now stay where they belong, and start with the same size they were closed with. GNOME is now pleasant to use again :-)
    Last edited by dawid.loubser (2013-05-21 13:37:25)

    dawid.loubser wrote:Thanks for the suggestion drtebi - I'll give it a try.
    I really like GNOME 3.x though (and would like to understand the windowing behaviour), but if the annoying quirks are insurmountable, I will happily switch.
    Man I love GNOME 3.x. I admire the courage they had to change, basically, everything, and I find myself more productive with my GNOME 3 Arch box than with my good ol' Slackware KDE 4 box. I just hate those bugs - for example I filed a task in their bugtracker for this window resize problem I have with gedit. If it's a love/hate relationship, I think it's marriage ^_^
    With the 3.8 upgrade, deadbeef was having a similar problem with window size/position. I just recompiled it against the latest GTK+3 package upgrade (that came after the 'big upgrade' here on Arch) and it was fixed. But not with gedit
    bwat47 wrote:
    Man I really hope this gets fixed soon, because aside from this one incredibly annoying issue I am loving gnome 3.8.
    I get the feeling gnome badly needs more beta testers, sizable regressions like this in "stable" releases happen way too often sad.
    I get the exact same feeling. Well bugs exist everywhere, there's no denying. But I think it would be wiser to 'alternate' the nature of each major stable release - one focusing on new features and one focusing on fixing bugs. For example if the only new features in GNOME 3.10 were the AppsFolder full implementation and the introduction of gnome-calendar, and the rest of the development cicle being devoted to fix bugs, I'd be more than happy.
    Like Fedora and Ubuntu, the fixed 6-month release cycle colaborates with the bugs. They don't do like Debian or Slackware which are released 'when they are ready'.
    EDIT: fout (yet) another bug. At least with facebook chat (haven't tested with other telepathy plugins) the buddy tray icon appear duplicate. Anybody with the same issue?
    Last edited by lmello (2013-05-02 14:06:06)

  • Gridbag layout continues to confound me...

    I am having problems with the gridx and gridy constraints when trying to use gridbag layout. They seem to have no effect when I use them. The button always appears in the top left of the panel. Other constraints seem to work as expected.
    For example: c2.fill = GridBagConstraints.BOTH; will fill up the entire panel with my button.
    Any advice on what I am doing wrong this time?
    Thanks
    import javax.swing.JPanel;
    import javax.swing.JTextArea;
    import javax.swing.JScrollPane;
    import javax.swing.JFrame;
    import java.util.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.image.*;
    import java.awt.Color;
    import javax.swing.*;
    import javax.swing.border.*;
    import javax.swing.text.*;
    import java.awt.*;
    import javax.swing.*;
    import javax.swing.event.*;
    public class Question
         public JPanel test()
              JPanel testPanel = new JPanel(new GridBagLayout());
              GridBagConstraints c2 = new GridBagConstraints();
              c2.insets = new Insets(5,5,5,5);
              c2.weightx = 1.0;
              c2.weighty = 1.0;
              c2.anchor = c2.NORTHWEST;
              JButton redButton = new JButton("Button");
              c2.gridx = 2;
              c2.gridy = 2;
              //c2.fill = GridBagConstraints.BOTH;//this works as expected
              testPanel.add(redButton,c2);
              return testPanel;
         public Container createContentPane()
              //Create the content-pane-to-be.
              JPanel contentPane = new JPanel(new BorderLayout());
              contentPane.setOpaque(true);
              return contentPane;
         private static void createAndShowGUI()
              //Create and set up the window.
              JFrame frame = new JFrame("question");
              frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
              //Create and set up the content pane.
              Question demo = new Question();
              frame.setContentPane(demo.createContentPane());
              frame.add(demo.test());
              //Display the window.
              frame.setSize(400, 400);
              frame.setVisible(true);
        public static void main(String[] args)
            //Schedule a job for the event-dispatching thread:
            //creating and showing this application's GUI.
            javax.swing.SwingUtilities.invokeLater(new Runnable()
                public void run()
                    createAndShowGUI();
        }//end main
    }//end Question

    GridBagLayout keeps zero width/height grid for non-existant component for the grid.
    You could override this behavior by using GBL's four arrays as shown below.
    However, in order to get the desired layout effect, using other layout manager, e.g. BoxLayout and/or Box, is much easier and flexible as camickr, a GBL hater, suggests.
    Anyway, however, before using a complex API class as GBL, you shoud read the documentation closely.
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    public class Question{
      // you may adjust these values for your taste
      static final int rowHeight    = 100;
      static final int colWidth     = 100;
      static final double rowWeight = 1.0;
      static final double colWeight = 1.0;
      public JPanel test(){
        GridBagLayout gb = new GridBagLayout();
        gb = keepAllRowsAndColumns(gb, 3, 3);
        JPanel testPanel = new JPanel(gb);
        GridBagConstraints c2 = new GridBagConstraints();
        c2.insets = new Insets(5,5,5,5);
        c2.weightx = 1.0;
        c2.weighty = 1.0;
        c2.anchor = c2.NORTHWEST;
        JButton redButton = new JButton("Button");
        c2.gridx = 2;
        c2.gridy = 2;
        // c2.fill = GridBagConstraints.BOTH;//this works as expected
        testPanel.add(redButton,c2);
        return testPanel;
      GridBagLayout keepAllRowsAndColumns(GridBagLayout g, int rn, int cn){
        g.rowHeights = new int[rn];
        g.columnWidths = new int[cn];
        g.rowWeights = new double[rn];
        g.columnWeights = new double[cn];
        for (int i = 0; i < rn; ++i){
          g.rowHeights[i] = rowHeight;
          g.rowWeights[i] = rowWeight;
        for (int i = 0; i < cn; ++i){
          g.columnWidths[i] = colWidth;
          g.columnWeights[i] = colWeight;
        return g;
      private static void createAndShowGUI(){
        JFrame frame = new JFrame("question");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        Question demo = new Question();
        frame.getContentPane().add(demo.test(), BorderLayout.CENTER);
        frame.setSize(400, 400);
        frame.setVisible(true);
      public static void main(String[] args){
        javax.swing.SwingUtilities.invokeLater(new Runnable(){
          public void run(){
            createAndShowGUI();
    }

  • Gridbag Layout and Tabbed Panes

    First off, I'm pretty new to Java, I started it around February as a school course, and it's been a pretty smooth ride. Recently, we started to code GUI, trying out different layouts and whatnot. I'm having a little trouble in this portion as our teacher did not really delve into it very much. My question concerns the GridBag layout within a panel of a tab (sorry if this is unclear, I'm talking about something that looks like this: http://www.codeproject.com/useritems/tabcontrol.asp). I don't know how to use GridBag constraints within a panel. What I used to do with GridBag was simply use some code that looked like (forgive me, this might not be very accurate): AddComponent button1(#, #, #, #) and define what each of those numbers meant later (first would be row, second would be column, third would be width, fourth would be height, for example). But now, with the tabs, each button, label, or any other control would be declared, then I would place a bunch of direct constraints after, like this:
    JButton button1 = new JButton("1");
    constraints.gridx = 0;
    constraints.gridy = 0;
    constraints.gridwidth = 1;
    constraints.gridheight = 1;then add them into the panel like this:
    panel1.add(button1, constraints);However, I have tried making the buttons on top of each other, and other patterns, but it would just place the buttons next to each other within the panel. How do I make it so that the constraints work? Or rather, if they do work, what kinda things might mess it up? I think the problem is that I dunno how to make the constraints specific to that one button.
    Also, how do I make each panel a certain size instead of each panel being as small as possible to contain anything within it.
    I'm sorry if this all sounds very vague, I'd be more than happy to give you any needed information. Also, I'm not asking any of you to do my homework, I just need a little clarification or an example. Just in case, here's the code I'm working on: http://hashasp.mine.nu/paster/?1376. Be aware that there are no comments or anything, it's very bare. Also, the form may be a bit messy because I'm pretty much reusing code that was given to me, just manipulating it in a different way. Thanks a lot in advanced for your time in reading this.

    * GridBag_Demo.java
    import java.awt.*;
    import javax.swing.*;
    public class GridBag_Demo extends JFrame {
        public GridBag_Demo() {
            setTitle("GridBag Demo");
            setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
            setSize(400,300);
            setLocationRelativeTo(null);
            GridBagConstraints gridBagConstraints;
            jTabbedPane1 = new JTabbedPane();
            jPanel1 = new JPanel();
            jButton1 = new JButton();
            jButton2 = new JButton();
            jButton3 = new JButton();
            jPanel1.setLayout(new GridBagLayout());
            jButton1.setText("jButton1");
            gridBagConstraints = new GridBagConstraints();
            gridBagConstraints.insets = new Insets(2,2,2,2);
            jPanel1.add(jButton1, gridBagConstraints);
            jButton2.setText("jButton2");
            gridBagConstraints.gridy = 1;
            jPanel1.add(jButton2, gridBagConstraints);
            jButton3.setText("jButton3");
            gridBagConstraints.gridy = 2;
            jPanel1.add(jButton3, gridBagConstraints);
            jTabbedPane1.addTab("tab1", jPanel1);
            getContentPane().add(jTabbedPane1, BorderLayout.CENTER);
        public static void main(String args[]) {
            new GridBag_Demo().setVisible(true);
        private JButton jButton1;
        private JButton jButton2;
        private JButton jButton3;
        private JPanel jPanel1;
        private JTabbedPane jTabbedPane1;
    }

  • JTextArea & Gridbag layout

    Hello everyone:
    I am currnetly using a JTextArea with the Gridbag layout. I want to limit the size of the JTextArea. Found an article when I did a "search" that says the size is actually controlled by the layout manager. Have tried setMaximunSize(), no luck. It appears the layout manager ignores this. The problem I am having is that I can set the size I want, however if the user types a lot of text in to the JTextArea, the control expands and over and "pushes" the controls below it down. How do I keep the layout manager from allowing this to happen?
    Thanks in advance, Bart

    Have set the rows and columns as you suggested in the original code,and get the size I want initalially. The problem is that the JTextArea will increase past the size set so it "bleeds" downward. I looked in the Java book I have, and it says that the scrollbars for a JTextArea are provided automatically, however they do not appear when the user supplies more lines of text than there are rows. Instead the height of the control is enlarged and this in turn pushes the other controls on the pane down. Is this a mi-print in the book, ie the scroll bars are not automatically provide in a JTextArea?
    Thanks,
    Bart

  • Gridbag Layout In Flex - Looking for canvasgrid

    Hello,
    I found a posting about a layout mechanism call canvasgrid,
    based on the gridbag layout principle. It is documented at
    Mannu's Blog.
    It is a very effective layout, but the download is broken, and so
    If anyone happens to have his open source reference The source
    filename is canvasgrid_v0.3.1.zip, and it was originally located
    at:
    http://tasmania.globat.com/~mannu.info/flex/canvasgrid_v0.3.1.zip
    If anyone happens to be a code squirel, like myself, and has
    a copy of this, that would be great. I will make it a part of the
    flexlib, just because it's so usefull.
    Thaks

    Hi, You can try this:
    package {
    import flash.display.MovieClip;
    import mx.core.SpriteAsset;
    import mx.core.MovieClipAsset;
    public class GameSymbols extends MovieClip {
    [Embed(source='gameSymbols.swf', symbol='Coffee')]
    private var CoffeeClass:Class;
    //CoffeeClass represents the embedded symbol.
    public function GameSymbols() {
    //Flex defines CoffeClass as a reference to a subclass of
    //SpriteAsset class - for single-frame SWF files or
    //MovieClipAsset class - for multiframe SWF files.
    var myCoffee:SpriteAsset = new CoffeeClass() as SpriteAsset;
    //var myCoffee:MovieClipAsset = new CoffeeClass() as
    MovieClipAsset;
    //You can manipulate the embedded Symbol by using the
    methods
    //and properties of the SpriteAsset or MovieClipAsset class.
    myCoffee.setActualSize(50, 50);
    myCoffee.x = 10;
    myCoffee.y = 10;
    myCoffee.alpha = 100;
    myCoffee.visible = true;
    addChild(myCoffee);
    Luk.

  • How to make JLabel Left Justified in Gridbag Layout?

    Hi Govind here I am using Gridbag layout and having Labels and TextBox . i want my All my Labels Left Justified (Currently they are RightJustified),I tried JLabel.Left but not working so any help most welcome............
    JLabel startDateLabel      = new JLabel("STARTDATE ",JLabel.LEFT);
              startDateLabel.setBorder( border );                                   
              //set the position of the label               
              gbc.fill = GridBagConstraints.NONE;
              usingGBC(startDateLabel,gbc,3,0,1,1.0);
                   //text field for start dat
    txtStartDate                = new JTextField();
              txtStartDate.setMinimumSize(minSize);
              txtStartDate.setPreferredSize( longField );
              txtStartDate.setEnabled( true);                    
              txtStartDate.setEditable(false);
              txtStartDate.setBackground(Color.WHITE);          
              gbc.fill = GridBagConstraints.HORIZONTAL;
              usingGBC(txtStartDate,gbc,4,0,2,1.0);
         here usingGBC is method as follows
    protected void usingGBC(Component com,GridBagConstraints gbc,int gx,int gy,int gwidth,double wx)
                   gbc.gridx = gx;
                   gbc.gridy = gy;
                   gbc.gridwidth = gwidth;
                   gbc.weightx = wx;
                   add(com,gbc);

    It seems you set the fill of the GridBagConstraints after you add the label. Either specify HORIZONTAL fill before adding the label or specify alignment for the constraints.
    Mike

  • Gridbag layout in jpanel?

    Hi, all
    I just start to use gridbag layout in my application. It looks very like table in html to me. While writing html table, I can use 'border=1' to determine width and height for each cell. Is it possible to reveal border of each grid in jpanel?
    Thanks,

    You can't reveal the border of the cells being used by GridBagLayout.
    You could add a LineBorder to each component but this won't show you the cell border, it will show you the component border. There could be padding between the component and the cell boundary.
    However, you should try to avoid thinking of GridBagLayout as being like an HTML table; this usually causes confusion. There's probably a number of topics in this forum about wanting to get HTML table behaviour with a GridBagLayout.
    Here's one for a start:
    http://forum.java.sun.com/thread.jspa?forumID=57&threadID=251815

  • Preview Application Resizing Problem

    Preview Application Resizing Problem
    Microsoft platforms have two significant advantages. The first is the concept of an operating system that can function with hardware that ranges from cellular phones to desktop computers. The second is that Microsoft has excellent software development tools
    for creating and testing computer programs.
    Software compatibility across hardware platforms requires not only meeting CPU and memory requirements. Application programs must be able to automatically adjust Window and control sizes to meet the needs of available window sizes, video display unit sizes
    and screen resolutions, switching between vertical and horizontal viewing, vertical and horizontal scrolling, changes in font sizes , and variations in the available screen space due to usage of vertical and horizontal toolbars.
     Software developers must be careful that resizing doesn’t squeeze controls too close together or make them so small that they cannot be finger or stylus selected when used with touch screens.
    Some of the required automatic resizing options are provided by the Microsoft software compilers and the Windows operating system.  Softgroup Component’s “.NET  Forms Resize” is an example of one of the third party applications that provide advanced
    resizing functionality.
    Many of the resizing functions are programmer options and, if not properly enabled, an application may not have the required resizing functionality. At the current state of the art, application programs are highly variable in their capability to do the “intelligent
    resizing” that is required to handle different display environments.
    A case in point is the Microsoft Preview Application for Windows 10. When entering lengthy comments, the send button becomes positioned off the end of the program window. It is possible to scroll to the button, but the display automatically resets when scroll
    is released. The result is that the text cannot be sent. You can try a "blind" TAB to the SEND button. This appeared to work after several tries, but the comment was apparently not received,
    RERThird

    Hi,
    What do you see when you open the same PDF file in Acrobat Reader? Are the words still squeezed than?
    Dimaxum

  • GridBag Layout help

    Hey all,
    I'm currently revamping an application I wrote for my company that I wrote with a GridLayout. It doesn't look bad, but it doesn't look as good as a standard windows application. I'd like to write it using a gridbag layout in order to make it look a little better but no where can I find a good idea of how to even use it... Javadocs on it seem kinda cryptic to me and I'd like a better visual example than just a bunch of buttons (which is what the tutorial gives). Anyone have any ideas where I can look?

    Here is a simple example of some of my code that uses the gridbag layout. (slightly modified so it will run by itself...). I added some comments on what some of the different settings do...
    import java.awt.event.*;
    import java.awt.*;
    import javax.swing.*;
    import java.text.*;
    import javax.swing.event.*;
    import java.util.Properties;
    * trackWindow.java
    * Created October 24, 2002, 6:26 PM
    * This class is used to display a tabbed internal window that is used to
    * display and control the settings required for localizing particles and
    * tracking their movement as part of the Trackguy program.
    public class trackWindow2 extends JInternalFrame implements ActionListener {
         private JTabbedPane trackTabPanel;
         private JPanel maskPanel;
         private JPanel trackPanel;
         private JLabel multLabel;
         private JTextField multiplier;
         private JLabel threshLabel;
         private JTextField threshold;
         private JToggleButton dilate2;
         private JToggleButton partBK;
         private JLabel partSizeLabel;
         private JTextField minPartSize;
         private JLabel scaleLabel;
         private JLabel tIntervalLabel;
         private JLabel xScaleLabel;
         private JTextField xScale;
         private JLabel yScaleLabel;
         private JTextField yScale;
         private JTextField tInterval;
         private JLabel timeUnitLabel;
         private JLabel maxDifLabel;
         private JTextField maxDif;
         private JLabel minTrackLabel;
         private JTextField minTrack;
         private JLabel difCoefLabel;
         private JTextField difCoef;
         public trackWindow2 () {
                initFields();
                collectPanels();
                buildWindow();
                setIcon();
                show();
         private void initFields() {
                multLabel = new JLabel("Multiplier: ");
                multiplier = new JTextField("10");
                multiplier.setActionCommand("multiplier");
                multiplier.setPreferredSize(new Dimension(40, 18));
                multiplier.addActionListener(this);
                threshLabel = new JLabel("Threshold: ");
                threshold = new JTextField("32770");
                threshold.setActionCommand("threshold");
                threshold.setPreferredSize(new Dimension(40, 18));
                threshold.addActionListener(this);
                dilate2 = new JToggleButton("Dilate 2 X");
                dilate2.addActionListener(this);
                partBK = new JToggleButton("Particle BK");
                partBK.addActionListener(this);
                partSizeLabel = new JLabel("Min Particle Size: ");
                minPartSize = new JTextField("9");
                minPartSize.setActionCommand("part. size");
                minPartSize.setPreferredSize(new Dimension(40, 18));
                minPartSize.addActionListener(this);
                scaleLabel = new JLabel("Scale");
                tIntervalLabel = new JLabel("Time Interval");
                xScaleLabel = new JLabel("x = ");
                xScale = new JTextField("1.08e-007");
                xScale.setActionCommand("xScale");
                xScale.setPreferredSize(new Dimension(60, 18));
                xScale.addActionListener(this);
                yScaleLabel = new JLabel("y = ");
                yScale = new JTextField("1.08e-007");
                yScale.setActionCommand("yScale");
                yScale.setPreferredSize(new Dimension(60, 18));
                yScale.addActionListener(this);
                tInterval = new JTextField("1.5");
                tInterval.setHorizontalAlignment(JTextField.CENTER);
                tInterval.setActionCommand("tInterval");
                tInterval.setPreferredSize(new Dimension(40, 18));
                tInterval.addActionListener(this);
                timeUnitLabel = new JLabel("Sec.");
                timeUnitLabel.setHorizontalAlignment(SwingConstants.LEFT);
                maxDifLabel = new JLabel("Max. Difference: ");
                maxDif = new JTextField("50");
                maxDif.setActionCommand("difference");
                maxDif.setPreferredSize(new Dimension(40, 18));
                maxDif.addActionListener(this);
                minTrackLabel = new JLabel("Min. Track: ");
                minTrack = new JTextField("20");
                minTrack.setActionCommand("minTrack");
                minTrack.setPreferredSize(new Dimension(40, 18));
                minTrack.addActionListener(this);
                difCoefLabel = new JLabel("Diff. Coefficient:");
                difCoef = new JTextField("6.00e-14");
                difCoef.setActionCommand("coefficient");
                difCoef.setPreferredSize(new Dimension(60, 18));
                difCoef.addActionListener(this);
         private void collectPanels() {
                GridBagConstraints gridBagConstraints;
                maskPanel = new JPanel();
              maskPanel.setLayout(new GridBagLayout());
                gridBagConstraints = new GridBagConstraints();
                //1st Column
                   gridBagConstraints.gridx = 0;
                //1st Row
                   gridBagConstraints.gridy = 0;
                //Stuck to the Left of the Column
                   gridBagConstraints.anchor = GridBagConstraints.WEST;
                maskPanel.add(multLabel, gridBagConstraints);
                gridBagConstraints = new GridBagConstraints();
                 gridBagConstraints.gridx = 1;
                     gridBagConstraints.gridy = 0;
                   gridBagConstraints.anchor = GridBagConstraints.WEST;
                maskPanel.add(multiplier, gridBagConstraints);
                gridBagConstraints = new GridBagConstraints();
                   gridBagConstraints.gridx = 0;
                   gridBagConstraints.gridy = 1;
                   gridBagConstraints.anchor = GridBagConstraints.WEST;
                maskPanel.add(threshLabel, gridBagConstraints);
                gridBagConstraints = new GridBagConstraints();
                   gridBagConstraints.gridx = 1;
                   gridBagConstraints.gridy = 1;
                   gridBagConstraints.anchor = GridBagConstraints.WEST;
                maskPanel.add(threshold, gridBagConstraints);
                gridBagConstraints = new GridBagConstraints();
                   gridBagConstraints.gridx = 0;
                   gridBagConstraints.gridy = 2;
                maskPanel.add(dilate2, gridBagConstraints);
                gridBagConstraints = new GridBagConstraints();
                   gridBagConstraints.gridx = 1;
                   gridBagConstraints.gridy = 2;
                maskPanel.add(partBK, gridBagConstraints);
                gridBagConstraints = new GridBagConstraints();
                   gridBagConstraints.gridx = 0;
                   gridBagConstraints.gridy = 3;
                   gridBagConstraints.anchor = GridBagConstraints.WEST;
                maskPanel.add(partSizeLabel, gridBagConstraints);
                gridBagConstraints = new GridBagConstraints();
                   gridBagConstraints.gridx = 1;
                   gridBagConstraints.gridy = 3;
                 gridBagConstraints.anchor = GridBagConstraints.WEST;
                maskPanel.add(minPartSize, gridBagConstraints);
                trackPanel = new JPanel();
                trackPanel.setLayout(new GridBagLayout());
                gridBagConstraints = new GridBagConstraints();
                //1st Column
                   gridBagConstraints.gridx = 0;
                //1st Row
                   gridBagConstraints.gridy = 0;
                //2 Columns Wide
                   gridBagConstraints.gridwidth = 2;
                //40 empty pixels to Left of the Component
                   gridBagConstraints.insets = new Insets(0, 40, 0, 0);
                trackPanel.add(scaleLabel, gridBagConstraints);
                gridBagConstraints = new GridBagConstraints();
                //3rd Column
                   gridBagConstraints.gridx = 2;
                //1st Row
                   gridBagConstraints.gridy = 0;
                //2 Columns wide
                   gridBagConstraints.gridwidth = 2;
                //5 empty pixels to the left of the component
                   gridBagConstraints.insets = new Insets(0, 5, 0, 0);
                trackPanel.add(tIntervalLabel, gridBagConstraints);
                  gridBagConstraints = new GridBagConstraints();
                //1st Column
                   gridBagConstraints.gridx = 0;
                //2nd Row
                   gridBagConstraints.gridy = 1;
                //Stuck to the Right of the Column
                   gridBagConstraints.anchor = GridBagConstraints.EAST;
                trackPanel.add(xScaleLabel, gridBagConstraints);
                gridBagConstraints = new GridBagConstraints();
                //2nd Column
                   gridBagConstraints.gridx = 1;
                //2nd Row
                   gridBagConstraints.gridy = 1;
                //Stuck to the Left of the Column
                   gridBagConstraints.anchor = GridBagConstraints.WEST;
                trackPanel.add(xScale, gridBagConstraints);
                gridBagConstraints = new GridBagConstraints();
                   gridBagConstraints.gridx = 0;
                   gridBagConstraints.gridy = 2;
                   gridBagConstraints.anchor = GridBagConstraints.EAST;
                trackPanel.add(yScaleLabel, gridBagConstraints);
                gridBagConstraints = new GridBagConstraints();
                   gridBagConstraints.gridx = 1;
                   gridBagConstraints.gridy = 2;
                trackPanel.add(yScale, gridBagConstraints);
                gridBagConstraints = new GridBagConstraints();
                   gridBagConstraints.gridx = 2;
                   gridBagConstraints.insets = new Insets(0, 10, 0, 0);
                trackPanel.add(tInterval, gridBagConstraints);
                gridBagConstraints = new GridBagConstraints();
                   gridBagConstraints.gridx = 2;
                   gridBagConstraints.insets = new Insets(0, 10, 0, 0);
                trackPanel.add(tInterval, gridBagConstraints);
                gridBagConstraints = new GridBagConstraints();
                   gridBagConstraints.gridx = 3;
                   gridBagConstraints.gridy = 1;
                   gridBagConstraints.anchor = GridBagConstraints.WEST;
                trackPanel.add(timeUnitLabel, gridBagConstraints);
                gridBagConstraints = new GridBagConstraints();
                //1st Column
                   gridBagConstraints.gridx = 0;
                //4th Row
                   gridBagConstraints.gridy = 3;
                //10 empty pixels on top of the Component
                   gridBagConstraints.insets = new Insets(10, 0, 0, 0);
                trackPanel.add(maxDifLabel, gridBagConstraints);
                gridBagConstraints = new GridBagConstraints();
                   gridBagConstraints.gridx = 1;
                   gridBagConstraints.gridy = 3;
                   gridBagConstraints.insets = new Insets(10, 0, 0, 0);
                trackPanel.add(maxDif, gridBagConstraints);
                gridBagConstraints = new GridBagConstraints();
                   gridBagConstraints.gridx = 0;
                   gridBagConstraints.gridy = 4;
                   gridBagConstraints.anchor = GridBagConstraints.EAST;
                trackPanel.add(minTrackLabel, gridBagConstraints);
                gridBagConstraints = new GridBagConstraints();
                   gridBagConstraints.gridx = 1;
                   gridBagConstraints.gridy = 4;
                trackPanel.add(minTrack, gridBagConstraints);
                gridBagConstraints = new GridBagConstraints();
                   gridBagConstraints.gridx = 2;
                   gridBagConstraints.gridy = 3;
                   gridBagConstraints.gridwidth = 2;
                   gridBagConstraints.insets = new Insets(0, 5, 0, 0);
                trackPanel.add(difCoefLabel, gridBagConstraints);
                gridBagConstraints = new GridBagConstraints();
                   gridBagConstraints.gridy = 4;
                   gridBagConstraints.gridwidth = 2;
                   gridBagConstraints.insets = new Insets(0, 0, 5, 0);
                trackPanel.add(difCoef, gridBagConstraints);
                trackTabPanel = new JTabbedPane();
                trackTabPanel.addTab("Masking", maskPanel);
                trackTabPanel.addTab("Tracking", trackPanel);
         private void buildWindow() {
                Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
                setResizable(true);
                setTitle("Tracking");
                setVisible(true);
                getContentPane().add(trackTabPanel, BorderLayout.CENTER);
                setBounds(10, 10, 280, 180);
                addComponentListener(new ComponentAdapter () {
                                              public void componentMoved(ComponentEvent evt) {
                                                   Component c = (Component)evt.getSource();
                                                   c.repaint();
         private void setIcon() {
         public void actionPerformed(ActionEvent evt) {
         public void savePreferences(Properties prefs) {
                prefs.put("multiplier", multiplier.getText());
                prefs.put("threshold", threshold.getText());
                prefs.put("dilate2x", (dilate2.isSelected())?"true":"false");
                prefs.put("particleBK", (partBK.isSelected())?"true":"false");
                prefs.put("minPartSize", minPartSize.getText());
                prefs.put("xScale", xScale.getText());
                prefs.put("yScale", yScale.getText());
                prefs.put("tInterval", tInterval.getText());
                prefs.put("maxDif", maxDif.getText());
                prefs.put("minTrack", minTrack.getText());
                prefs.put("difCoef", difCoef.getText());
         public void setScale (double value,String unit) {
         public static void main(String[] args) {
              JFrame mFrame = new JFrame();
              JDesktopPane jdp = new JDesktopPane();
              trackWindow2 tw = new trackWindow2();
              jdp.add(tw);
              mFrame.setContentPane(jdp);
              mFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
              mFrame.setSize(new Dimension(300,250));
              mFrame.setVisible(true);
    }

  • Illustrator Resizing Problems

    I am having reduction resizing problems in Illustrator CS6.  E.G. Straws & lemon wedge rinds become significantly proportionately wider when reduced.   Also, numerical text does not skew with everything else.  I have a CC membership w/PS & Ai installed.  There are no other Adobe items on my 8 month old imac, Lion 10.7.4 except Flash player & the Adobe Applications Manager.  Additionally, I have no 3rd party plug-ins & deleting the prefs file does not fix the problem.  Any help on this matter would be greatly appreciated. 

    sgem,
    And untick Align to Pixel Grid if ticked (you can do it in the Transform panel, or search for it in the Helpfile).
    Hi Steve. Still summer?

Maybe you are looking for

  • Needed: flash for dummies - a template for HTML snippet

    I am a dummy when in comes to code. I understand that iWeb '08 uses "HML Snippets" and by using them, you can add flash movies (.flv or .swf) to your iWeb '08 created site. What I (and apparently many other dummies) need is a step by step set of inst

  • Resizing the JFrame equal Screen size

    Dear Friends, I have a problem in resizing the JFrame. I am using the standard bounds for JFrame setBounds(0,0,800,600); If the Screen size 1024*728 then JFrame will be visible a the corner of the screen. I would like to resize by JFrame screen dynam

  • So slow iphone 3g

    Have an iphone 3G approx 15 months old Since downloading software 4.0.1 my phone is so so slow! There is always a delay in anything I do with it! Anyone help??

  • Is it possibile to retrieve .nav from pdf portfolio?

    Hi all! There is any way to retrieve the .nav file from pdf portfolio? Is it possible? Thanks

  • Loading errors!

    My Int data load for a BW stat cube got failed with error 'caller 70' is missing. This is a production issues. I checked my ST22 and got the following details; Runtime Errors DBIF_RSQL_INTERNAL_ERROR Internal error when accessing a table. DBIF_RSQL_I