Swing GUI - Komponenten platzieren

White_Fox

Top Contributor
Moin Leute

Ich bräuchte mal etwas Hilfe beim Platzieren von Komponenten in Swing. (Hab damit nie ernsthaft gearbeitet und nur etwas herumexperimentiert, und selbst das ist Jahre her...)

Ich will meinen kleinen Tippentrainer (den ich mal mit ImGUI gemacht habe) etwas erweitern, und das geht mit ImGUI schlecht -> also schreibe ich die GUI in Swing um. Im Wesentlichen habe ich alles drin was ich brauche, bin aber mit dem Layout noch nicht einverstanden. Ich hätte das eigentlich gerne so wie hier:

Screenshot 2025-04-27 191548.png

Hat jemand eine Idee, wie ich das so hingezogen bekomme?
Der Code wie ich das bisher mache ist hier:

PS: Habe ich es nur übersehen, oder haben Slider in Swing wirklich kein Label?
 
Hat jemand eine Idee, wie ich das so hingezogen bekomme?
Die Frage wäre, wie der freie Platz verteilt werden soll. Beispiel: wenn das Fenster vergrößert wird, soll der Platz zwischen den beiden "Spalten" vergrößert oder am rechten Rand vergrößert werden? Analog dazu: wie sieht es in der Höhe aus?

Das von @MisterBu angesprochene GridLayout wäre eine Möglichkeit, allerdings sind da die Zellen gleich groß. Wenn sie asymmetrisch wie im Bild sein sollen, dann wäre das z. B. mit einem GridBagLayout möglich. Es gibt aber viele Möglichkeiten, z. B. SpringLayout, GroupLayout... JGoodies Layout war (ist?) sehr gut, ähnlich dürfte das MigLayout sein.
 
Mit dem JVxFormLayout muesste das recht einfach gehen.

Java:
        JVxFormLayout windowFormLayout = new JVxFormLayout();
        windowFrame.setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        windowFrame.setTitle(texts.getString("1"));
        windowFrame.setLayout(windowFormLayout);

        windowFrame.add(trainingOutput, windowFormLayout.createConstraint(0, 0, -1, 0));
        windowFrame.add(userInput, windowFormLayout.createConstraint(0, 1, -1, 1));
        windowFrame.add(numRowOptionBox, windowFormLayout.createConstraint(0, 2));
        windowFrame.add(skillModeSlider, windowFormLayout.createConstraint(1, 2, -2, 2));
        windowFrame.add(skillmodeSliderLabel, windowFormLayout.createConstraint(-1, 2));
        windowFrame.add(upperRowOptionBox, windowFormLayout.createConstraint(0, 3));
        windowFrame.add(fingermodeSlider, windowFormLayout.createConstraint(1, 3, -2, 3));
        windowFrame.add(fingermodeSliderLabel, windowFormLayout.createConstraint(-1, 3));
        windowFrame.add(middleRowOptionBox, windowFormLayout.createConstraint(0, 4));
        windowFrame.add(errorStats, windowFormLayout.createConstraint(1, 4));
        windowFrame.add(bottomRowOptionBox, windowFormLayout.createConstraint(0, 5));
        windowFrame.add(speedStats, windowFormLayout.createConstraint(1, 5));

Screenshot at 2025-05-06 09:05:58.png

Eine relativ genaue Beschreibung wie das FormLayout funktioniert gibt es in dem Blog Eintrag dazu.

Das JVxFormLayout kann man sich gut kopieren, man braucht eben noch JVxConstants und zwei Methoden aus JVxUtil damit es laeuft.
 
Moin allerseits.

Vielen Dank. Bisher habe ich ja mit BoxLayout und GridbagLayout gearbeitet, das Ergebnis sieht aber etwas unerwartet aus.

Mit dem JVxFormLayout muesste das recht einfach gehen.

Java:
        JVxFormLayout windowFormLayout = new JVxFormLayout();
        windowFrame.setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        windowFrame.setTitle(texts.getString("1"));
        windowFrame.setLayout(windowFormLayout);

        windowFrame.add(trainingOutput, windowFormLayout.createConstraint(0, 0, -1, 0));
        windowFrame.add(userInput, windowFormLayout.createConstraint(0, 1, -1, 1));
        windowFrame.add(numRowOptionBox, windowFormLayout.createConstraint(0, 2));
        windowFrame.add(skillModeSlider, windowFormLayout.createConstraint(1, 2, -2, 2));
        windowFrame.add(skillmodeSliderLabel, windowFormLayout.createConstraint(-1, 2));
        windowFrame.add(upperRowOptionBox, windowFormLayout.createConstraint(0, 3));
        windowFrame.add(fingermodeSlider, windowFormLayout.createConstraint(1, 3, -2, 3));
        windowFrame.add(fingermodeSliderLabel, windowFormLayout.createConstraint(-1, 3));
        windowFrame.add(middleRowOptionBox, windowFormLayout.createConstraint(0, 4));
        windowFrame.add(errorStats, windowFormLayout.createConstraint(1, 4));
        windowFrame.add(bottomRowOptionBox, windowFormLayout.createConstraint(0, 5));
        windowFrame.add(speedStats, windowFormLayout.createConstraint(1, 5));

