0

for some reason my insets aren't resetting after I set the values back to 0. My intention is to move one button, for example, to the left 200 pixels, an then reset that straight after so that any other components are not moved 200 pixels. The problem being that even though I redefine my insets as being 0,0,0,0 both components are being moved. Thank's for any answers!

Here's the code that I use to initiate the JPanel that has the problem :

import java.awt.BorderLayout;
import java.io.IOException;

import javax.swing.JComponent;
import javax.swing.JFrame;


public class MainFrame {
         public static void main(String[] args) throws IOException {

            MainFrame gui = new MainFrame();
            gui.Frame();
        } 

     public JFrame Frame() throws IOException {

         JFrame frame = new JFrame(""); 

         PlayGamePanel passme = new PlayGamePanel();
         JComponent panel2 = passme.GamePanel2(frame);

         frame.add(panel2);
         frame.getContentPane().add(BorderLayout.CENTER, panel2 );
         frame.setSize(860,500);
         frame.setLocationRelativeTo(null);
         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         frame.setResizable(false);
         frame.setVisible(true);
         frame.getContentPane().add(panel2, BorderLayout.CENTER);

         return frame;
     }

}

Here's my code that has the inset problem :

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.image.BufferedImage;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JLabel;

@SuppressWarnings("serial")
public class PlayGamePanel extends JComponent{

    JComponent GamePanel2() throws IOException {

        JComponent GamePanel = new JLabel();

        //Setting up the GridBagLayout
        GamePanel.setLayout(new GridBagLayout());
        GridBagConstraints gbLayout = new GridBagConstraints();

        //Creating the buttons + setting up the GridBagConstraints
        gbLayout.insets = new Insets(300, 350, 0, 450);
        JButton AnswerOneButton = new JButton("")
        GamePanel.add(AnswerOneButton, gbLayout);

        gbLayout.insets = new Insets(0, 0, 0, 0);
        JButton AnswerTwoButton = new JButton("1");
        GamePanel.add(AnswerTwoButton, gbLayout);
bgigurtsis
  • 145
  • 1
  • 2
  • 15
  • 3
    rest is important not code posted here, for better help sooner post an [MCVE](http://stackoverflow.com/help/mcve), short runnable, compilable, not something without end, start or both, whats FileIO and ImageIO has something to do with your question about Insets in GBC – mKorbel Jan 10 '14 at 07:36
  • @mKorbel Removed the bad imports and added the required code to run the whole thing. – bgigurtsis Jan 10 '14 at 07:50
  • @alex2410 here are two worlds one of them is Andrew Thompson's SSCCE and second is the [community here StackOverflow](http://meta.stackexchange.com/questions/214955/can-we-create-a-help-center-topic-that-outlines-what-a-sscce-mwe-means-for-sta) – mKorbel Jan 10 '14 at 07:56
  • 3
    [see excelent guide by @splungebob](http://stackoverflow.com/a/21030105/714968) – mKorbel Jan 10 '14 at 07:59
  • Crossposted: http://www.coderanch.com/t/626704/GUI/java/stop-insets-changing – camickr Jan 10 '14 at 16:35

1 Answers1

3

Actually, there is just one configuration of Insets for one Layout at one moment.
By doing:

gbLayout.insets = new Insets(300, 350, 0, 450);
// Some code...
gbLayout.insets = new Insets(0, 0, 0, 0);

it just eventually sets gbLayout.insets = new Insets(0, 0, 0, 0) and gets rid of its previous value.

You'll have to play around with a more complex layout organization to obtain what you described!

For example, you could try to use a GridBagLayout as follow, if you want you buttonIcon2 to have some space on its left:

+------------- GridBagLayout -------------+
|                                         |
|               buttonIcon                |
|                                         |
+--------+--------------------------------+
|        |                                |
|  glue  |          buttonIcon2           |
|        |                                |
+--------+--------------------------------+

You can read more about how to use the GridBagLayout here.

ccjmne
  • 9,333
  • 3
  • 47
  • 62