1

Every time I create a JButton with an image constructed to it, it sets the JButton's size to a size slightly larger than the Image's size. I tried to use button.setBounds(x,y,width,height), and then repainted the button after this, but it did not do anything. I want to set the JButton to the exact size of the image, not slightly larger than it, and I also want to move the JButton down to the bottom of the screen. Both of these are attainable through setBounds but it does not do anything on a pre-constructed JButton with an image. What's your suggestion? Thank you for your time.

mKorbel
  • 109,525
  • 20
  • 134
  • 319
keyert
  • 255
  • 1
  • 4
  • 12
  • never-ever call setBounds on _any_ components (except the top-level window): locating/sizing is the exclusive task of the LayoutManager. So learn how to use them, f.i. from the layout chapter in the online tutorial referenced in the Swing tag description :-) – kleopatra Apr 18 '12 at 09:00

3 Answers3

3

How do I set the size of a JButton that already has a background image?

Don't do that. Instead just call setBorderPainted(false) & setContentAreaFilled(false) as shown in this example.

If the image itself is the 'wrong size' I can think of 3 alternatives. Let me know if that is the case.

Community
  • 1
  • 1
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
2

Try:

setMargin(new Insets(0,0,0,0));
Ben Taitelbaum
  • 7,343
  • 3
  • 25
  • 45
  • It still won't move the JButton though, the insets worked perfectly to trim down the extra frame space, but setBounds and setLocation still both do not move the JButton, it just remains in the same place every time. – keyert Apr 18 '12 at 22:10
  • 1
    @keyert The location of the button will depend on the layout manager of its parent container. The easiest to work with is probably a GridLayout, but for the most control I'd recommend using a SpringLayout. – Ben Taitelbaum Apr 18 '12 at 23:15
  • Thank you for your responses. Even if I am going to have a largely customized layout that will consist of a wide arrangement of JComponent positions, should I still use a standard Layout preset or would it better to use the null Layout? Thank you. – keyert Apr 19 '12 at 00:05
  • 1
    With a large, complex layout, I would recommend breaking it down into smaller containers, each of which can manage their own layouts. Draw it on paper first to figure out what layout makes sense. A fairly intuitive layout scheme is to use Box layouts, where you can nest horizontal boxes inside vertical boxes (as well as the other way around). Again, with SpringLayout you can be very explicit about where the layout should stretch and where it should be constrained as the window changes sizes. – Ben Taitelbaum Apr 19 '12 at 00:47
  • How do I easily set 3 buttons next to each other on the bottom of the screen and a rectangle in the top left with SpringLayout? I've looked through tons of examples but they all show crazy amounts of constraints and variables that seem a bit unnecessary. I feel like there should be a relatively quick way to do it, no? – keyert Apr 19 '12 at 02:54
  • 1
    It might be easier with something like a GridBagLayout (although I never got a good feel for that one). With a SpringLayout, you'd tie the top of the rectangle to the top of the window and the left of the rectangle to the left of the window. I'd put the buttons into a FlowLayout (or a Box with an X_AXIS layout) and then attach that button panel to the bottom of the window. – Ben Taitelbaum Apr 19 '12 at 02:59
  • How do I attach a panel with a layout on it to a specific part of the screen? – keyert Apr 20 '12 at 01:11
  • 1
    That might be a good candidate for a separate question, but if you're trying to place a panel inside a container, then it's all about the container's layout manager (some containers let you insert rigid space for spacing, for example). If it's about where to put the window on the screen, then you want to use something like http://www.java-forums.org/awt-swing/3491-jframe-center-screen.html – Ben Taitelbaum Apr 20 '12 at 14:02
-1

Try calling (the container for the JButton).Invalidate() after setting the bounds

ghostbust555
  • 2,040
  • 16
  • 29
  • Nah, just tried that, sorted through all the nullpointerexceptions it caused, and it didn't change anything. Was a good try though. – keyert Apr 18 '12 at 02:09