Ich habe festgestellt, dass in meiner Anwendung in manchen Fällen plötzlich der “Anfasser” der ScrollBar verschwindet. Dies tritt auf, wenn der Text eine gewisse Länge überschreitet.
Ich habe mal ein Beispielprogramm erstellt, an dem der Effekt zu beobachten ist:
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Dialog.ModalityType;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.UIManager;
/**
* In einem Projekt fehlen plötzlich die mittleren Anfasser der ScrollBar.
* Den Unterschied sieht man, wenn man REPEAT von 100 auf 10 wechselt.
*/
public class MissingScrollBarElementsStandAlone {
private static final int REPEAT = 100;
public static void main(String[] args) {
setNiceLayoutManager(); // ohne wird er angezeigt!
JDialog dialog = createDialog();
dialog.setVisible(true);
}
public static void setNiceLayoutManager() {
try {
UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
}
catch (Exception e) {
e.printStackTrace();
}
}
private static JDialog createDialog() {
JDialog dialog = new JDialog();
dialog.setTitle("Langer Beispieltext");
dialog.setModalityType(ModalityType.APPLICATION_MODAL);
dialog.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
dialog.setLayout(new BorderLayout());
dialog.add(createCenterPart(), BorderLayout.CENTER);
dialog.pack();
dialog.setLocation(150, 90);
return dialog;
}
private static Component createCenterPart() {
JPanel panel = new JPanel();
panel.setLayout(new BorderLayout());
JTextArea area = new JTextArea();
area.setText(getLongLoremIpsumText());
panel.add(area, BorderLayout.CENTER);
JScrollPane scroll = new JScrollPane();
scroll.setViewportView(panel);
scroll.setPreferredSize(new Dimension(800, 600));
return scroll;
}
private static String getLongLoremIpsumText() {
StringBuilder builder = new StringBuilder();
for (int i=0; i<REPEAT; ++i) {
builder.append(getLoremIpsumText());
if (i < REPEAT - 1) {
builder.append("
");
}
}
return builder.toString();
}
private static String getLoremIpsumText() {
return "Lorem ipsum dolor sit amet, consectetur adipisici " + "
"
+ "elit, sed eiusmod tempor incidunt ut labore et dolore " + "
"
+ "magna aliqua. Ut enim ad minim veniam, quis nostrud " + "
"
+ "exercitation ullamco laboris nisi ut aliquid ex ea " + "
"
+ "commodi consequat. Quis aute iure reprehenderit in " + "
"
+ "voluptate velit esse cillum dolore eu fugiat nulla " + "
"
+ "pariatur. Excepteur sint obcaecat cupiditat non proident, " + "
"
+ "sunt in culpa qui officia deserunt mollit anim id est " + "
"
+ "laborum.";
}
}```
Hat jemand dazu eine gute Idee? Ich würde ungern auf das Look & Feel verzichten. Mir scheint auch, das trat erst auf, nachdem ich meine Javaversion upgedatet habe (von 1.8.0_31 auf 1.8.0_66).
Edit: Ich habe es nochmal getestet, aufgerufen mit 1.8.0_31 wird der Anfasser angezeigt!