0

I have added my button to my Container which can hold components, everything works fine however, the clearButton.setLocation() method below is not working. any ideas?

public JButton clearButton(){ // clearButton method
    JButton clearButton = new JButton("CLEAR"); // new button of type JButton
    clearButton.setPreferredSize(new Dimension(200,30)); // dimension of the button
    clearButton.setLocation(400,450);
    return clearButton; // return reference to the JButton object
}
Ousmane D.
  • 54,915
  • 8
  • 91
  • 126
  • 4
    **Warning** don't follow the advice in the first answer. It will lead to *many problems.* Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). – Andrew Thompson Nov 11 '16 at 14:45
  • What is the ultimate purpose of pursuing this strategy? – Andrew Thompson Nov 11 '16 at 14:46
  • 1
    cheers and yes i have kept that in mind all along. just wanted to say thanks to him since he made the effort to answer my question. Thank you also! – Ousmane D. Nov 11 '16 at 14:49
  • 1
    using a null layout manager is never the best course of action, if you want a button to be located somewhere specific it can be achieved by nesting managers possibly, – Chains Nov 11 '16 at 14:49
  • Provide ASCII art or a simple drawing of the *intended* layout of the GUI at minimum size, and if resizable, with more width and height. – Andrew Thompson Nov 11 '16 at 15:00

1 Answers1

-1

You have to set a absolute layout to the JPanel where you are adding the button to. Then it should work.

kenorb
  • 155,785
  • 88
  • 678
  • 743
XtremeBaumer
  • 6,275
  • 3
  • 19
  • 65
  • 2
    This answer 'solves' one problem, while creating 10 more. Or.. one step forward, ten steps back. – Andrew Thompson Nov 11 '16 at 14:46
  • Yet it is what is requested by @Anonymous. You have a better answer while still having this little effort to achieve it? – XtremeBaumer Nov 11 '16 at 14:53
  • 1
    *"Yet it is what is requested by @Anonymous"* See [Is “Don't do it” a valid answer?](http://meta.stackexchange.com/questions/8891/is-dont-do-it-a-valid-answer) (Yes, so long as you provide a better alternative.) *"You have a better answer"* Probably, but I'm still trying to nail down the detail of exactly what is required. *"..while still having this little effort to achieve it?"* The 'easiest path' is, in programming at least, rarely either a robust or good solution. It is the latter we should be aiming at - set the bar high. This answer drops the bar then trammels it into the dirt. – Andrew Thompson Nov 11 '16 at 15:00
  • if you want a high bar, use JGoodies FormLayout, but since we don't know how the program should look and are left with this little detail, i think that would be a overkill – XtremeBaumer Nov 11 '16 at 15:06
  • 1
    (1-) `Yet it is what is requested ` - because the OP doesn't know any better. Swing was designed to be used by layout manager for too many reasons to list. This is also no need to use the FormLayout. The OP can used the layout managers provided in the JDK. Start by reading the section from the Swing tutorial on [Using Layout Manager](http://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html). – camickr Nov 11 '16 at 15:09