GridBagLayout: Bei Größenanpassung "springt" eine

  • Themenstarter Themenstarter Guest
  • Beginndatum Beginndatum
Status
Nicht offen für weitere Antworten.
G

Guest

Gast
Hallo zusammen 🙂,

ich bin dabei eine Oberfläche mit dem GridBagLayout zu erstellen und habe ein paar Schwierigkeiten. Wenn ich das JFrame starte und die Größe des Frames anpasse, verändert sich die Größe eine der enthaltenen Componenten sehr stark. Die Größe "springt" regelrecht ;-). Normalerweise sollte sich diese Größe im Verhältnis zur Größenänderung des JFrames in die gleiche Richtung verändern. Bei mir werden einige Komponenten jedoch deutlich größer, sobald ich das gesamte Fenster kleiner mache? Habt ihr eine Idee?

Ich habe für Versuche das ganze mal auf 1 Datei reduziert:

Code:
package tmp;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTable;
import javax.swing.JTextArea;

public class ProblemFrame extends JFrame {
    
    private final JTabbedPane tabbedPane = new JTabbedPane();
    private final MyPanel allProductsPanel = new MyPanel();
    
    /**
     * Konstruktor.
     */
    public ProblemFrame() {
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setSize(700, 600);
        this.initPanels();
        this.setVisible(true);
    }
    
    private void initPanels() {
        this.add(this.allProductsPanel);
    }
    
    /**
     * Main-Methode zum Aufrufen des Viewers.
     */
    public static final void main(final String[] args) {
        new ProblemFrame();
    }

}

class MyPanel extends JPanel {
    
    private final JTextArea textArea = new JTextArea();
    
    private final JTable table = new JTable();

    /**
     * Konstruktor.
     */
    MyPanel() {
        final GridBagLayout layout = new GridBagLayout();
        this.setLayout(layout);
        
        final GridBagConstraints labelAspekteC = createGBC(0, 0, 1, 2);
        final JLabel jLabelAspekte = new JLabel("Aspekte:"); //$NON-NLS-1$
        labelAspekteC.anchor = GridBagConstraints.FIRST_LINE_START;
        labelAspekteC.weighty = 0;
        this.add(jLabelAspekte, labelAspekteC);
        
        final GridBagConstraints textAreaC = createGBC(1, 0, 1, 2);
        textAreaC.anchor = GridBagConstraints.FIRST_LINE_START;
        textAreaC.fill = GridBagConstraints.BOTH;
        this.add(new JScrollPane(this.textArea), textAreaC);
        
        final GridBagConstraints tableC = createGBC(0, 2, 4, 1);
        tableC.anchor = GridBagConstraints.FIRST_LINE_START;
        tableC.fill = GridBagConstraints.BOTH;
        this.add(new JScrollPane(this.table), tableC);
        
        final JComboBox comboBox = new JComboBox();
        final GridBagConstraints comboBoxC = createGBC(2, 0, 1, 1);
        comboBoxC.anchor = GridBagConstraints.FIRST_LINE_START;
        comboBoxC.weighty = 0;
        comboBoxC.fill = GridBagConstraints.HORIZONTAL;
        this.add(comboBox, comboBoxC);
        comboBox.addItem("maßStandard");
        
        final JButton buttonTakeIt = new JButton("Übernehmen");
        
        final GridBagConstraints buttonTakeItC = createGBC(2, 1, 1, 1);
        buttonTakeItC.anchor = GridBagConstraints.FIRST_LINE_START;
        buttonTakeItC.weighty = 0;
        buttonTakeItC.fill = GridBagConstraints.HORIZONTAL;
        this.add(buttonTakeIt, buttonTakeItC);
        
        final JButton buttonDoIt = new JButton("Produkterstellung");
        
        final GridBagConstraints buttonDoItC = createGBC(3, 0, 1, 2);
        buttonDoItC.anchor = GridBagConstraints.FIRST_LINE_START;
        buttonDoItC.weighty = 0;
        buttonDoItC.fill = GridBagConstraints.HORIZONTAL;
        this.add(buttonDoIt, buttonDoItC);
    }

    private static final GridBagConstraints createGBC(final int row, final int column, final int height, 
            final int width) {
        final GridBagConstraints constraints = new GridBagConstraints();
        constraints.gridx = column;
        constraints.gridy = row;
        constraints.gridwidth = width;
        constraints.gridheight = height;
        constraints.weightx = 1;
        constraints.weighty = 1;
        constraints.ipadx = 10;
        constraints.ipady = 10;
        return constraints;
    }

}

Wenn ihr beim Start des JFrames jetzt einfach rechts die Größe verändert, das Fenster also breiter und schmaler schiebt, werdet ihr mein Problem hoffentlich auch haben.

Vielen Dank schonmal und lg,

Hendrik
 
Ich kann dir leider keine wirkliche Lösung des Problems geben, ich bin selber immer stundenlang dabei, an diesem verflixten LayoutManager rumzuschrauben. Aber scheinbar hat es etwas mit der JTable zu tun, denn wenn du in Zeile 72 statt this.table eine andere Komponente new JPanel() einfügst, gibt es keine Sprünge.
 
Das GridBagLayout macht folgendes: Wenn das gesamte Panel mit Preferred Size ausgelegt werden kann, dann werden die Preferred Sizes aller Komponenten für die Berechnung genutzt. Wenn aber der Platz zu knapp wird (entweder Breite oder Höhe oder beides) dann nutzt das GridBagLayout die Minimum Size aller Komponenten.

Tausche mal Zeile 42 aus gegen das hier:
Code:
final JScrollPane scrollPane = new JScrollPane(this.table);
scrollPane.setMinimumSize(new Dimension(400, 400));
this.add(scrollPane, tableC);

Dann siehst Du, dass das Layout sich anders verhält. Ich hoffe, der Hinweis hilft Dir weiter. Viel Erfolg!

Ebenius
 
Vielen Dank, ich werde es gleich morgen früh ausprobieren 🙂

lg,

Hendrik
 
Status
Nicht offen für weitere Antworten.

Zurück
Oben