Refactor component add buttons

This method works also for dark mode
This commit is contained in:
SiboVG 2023-06-30 15:38:20 +02:00
parent dfa5a3a968
commit 65c6e96446

View File

@ -209,7 +209,7 @@ public class ComponentAddButtons extends JPanel implements Scrollable {
for (col = 0; col < buttons[row].length; col++) { for (col = 0; col < buttons[row].length; col++) {
buttons[row][col].setMinimumSize(d); buttons[row][col].setMinimumSize(d);
buttons[row][col].setPreferredSize(d); buttons[row][col].setPreferredSize(d);
buttons[row][col].getComponent(0).validate(); buttons[row][col].validate();
} }
} }
@ -315,29 +315,32 @@ public class ComponentAddButtons extends JPanel implements Scrollable {
* The label may contain "\n" as a newline. * The label may contain "\n" as a newline.
*/ */
public ComponentButton(String text, Icon enabled, Icon disabled) { public ComponentButton(String text, Icon enabled, Icon disabled) {
super(); super(text, enabled);
setLayout(new MigLayout("fill, flowy, insets 0, gap 0", "", ""));
setVerticalTextPosition(SwingConstants.BOTTOM); // this will put the text below the icon
add(new JLabel(), "push, sizegroup spacing"); setHorizontalTextPosition(SwingConstants.CENTER); // this will center the text horizontally beneath the icon
//setIconTextGap(0); // this is optional, it sets the gap between the icon and the text
// Add Icon
if (enabled != null) { // set the disabled icon if it is not null
JLabel label = new JLabel(enabled); if (disabled != null) {
if (disabled != null) setDisabledIcon(disabled);
label.setDisabledIcon(disabled);
add(label, "growx");
} }
// Add labels setHorizontalAlignment(SwingConstants.CENTER); // this will center the button itself in its parent component
String[] l = text.split("\n");
for (int i = 0; i < l.length; i++) { // if you have multiline text, you could use html to format it
add(new StyledLabel(l[i], SwingConstants.CENTER, -2.0f), "growx"); if (text != null && text.contains("\n")) {
text = "<html>" + text.replace("\n", "<br>") + "</html>";
setText(text);
}
// Initialize enabled status
valueChanged(null);
// Attach a tree selection listener if selection model is not null
if (selectionModel != null) {
selectionModel.addTreeSelectionListener(this);
} }
add(new JLabel(), "push, sizegroup spacing");
valueChanged(null); // Update enabled status
selectionModel.addTreeSelectionListener(this);
} }