1

I am extending the JButton function so as to automate a couple of things, so I have only added this function:

public void addButton(Container container, int left, int top, int width, int height)
{

    this.setBounds(left, top, width, height);
    this.setSize(width, height);
    container.add(this);
    this.repaint();
}

and I am running this code in my pannel:

buttonLeft = new extendedButton("Left");
    buttonMiddle = new extendedButton("Middle");
    buttonRight = new extendedButton("Right");

    Insets inset = this.getInsets();

    setLayout(null);

    Container c = getContentPane();

    buttonLeft.addButton(c, inset.left, inset.top, 150, 50);
    buttonMiddle.addButton(c, inset.left + buttonLeft.WIDTH, inset.top, 150, 50);
    buttonRight.addButton(c, inset.left + buttonLeft.WIDTH + buttonRight.WIDTH, inset.top, 150, 50);

however, though I expect the buttons to appear one next to the other, they seem to appear one on top of the other and one pixel to the right. If however I replace the .WIDTH parameter with 150, it works as expected, any clues as to why .WIDTH denies to give the value I have set? I am not using a layout, I gave it null as a parameter.

mKorbel
  • 109,525
  • 20
  • 134
  • 319
user3079666
  • 1,159
  • 10
  • 23
  • Avoid `null` layout. Handover the responsibility of component size and position to the layout manager that's why it is made for. – Braj May 25 '14 at 15:12
  • 2
    The constant `WIDTH` does **NOT** correspond to the width of the button. Use `button.getWidth()` instead. But ... `null`-Layout is a bad idea nevertheless. – Marco13 May 25 '14 at 15:35
  • I am used to using coordinates from XNA and Win32, and I have my reasons, don't worry about that... ok, thanks for the getWidth() – user3079666 May 25 '14 at 15:54
  • @Macro13, can you post that as an answer so I can mark the question as answered? – user3079666 May 25 '14 at 15:57
  • I am probably getting annoying, but though it worked for the middle button, the right one doesn't appear whatsoever, however just multiplying with two the middle one is fine, you could say it's multiplying with the number of buttons to the left, so I'm good, thanks again – user3079666 May 25 '14 at 16:01

0 Answers0