3

I have a program which takes two buttons, one that is regular and one that has a picture that changes depending on mouse roll over. Currently, since the picture is large, JButton custom is very large as well, can I change the size of custom and keep the image (and roll over image) proportionate? I have tried setSize, and it does't do anything. Any feedback would be appreciated!

 custom.setSize(50, 50);

Here is all of my code:

Main class:

package Buttons;

import javax.swing.JFrame;

public class Main_buttons{

public static void main(String[] args) {

    ButtonClass press = new ButtonClass();
    press.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    press.setSize(500,500);
    press.setVisible(true);
    }
}

Other class:

package Buttons;

import java.awt.FlowLayout; //layout proper
import java.awt.event.ActionListener; //Waits for users action
import java.awt.event.ActionEvent; //Users action
import javax.swing.JFrame; //Window
import javax.swing.JButton; //BUTTON!!!
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane; //Standard dialogue box

public class ButtonClass extends JFrame {

private JButton regular;
private JButton custom;

public ButtonClass() { // Constructor
    super("The title"); // Title
    setLayout(new FlowLayout()); // Default layout

    regular = new JButton("Regular Button");
    add(regular);

    Icon b = new ImageIcon(getClass().getResource("img.png"));
    Icon x = new ImageIcon(getClass().getResource("swag.png"));
    custom = new JButton("Custom", b);
    custom.setRolloverIcon(x); //When you roll over the button that says custom the image will change from b to x
    custom.setSize(50, 50);
    add(custom);

    Handlerclass handler = new Handlerclass();
    regular.addActionListener(handler);
    custom.addActionListener(handler);


}

public class Handlerclass implements ActionListener { // This class is inside the other  class


    public void actionPerformed(ActionEvent eventvar) { // This will happen
                                                        // when button is
                                                        // clicked
        JOptionPane.showMessageDialog(null, String.format("%s", eventvar.getActionCommand()));//Opens a new window with the name of the button
    }
}





}
  • Yes, I had looked at that post already, but for some reason, setPreferredSize() doesn't work either...? Any idea why? –  Aug 18 '15 at 23:56
  • @Billybobsteven *"Any idea why?"* - Not without a code example which demonstrates it not working...Also, do you really want a button that's 40, 000 pixels high? Not sure I know many screens that get that big... – MadProgrammer Aug 18 '15 at 23:57
  • This button stays the same size every time I change the pixel dimensions. That 40, 000 pixel height was me constantly changing the size. After changing it from 50, 50 to 100, 50 to 90, 75 to etc. The result didn't change in neither the button nor the width. –  Aug 19 '15 at 00:01
  • 1
    That's because the `FlowLayout` is using the `preferredSize` of the component to make it's decisions about how the component should be laid out, overriding anything you pass to `setSize` – MadProgrammer Aug 19 '15 at 00:05

3 Answers3

4

As some users have mentioned, you can override getPreferredSize() and getMinimum/MaximumSize(). Or you could just call the setter methods for the preferred, maximum, and minimum sizes. If you set the preferred size and the maximum size...

custom.setPreferredSize(new Dimension(50, 50));
custom.setMaximumSize(new Dimension(50, 50));

then you will usually get that size.

M3579
  • 890
  • 2
  • 11
  • 22
  • Will this pixel size re-adjust to any screen monitor? –  Aug 19 '15 at 00:37
  • The size of the component will not get any bigger than the size you set here, because of the call to `setMaximumSize`. If you want the component to adjust itself based on the screen monitor size, then you can't hard code the values into the function calls. You will need to calculate the appropriate values in the program and use those. – M3579 Aug 19 '15 at 00:42
0

Start by having a look at Laying Out Components Within a Container. In Swing, and many other UI frameworks, you don't actually control the size or position of components, but instead, through different mechanisms, provide hints and constraints that allow layout managers to make the final decisions before you.

Now, before you suddenly decide that layout managers a "bad idea" and "you can do better", avoid using null/absolute layouts, pixel perfect layouts are an illusion within modern ui design. There are too many factors which affect the individual size of components, none of which you can control. Swing was designed to work with layout managers at the core, discarding these will lead to no end of issues and problems that you will spend more and more time trying to rectify

If you "really" want to effect the size of a component, then you should override the getPreferredSize (and/or getMinimumSize/getMaximumSize) method and return your required values. Beware though, these are only hints and some layout managers will ignore them (or only use them for their initial calculations)

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
0

You can actually get setSize to work if you use BorderLayout and call the setSize method on the container that has the border layout. FlowLayout is terrible and very hard to control. I use BorderLayout for all containers unless I have a specific need for GridLayout.

The advantage of BorderLayout is that you don't need to include all the regions. If you want one large panel and one small bottom panel, just put the large one in BorderLayout.CENTER and the small one in BorderLayout.SOUTH. The south panel will be just big enough to contain its elements while the center will occupy all remaining space. And the container as a whole really will be the size you say you want it to be! You can even use BorderLayout with only the center region.