0

I can't seem to figure out how to resize the width of a JButton currently in vertical state. Right now it looks like this, and I'm trying to adjust the width of the button so it fits the logo.

        JButton jbt2 = createButton("1", "discord.png", "Open the official Skyfire homepage.");
        JButton jbt3 = createButton("1", "discord.png", "Open the official Skyfire homepage.");
        JButton jbt4 = createButton("1", "discord.png", "Open the official Skyfire homepage.");
    JButton jbt1 = createButton("1", "discord.png", "Open the official Skyfire homepage.");
  Box menuPanel = Box.createVerticalBox();
        menuPanel.add(jbt1);
        menuPanel.add(jbt2);
        menuPanel.add(jbt3);
        menuPanel.add(jbt4);

        add(menuPanel);  
        frame.getContentPane().add(menuPanel, BorderLayout.EAST);
    }

    private JButton createButton(String name, String image, String tooltip) {
        JButton button = new JButton(name);
        if (image != null)
            button.setIcon(new ImageIcon(ResourceLoader.loadImage(image)));
        button.addActionListener(this);
        if (tooltip != null)
            button.setToolTipText(tooltip);
    

        button.setBorder(BorderFactory.createEmptyBorder());
        button.setContentAreaFilled(false);
        button.setCursor(new Cursor(Cursor.HAND_CURSOR));
        button.setForeground(Color.decode("#4d2600"));
        return button;
    }
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • 1) For better help sooner, [edit] to add a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). 2) 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 - to show how the extra space should be used. 3) See [Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?](http://stackoverflow.com/q/7229226/418556) (Yes.) – Andrew Thompson Sep 28 '20 at 23:37
  • See also [this answer](https://stackoverflow.com/a/10862262/418556) in which 4 buttons and 5 labels are made from a single image (each using different parts of it) with no space between them. – Andrew Thompson Sep 28 '20 at 23:41
  • *"I'm trying to adjust the width of the button so it fits the logo"* But what about the text of the button? The example I linked has no button text (the part of the image used for that button shows all the user needs to know). – Andrew Thompson Sep 28 '20 at 23:47

1 Answers1

1

so i think i found it, i just had to add

button.setPreferredSize(new Dimension(29, 40));
camickr
  • 321,443
  • 19
  • 166
  • 288
  • 1
    You don't need to do anything. Swing will automatically calculate the preferred size of the button. – camickr Sep 28 '20 at 22:31