Skip to content

Commit 345a9fa

Browse files
committed
Fix minimum label width
GridBagLayout does not respect minimum size, only preferred size. GridBagLayout also treats preferred size as the maximum size. In order to set a minimum width, while allowing a label to grow if text overflows, the only solution is to override getPreferredSize dynamically.
1 parent bb0932e commit 345a9fa

File tree

2 files changed

+39
-33
lines changed

2 files changed

+39
-33
lines changed

src/main/java/com/rarchives/ripme/ui/MainWindow.java

Lines changed: 6 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -65,17 +65,17 @@ public final class MainWindow implements Runnable, RipStatusHandler {
6565

6666
private static JLabel statusLabel;
6767
private static final ProgressTextField currentlyRippingProgress = new ProgressTextField();
68-
private static final JLabel pendingValue = new JLabel("0");
68+
private static final JLabel pendingValue = new MinimumWidthLabel("1000", "0");
6969
private static final JLabel pendingLabel = new JLabel("Pending");
70-
private static final JLabel activeValue = new JLabel("0");
70+
private static final JLabel activeValue = new MinimumWidthLabel("1000", "0");
7171
private static final JLabel activeLabel = new JLabel("Active");
72-
private static final JLabel completedValue = new JLabel("0");
72+
private static final JLabel completedValue = new MinimumWidthLabel("1000", "0");
7373
private static final JLabel completedLabel = new JLabel("Completed");
74-
private static final JLabel erroredValue = new JLabel("0");
74+
private static final JLabel erroredValue = new MinimumWidthLabel("1000", "0");
7575
private static final JLabel erroredLabel = new JLabel("Errored");
76-
private static final JLabel totalValue = new JLabel("0");
76+
private static final JLabel totalValue = new MinimumWidthLabel("1000", "0");
7777
private static final JLabel totalLabel = new JLabel("Total");
78-
private static final JLabel transferRateValue = new JLabel("0.00 B/s");
78+
private static final JLabel transferRateValue = new MinimumWidthLabel("999.00 KiB/s", "0.00 B/s");
7979
private static final JLabel transferRateLabel = new JLabel("Speed");
8080
private static final JButton openButton = new JButton();
8181

@@ -406,10 +406,6 @@ public void replace(FilterBypass fb, int offset, int length, String text, Attrib
406406
pendingValue.setFont(monospaced);
407407
pendingValue.setHorizontalAlignment(JLabel.TRAILING);
408408
pendingValue.setBorder(valueLabelBorder);
409-
pendingValue.setText("1000");
410-
pendingValue.setPreferredSize(pendingValue.getPreferredSize());
411-
pendingValue.setMinimumSize(pendingValue.getPreferredSize());
412-
pendingValue.setText("0");
413409
gbc = new GridBagConstraints();
414410
gbc.gridx = 0;
415411
gbc.gridy = 0;
@@ -423,10 +419,6 @@ public void replace(FilterBypass fb, int offset, int length, String text, Attrib
423419
activeValue.setFont(monospaced);
424420
activeValue.setHorizontalAlignment(JLabel.TRAILING);
425421
activeValue.setBorder(valueLabelBorder);
426-
activeValue.setText("1000");
427-
activeValue.setPreferredSize(activeValue.getPreferredSize());
428-
activeValue.setMinimumSize(activeValue.getPreferredSize());
429-
activeValue.setText("0");
430422
gbc = new GridBagConstraints();
431423
gbc.gridx = 0;
432424
gbc.gridy = 1;
@@ -440,10 +432,6 @@ public void replace(FilterBypass fb, int offset, int length, String text, Attrib
440432
completedValue.setFont(monospaced);
441433
completedValue.setHorizontalAlignment(JLabel.TRAILING);
442434
completedValue.setBorder(valueLabelBorder);
443-
completedValue.setText("1000");
444-
completedValue.setPreferredSize(completedValue.getPreferredSize());
445-
completedValue.setMinimumSize(completedValue.getPreferredSize());
446-
completedValue.setText("0");
447435
gbc = new GridBagConstraints();
448436
gbc.gridx = 3;
449437
gbc.gridy = 0;
@@ -457,10 +445,6 @@ public void replace(FilterBypass fb, int offset, int length, String text, Attrib
457445
erroredValue.setFont(monospaced);
458446
erroredValue.setHorizontalAlignment(JLabel.TRAILING);
459447
erroredValue.setBorder(valueLabelBorder);
460-
erroredValue.setText("1000");
461-
erroredValue.setPreferredSize(erroredValue.getPreferredSize());
462-
erroredValue.setMinimumSize(erroredValue.getPreferredSize());
463-
erroredValue.setText("0");
464448
gbc = new GridBagConstraints();
465449
gbc.gridx = 3;
466450
gbc.gridy = 1;
@@ -474,10 +458,6 @@ public void replace(FilterBypass fb, int offset, int length, String text, Attrib
474458
totalValue.setFont(monospaced);
475459
totalValue.setHorizontalAlignment(JLabel.TRAILING);
476460
totalValue.setBorder(valueLabelBorder);
477-
totalValue.setText("1000");
478-
totalValue.setPreferredSize(totalValue.getPreferredSize());
479-
totalValue.setMinimumSize(totalValue.getPreferredSize());
480-
totalValue.setText("0");
481461
gbc = new GridBagConstraints();
482462
gbc.gridx = 6;
483463
gbc.gridy = 0;
@@ -491,11 +471,6 @@ public void replace(FilterBypass fb, int offset, int length, String text, Attrib
491471
transferRateValue.setFont(monospaced);
492472
transferRateValue.setHorizontalAlignment(JLabel.TRAILING);
493473
transferRateValue.setBorder(valueLabelBorder);
494-
transferRateValue.setText("999.00 KiB/s"); // Maximum width value
495-
// Set preferred size to maximum width value
496-
transferRateValue.setPreferredSize(transferRateValue.getPreferredSize());
497-
transferRateValue.setMinimumSize(transferRateValue.getPreferredSize());
498-
transferRateValue.setText("0 B/s"); // Restore default value
499474
gbc = new GridBagConstraints();
500475
gbc.gridx = 6;
501476
gbc.gridy = 1;
@@ -522,8 +497,6 @@ public void replace(FilterBypass fb, int offset, int length, String text, Attrib
522497
gbc.fill = GridBagConstraints.HORIZONTAL;
523498
statusDetailPanel.add(spacer2, gbc);
524499

525-
statusDetailPanel.setPreferredSize(new Dimension(350, statusDetailPanel.getPreferredSize().height));
526-
527500
openButton.setVisible(false);
528501

529502
gbc = new GridBagConstraints();
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.rarchives.ripme.ui;
2+
3+
import javax.swing.JLabel;
4+
import java.awt.Dimension;
5+
6+
/**
7+
* GridBagLayout does not respect minimum size, only preferred size.
8+
* In order to set a minimum width, we need to override getPreferredSize.
9+
*/
10+
public class MinimumWidthLabel extends JLabel {
11+
String minimumWidthText;
12+
13+
public MinimumWidthLabel(String minimumWidthText, String defaultText) {
14+
this.minimumWidthText = minimumWidthText;
15+
setText(defaultText);
16+
}
17+
18+
@Override
19+
public Dimension getPreferredSize() {
20+
String text = getText();
21+
Dimension preferredSize = super.getPreferredSize();
22+
setText(minimumWidthText);
23+
int minimumWidth = super.getPreferredSize().width;
24+
// Hopefully GridBagLayout correctly handles maximum size and we don't need to clamp
25+
preferredSize.width = Math.max(minimumWidth, preferredSize.width);
26+
setText(text);
27+
return preferredSize;
28+
}
29+
30+
public void setMinimumWidthText(String minimumWidthText) {
31+
this.minimumWidthText = minimumWidthText;
32+
}
33+
}

0 commit comments

Comments
 (0)