Anhang anzeigen 23522

Eine relativ genaue Beschreibung wie das FormLayout funktioniert gibt es in dem Blog Eintrag dazu.

Das JVxFormLayout kann man sich gut kopieren, man braucht eben noch JVxConstants und zwei Methoden aus JVxUtil damit es laeuft.
Du bist ja verrückt...vielen Dank. 🙂

Und endlich sehe ich, daß das mit den Locales auch funktioniert, das habe ich bisher nie getestet. Recht vielen Dank dafür.
 
@Robert
Mal eine Frage noch, wie hast du JVx als dependency eingebunden? Die verweisen zwar auf ein Maven Repository, aber auf dem Link kommt erstmal eine Fehlermeldung und die Suchergebnisse von sonatype.org sind doch recht viele. Ich kann notfalls alles runterladen und den Ordner so als Abhängigkeit einbinden, aber schön ist das nicht.
 
Ich wuerde einfach JVxFormLayout kopieren, also die Source-Datei, und bei dir ins Projekt packen (ist ja auch MIT). Dann brauchst du noch JVxConstants von einem Paket darueber, und zwei Methoden aus JVxUtil.

Ansonsten die bei Restlet haben, glaube ich, ihr Repository aufgeraeumt (super praktisch), aber du kannst fast alle Abhaengigkeiten ausschlieszen wenn du nur die Swing-Erweiterungen verwenden willst. Ich habe gerade nur eine Ivy Konfiguration dafuer da:

XML:
        <dependency org="com.sibvisions.jvx" name="jvxall" rev="3.1">
            <exclude org="com.fasterxml.jackson.core"/>
            <exclude org="com.fasterxml.jackson.dataformat"/>
            <exclude org="com.fasterxml.jackson.module"/>
            <exclude org="commons-fileupload"/>
            <exclude org="javax.mail"/>
            <exclude org="org.codehaus.woodstox"/>
            <exclude org="org.restlet.jee"/>
        </dependency>

Damit ist dann alles weg was du nicht brauchst. JVx ist ein gesamtes ERP/RAD-Framework, die Swing-Erweiterungen haben damit aber nichts unmittelbar zu tun, weil sie "nur" fehlende Features und so weiter in Swing nachbessern.
 
Zuletzt bearbeitet:
Also fuer Maven waere es dann:

XML:
        <dependency>
            <groupId>com.sibvisions.jvx</groupId>
            <artifactId>jvxall</artifactId>
            <version>3.1</version>

            <!-- Excluding dependencies as we will not use
            the functionality which does require them.
          
            JVx is a full-fledged ERP/RAD framework, but we're only going to use
            the Swing GUI from it together with local, held-in-memory data. -->
            <exclusions>
                <exclusion>
                    <groupId>commons-fileupload</groupId>
                    <artifactId>commons-fileupload</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>com.fasterxml.jackson.core</groupId>
                    <artifactId>jackson-core</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>com.fasterxml.jackson.core</groupId>
                    <artifactId>jackson-annotations</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>com.fasterxml.jackson.core</groupId>
                    <artifactId>jackson-databind</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>jakarta.activation</groupId>
                    <artifactId>jakarta.activation-api</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>javax.xml.bind</groupId>
                    <artifactId>jaxb-api</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>javax.mail</groupId>
                    <artifactId>javax.mail-api</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>com.fasterxml.jackson.module</groupId>
                    <artifactId>jackson-module-jsonSchema</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>com.fasterxml.jackson.dataformat</groupId>
                    <artifactId>jackson-dataformat-smile</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>com.fasterxml.jackson.dataformat</groupId>
                    <artifactId>jackson-dataformat-xml</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>com.fasterxml.jackson.module</groupId>
                    <artifactId>jackson-module-jaxb-annotations</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>com.fasterxml.jackson.dataformat</groupId>
                    <artifactId>jackson-dataformat-yaml</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>com.fasterxml.jackson.dataformat</groupId>
                    <artifactId>jackson-dataformat-csv</artifactId>
                </exclusion>
                <!-- These artifacts are only available from
                https://maven.restlet.org, however, they appear to have cleaned
                their repository of old versions, so if included a manual
                inclusion of a newer version might be rewuired.-->
                <exclusion>
                    <groupId>org.restlet.jee</groupId>
                    <artifactId>org.restlet.ext.servlet</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.restlet.jee</groupId>
                    <artifactId>org.restlet</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.restlet.jee</groupId>
                    <artifactId>org.restlet.ext.jackson</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
 
