1

I am writing a GUI for my program using Java Swing. I wrote a class to extend my JFrame using the following link. My problem is that my buttons are not centered on the background at all. That is, something like this:

--------------------------------------
|                      |             |
|                      |             |
|           my picture |             |
--------------------------------------
|          |           |             |
|          | Label     |             |
|          | text      |             |
|          | button    |             |
--------------------------------------

I have included some pseudocode below:

    public class demo extends JFrame implements ActionListener
    {
        class demo()
        {

              CustomJFrame frame = new CustomJFrame();
              JPanel pane = new JPanel(new GridBagLayout());

              GridBagConstraints c = new GridBagConstraints();
              JTextField dummyText = new JTextField("I'm some text");
              JLabel dummyLabel = new JLabel("I'm a label);

              JButton dummyButton = new JButton("I'm a button");

              c.fill = GridBagConstraints.BOTH;
              c.gridx = 0;
              c.gridy = 0;
              pane.add(dummyText, c);

              c.fill = GridBagConstraints.BOTH;
              c.gridx = 1; 
              c.gridy = 0;
              pane.add(dummyLabel, c);

              c.fill = GridBagConstraints.BOTH;
              c.gridx = 0;
              c.gridy = 2; 
              pane.add(dummyButton, c);

              frame.add(pane);

        }

    }

Here is my custom JFrame class based on the link.

public class CustomJFrame extends JFrame
{
     private ImageIcon background = new ImageIcon("mypicture.png");
     private JLabel label = new JLabel(background);

     public CustomJFrame()
     {
        this.SetDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setTitle("Traffic Alert Demo");
        setSize(800,480); //specific to my picture though
        add(label, BorderLayout.CENTER);
        setVisible(true);
      }
}

So I want some way to be able to have the buttons be centered on my picture or at least the ability for me to move the buttons around anywhere I please on the picture. I have found nothing to help me thus far, does any ideas? Or is my picture, since I made it a label, interacting badly with the GridBag? Please help.

Regards, Geeky

Community
  • 1
  • 1
GeekyOmega
  • 1,235
  • 6
  • 16
  • 34
  • There is more to the story here, when I minimize the screen size to just the picture, my buttons/label pane goes BEHIND the picture. So maybe the real question is how to draw the buttons/label pane in front of the picture? – GeekyOmega Jul 05 '13 at 18:37
  • 1
    The `CustomJFrame` class will not compile, I doubt whether you tested the code before posting. – Extreme Coders Jul 05 '13 at 18:56
  • It is pseudo code, but the one line I added compiles for me. Anyway, I think what I have to do is create a glass panel? See: http://docs.oracle.com/javase/tutorial/uiswing/components/rootpane.html – GeekyOmega Jul 05 '13 at 19:07

1 Answers1

2

I was able to solve this problem by treating my pane as glass object. Thus, we have the following pseudo code:

public class demo extends JFrame implements ActionListener
{
    class demo()
    {

          CustomJFrame frame = new CustomJFrame();
          JPanel pane = new JPanel(new GridBagLayout());

          GridBagConstraints c = new GridBagConstraints();
          JTextField dummyText = new JTextField("I'm some text");
          JLabel dummyLabel = new JLabel("I'm a label);

          JButton dummyButton = new JButton("I'm a button");

          c.fill = GridBagConstraints.BOTH;
          c.gridx = 0;
          c.gridy = 0;
          pane.add(dummyText, c);

          c.fill = GridBagConstraints.BOTH;
          c.gridx = 1; 
          c.gridy = 0;
          pane.add(dummyLabel, c);

          c.fill = GridBagConstraints.BOTH;
          c.gridx = 0;
          c.gridy = 2; 
          pane.add(dummyButton, c);

          pane.setOpaque(false);
          frame.setGlassPane(pane);
          pane.setVisible(true);

    }

}

The key is the following idea. If you want a background with buttons and things on the front of it. Tell JFrame that is is a glass pane. You then set the buttons and objects associated with this pane and opaque and visible. That's all there is to it.

GeekyOmega
  • 1,235
  • 6
  • 16
  • 34
  • 1
    Hopefully this [answer](http://stackoverflow.com/a/11428289/1057230) and this [answer](http://stackoverflow.com/a/11372350/1057230), might can help you too, in some way :-) – nIcE cOw Jul 06 '13 at 04:45