LayoutManager Positionierung zwischen zwei Layoutkomponenten

berlichingen

Mitglied
Hallo
ich habe eine Frage zum GridbagLayout.
In einem JPanel generiere ich JButton und platziere sie per GridBagLayout. Die Positionen sehen auf dem Panel etwa so aus:

B10,B11,B12,B13
B20,B21,B22,B23

[Java]
private static GridBagConstraints constraints(
int x,
int y,
int width,
int height,
double weightx,
double weighty,
int full) {
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = x;
gbc.gridy = y;
gbc.gridwidth = width;
gbc.gridheight = height;
gbc.weightx = weightx;
gbc.weighty = weighty;
gbc.anchor = GridBagConstraints.FIRST_LINE_START;
gbc.insets = new Insets(4, 4, 4, 4);
gbc.fill = full;

return gbc;
}

public void createPanel(){
JPanel panel = new JPanel();
panel.setLayout(new GridBagLayout());
panel.add(new JButton(),constraints(0, 0, 1, 1, 1, 0,GridBagConstraints.NONE));
panel.add(new JButton(),constraints(0, 1, 1, 1, 1, 0,GridBagConstraints.NONE));
panel.add(new JButton(),constraints(0, 2, 1, 1, 1, 0,GridBagConstraints.NONE));
panel.add(new JButton(),constraints(0, 3, 1, 1, 1, 0,GridBagConstraints.NONE));
panel.add(new JButton(),constraints(1, 0, 1, 1, 1, 0,GridBagConstraints.NONE));
panel.add(new JButton(),constraints(1, 1, 1, 1, 1, 0,GridBagConstraints.NONE));
panel.add(new JButton(),constraints(1, 2, 1, 1, 1, 0,GridBagConstraints.NONE));
panel.add(new JButton(),constraints(1, 3, 1, 1, 1, 0,GridBagConstraints.NONE));
}
[/code]

Nun möchte ich über eignen separaten Button eine neue Gruppe von Buttons erzeugen die in der Reihe zwischen den beiden reihen liegt.
Die neue Aufteilung sieht dann wie Folgt aus:

B10,B11,B12,B13 // die Reihe bleibt bestehen.
B20,B21,B22,B23 // Diese Reihe wird neu eingefügt
B30,B31,B32,B33 // Das ist die alte B20...Reihe

Gibt es eine einfach möglichkeit dies zu bewerkstelligen oder muss ich erste alle Buttons des Panels abfragen und neue Positionen vergeben?

Genauer soll der Butten eigentlich immer eine Reihe mit Buttons unter die gerade Aktivierten Butten (B10,B11,…B20,B21,…) erstellen.

Vielleicht hat dazu jemand eine Idee.
Vielen Dank schon mal!
 
Hallo berlichingen,

hier ist ein kleines Beispiel:

Java:
private void insertButtonsAt(final int row) {
    Component[] components = panel.getComponents();
    for (int i = 0; i < components.length; i++) {
        Component component = components[i];
        GridBagConstraints constraints = gbl.getConstraints(component);
        if (constraints.gridy >= row) {
            constraints.gridy++;
            gbl.setConstraints(component, constraints);
            ((JButton) component).setText("Test" + constraints.gridy
                    + "_" + constraints.gridx);
        }
    }
    panel.add(new JButton("Test" + row + "_0"), constraints(0, row, 1, 1,
            1, 0, GridBagConstraints.NONE));
    panel.add(new JButton("Test" + row + "_1"), constraints(1, row, 1, 1,
            1, 0, GridBagConstraints.NONE));
    System.out.println("Test" + row);
    pack();
}

Gruß,
André
 
Vielen Dank für die schnelle Hilfe,
hat SUPER funktioniert und war auch einfach auf meine Bedürfnisse abzuändern!

Also noch mal DANKE

*kann geschlossen werden
 

Neue Themen


Zurück
Oben