[Erledigt] Eigene JButtons für Toolbar - Buttons zu breit

Hallo!

Habe mir einen Button, der von JButton erbt, für meine JToolBar geschrieben.
Er soll das Icon oben positionieren, und darunter der Text.

Der Button:

import javax.swing.*;

public class ToolBarButton extends JButton {

	public ToolBarButton(Action a) {
		
		addActionListener(a);

		this.setLayout(new BorderLayout());
		JLabel shortDescription = new JLabel((String) a
				.getValue(Action.SHORT_DESCRIPTION));
		shortDescription.setHorizontalAlignment(JLabel.CENTER);

		if (shortDescription == null) {
			shortDescription = new JLabel(getText());
		}

		if (a.getValue(Action.SMALL_ICON) != null) {
			JLabel labelIcon = new JLabel((Icon) a.getValue(Action.SMALL_ICON));
			add(BorderLayout.CENTER, labelIcon);
		}
		if (shortDescription != null) {
			this.add(shortDescription, BorderLayout.SOUTH);
		}
	}
}```

Jetzt sind aber die Buttons zu breit (ungefähre Darstellung):
[img]http://img1.abload.de/img/screenshot-06.12.200gzx.png[/img]

Wie kann ich das ändern? Oder geht das irgendwie einfacher, dass der Text **unter** dem Icon ist?

Gruß,
pcworld

mit horizontalAlignment(SwingConstants.CENTER);
und verticalAlignment(SwingConstants.BOTTOM);

Hab jetzt nicht in die API geguckt (nur so aus dem Kopf), probiere das bitte mal aus.

du kannst auch über die -Margin/Insets arbeiten

[QUOTE=L-ectron-X]mit horizontalAlignment(SwingConstants.CENTER);
und verticalAlignment(SwingConstants.BOTTOM);

Hab jetzt nicht in die API geguckt (nur so aus dem Kopf), probiere das bitte mal aus.[/QUOTE]

Hab mal folgendes ausprobiert, bringt aber nichts:

import javax.swing.*;

public class ButtonProblem extends JButton {

	public static void main(String[] args) {
		new ButtonProblem().los();
	}

	public ButtonProblem() {
		super("Test", new MyIcon());
		setVerticalAlignment(SwingConstants.CENTER);
		setVerticalTextPosition(SwingConstants.BOTTOM);
	}

	public void los() {
		JFrame frame = new JFrame();
		frame.setSize(500, 500);
		frame.setLocationRelativeTo(null);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		JToolBar tb = new JToolBar();
		tb.add(this);
		frame.add(BorderLayout.NORTH, tb);

		frame.setVisible(true);
	}
}

class MyIcon implements Icon {
	public void paintIcon(Component c, Graphics g, int x, int y) {
		g.setColor(Color.red);
		g.fillOval(x, y, getIconWidth(), getIconHeight());
	}

	public int getIconWidth() {
		return 20;
	}

	public int getIconHeight() {
		return 20;
	}
}```

**Ausgabe:**
[img]http://www.abload.de/img/screenshot-07.12.2001f1.png[/img]

Gruß,
pcworld

Keine Ahnung, was das ist :slight_smile:

Gruß,
pcworld

ganz auf die schnelle:

import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics;

import javax.swing.AbstractButton;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JToolBar;

public class ButtonProblem {

	public ButtonProblem() {
		init();
	}
	
	private void init() {
		JFrame frame = new JFrame("Foo ManChu");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		JButton button = new JButton("joe", new MyIcon());
		button.setVerticalTextPosition(AbstractButton.BOTTOM);
		button.setHorizontalTextPosition(AbstractButton.CENTER);

		JToolBar tb = new JToolBar();
		tb.add(button);
		frame.add(BorderLayout.NORTH, tb);
		
		frame.pack();
		frame.setVisible(true);
	}


	public static void main(String[] args) {
		new ButtonProblem();
	}

	static class MyIcon extends ImageIcon {
		public void paintIcon(Component c, Graphics g, int x, int y) {
			g.setColor(Color.red);
			g.fillOval(x, y, getIconWidth(), getIconHeight());
		}

		public int getIconWidth() {
			return 20;
		}

		public int getIconHeight() {
			return 20;
		}
	}

}```

EDIT:
JPanel mit JToolBar wechsel

Margins gibts z.B. bei Buttons. Sie bestimmen den Rand um den Button-Inhalt (Text/Icon).
button.setMargin(new Insets(2, 2, 2, 2));

Um den Text unter das Icon zu bekommen geht Folgendes:

button.setHorizontalTextPosition(SwingConstants.CENTER);
button.setVerticalTextPosition(SwingConstants.BOTTOM);```
hab ich probiert.

Danke, geht jetzt.

Gruß,
pcworld