Agent Draxed
Agent Draxed

Reputation: 21

How to make a JButton width smaller?

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;
    }

Upvotes: 0

Views: 182

Answers (1)

Agent Draxed
Agent Draxed

Reputation: 21

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

button.setPreferredSize(new Dimension(29, 40));

Upvotes: 1

Related Questions