Danke, aber ich habe es mir jetzt doch so einfach gemacht und die Dateien lokal als Abhängigkeit angelegt. Ivy und Maven helfen ja nur bedingt (Gradle kann auch Mavenskripte ausführen, soweit ich weiß, aber nicht ob das für Ivy auch geht.)

Ich hatte einfach gehofft es gäbe doch einen Link auf irgendein Repository den ich nur nicht gefunden habe, dann würden nämlich auch Updates automatisch mit einfließen wenn Gradle sich die neuste Version selber sucht.
So habe ich jetzt nur das was im entsprechenden Ordner liegt...aber dafür ist die Änderung im gradle.build.kts nur ein Einzeiler.
 
Eine Frage hätte ich aber doch noch: Ich versuche gerade noch ein paar Anpassungen, z.B. hätte ich die Slider gerne deutlich schmaler.
Ich vermute mal, die vier Argumente in windowFormLayout.createConstraint() sind xy-Koordinaten sowie Breite und Höhe. Aber was ist was?

Edit: Andere Suchmaschine findet doch etwas. Demnach ist der dritte Parameter int pEndColumn und der, den ich ändern müßte.

Aber sämtliche Änderungen in beide Richtungen enden in merkwürdigen Resultaten, in jedem Fall nicht dem was ich erwarten würde, nichtmal eine "Halbierung" der Länge. Entweder verschwindet der Slider völlig, bleibt so wie er ist, oder wird viel zu groß.

Was ich auch nicht so ganz verstehe: Warum ist pEndColumn immer negativ? Und das erste Label (mit den kyrillischen Buchstaben) und die Textbox erscheinen mir auch deutlich breiter als die Parametrierung im Code vermuten läßt. Gibt es eigentlich eine Methode, die Constraints sichtbar zu schalten?
 
Das Form-Layout ist Anker-basierend, aber man kann es als Spalten/Zeilen/Zellen-basierend verwenden.

Also im Groszen und Ganzen:

Java:
createConstraint(column, row);
createConstraint(startColumn, startRow, endColumn, endRow);
``

```java
// First "cell" in the upper left corner:
createConstraint(0, 0)

// One row lower:
createConstraint(0, 1)

Negative Werte gehen dabei vom rechten/unteren Rand weg:

Java:
// First "cell" in the upper left corner:
createConstraint(0, 0)

// Last "cell" in the upper right corner:
createConstraint(-1, 0)

// Lower right "cell":
createConstraint(-1, -1)

Dabei koennen Elemente auch mehrere "Zellen" ueberspannen, wie es zum Beispiel die Slider tun.

Im Moment gehen die beiden Slider zum rechten Rand, quasi das:

Screenshot at 2025-05-06 09_05_58.png

Aber ich sehe gerade dass die praeferierte Groesze vom Slider im Weg steht. Vielleicht reicht auch einfach:

Java:
        skillModeSlider.setPreferredSize(new Dimension(0, 0));
        fingermodeSlider.setPreferredSize(new Dimension(0, 0));

Screenshot at 2025-05-14 17:23:12.png

Damit koennen die Slider zusammenfallen auf die minimale Groesze der Labels darunter.
 
Zusaetzlich dazu kannst du errorStats und speedStats auf den rechten Rand haengen, dann koennen die Slider noch kleiner werden.

Java:
        windowFrame.add(errorStats, windowFormLayout.createConstraint(1, 4, -1, 4));
        windowFrame.add(speedStats, windowFormLayout.createConstraint(1, 5, -1, 4));

Dann musst du aber eine preferredSize auf die Slider setzen, sonst koennten die bis auf Nichts kollabieren.
 
Ich hatte zwischendurch noch ein Beispiel-Projekt fier reine Swing-Applikationen mit JVx Komponenten gemacht. Eigentlich bringt ja JVx seine eigenen UI-Wrapper-Klassen mit, aber es gibt eine Schicht auf jeweils der GUI-Technologie welche man direkt verwenden kann. Das hatte ich mal in einem Blog-Eintrag naeher beschrieben.

Also JVx hat eine Swing-Erweiterungsschicht welche relativ viele, auch praktische Dinge nachreicht (wie zum Beispiel schlieszbare Tabs, wie ich gerade wieder gemerkt habe, oder eben die Layouts).
 

Zurück
Oben