GridLayout - Spaltenbreite variabel ?

Status
Nicht offen für weitere Antworten.

v2elite

Mitglied
Hai

Kann man mit einem Gridlayout 2 verschieden breite Spalten erzeugen oder ist dafür zwingend das GridBagLayout erforderlich ?


ich möchte ein borderlayout erzeugen und in CENTER ein Gridlayout plazieren

ein label und ein textfeld ungefähr so:


------Label1: |-------TextFeld-------|
langer Label2: |-------TextFeld-------|

also die textfelder spalte soll breiter sein als die label spalte
 
Hi,

also so richtig "drin" bin ich in den Layout-Managern von Java selbst nicht. Kenne BorderLayout und GridLayout und kann sie auch anwenden. GridBagLayout ist mir zu kompliziert 🙂
Aber für solche Sachen würde ich überlegen, ob ich JGoodies FormLayout (JGoodies :: Forms) nehme. Sieht ja auch so aus, als wäre es eine Art Formular. Das ist recht einfach zu handhaben.

Ansonsten würd ich BorderLayout und GridLayout kombinieren. Die Label-Spalte als eigenes Panel nach BorderLayout.WEST und die Textfeld-Spalte nach BorderLayout.CENTER. Musst nur darauf achten, dass die Zeilenhöhen immer schön gleich bleiben.

Lg
sayang
 
Musst nur darauf achten, dass die Zeilenhöhen immer schön gleich bleiben.

... und genau DAS ist das Problem 🙁 Ich h*sse das GridBagLayout eigentlich auch. Es gibt IMHO nur EINEN Bereich, wo man das sinnvoll anwenden kann - nämlich eben genau bei sowas wie
Code:
+-------+-------------------+
| label | textField         |
+-------+-------------------+
| label | textField         |
+-------+-------------------+
| label | textField         |
+-------+-------------------+
Im allgemeinen bilden bei sowas die Zeilen sinnvolle Einheiten - und das dann in "linke und rechte Hälfte" aufzuteilen kann Probleme machen...
 
Und damit ist das u.U. gar nicht sooo aufwändig - hab' da mal ein Beispiel gebastelt
Java:
import java.awt.*;
import javax.swing.*;

public class GridBagLayoutSample extends JPanel
{
    public static void main(String args[])
    {
        JFrame f = new JFrame();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.getContentPane().add(new GridBagLayoutSample());
        f.pack();
        f.setVisible(true);
    }


    public GridBagLayoutSample()
    {
        GridBagLayout gridBagLayout = new GridBagLayout();
        setLayout(gridBagLayout);

        addRow(0);
        addRow(1);
        addRow(2);
        addRow(3);
    }


    private void addRow(int y)
    {
        GridBagConstraints constraints = new GridBagConstraints();

        // These will be the same for all components in one row
        constraints.fill = GridBagConstraints.BOTH;
        constraints.insets = new Insets(4, 4, 4, 4);
        constraints.weighty = 1.0;
        constraints.gridy = y;

        // gridx  : The index of the component in the row
        // weightx: How much of the extra space that is available
        //          will be given to this component
        constraints.gridx = 0;
        constraints.weightx = 0.0;
        add(new JLabel("Label "+y), constraints);

        // The text field has weightx=1.0, thus it will get
        // all extra space that may be available
        constraints.gridx = 1;
        constraints.weightx = 1.0;
        add(new JTextField("TextField "+y), constraints);

        constraints.gridx = 2;
        constraints.weightx = 0.0;
        add(new JButton("Button "+y), constraints);
    }
}
 
Hier dann mal ein Code-Beispiel mit GridBagLayout:
Java:
import javax.swing.*;
import java.awt.*;

public class GBLTest {
   public static void main(String[] args)  {
      GridBagConstraints gbc = new GridBagConstraints();
      
      JPanel p = new JPanel(new GridBagLayout()); //Panel mit Layout anlegen
      p.setBorder(BorderFactory.createEmptyBorder(20, 10, 20, 10));
      
      //benötigte Komponenten erstellen
      JTextField tf1 = new JTextField();
      tf1.setPreferredSize(new Dimension(300, tf1.getPreferredSize().height));
      JTextField tf2 = new JTextField();
      JTextField tf3 = new JTextField();
      
      //Layout einstellen und Komponenten hinzufügen
      gbc.anchor = GridBagConstraints.EAST;
      gbc.insets = new Insets(0, 0, 5, 5);
      p.add(new JLabel("Name:"), gbc);
      
      gbc.gridy = 1;
      p.add(new JLabel("Vorname:"), gbc);
      
      gbc.gridy = 2;
      p.add(new JLabel("Staatsangehörigkeit:"), gbc);
      
      gbc.gridy = 0;
      gbc.gridx = 1;
      gbc.fill = GridBagConstraints.HORIZONTAL;
      gbc.weightx = 1.0;
      gbc.anchor = GridBagConstraints.WEST;
      gbc.insets = new Insets(0, 0, 5, 0);
      p.add(tf1, gbc);
      
      gbc.gridy = 1;
      p.add(tf2, gbc);
      
      gbc.gridy = 2;
      p.add(tf3, gbc);
      
      JFrame mf = new JFrame("GBL-Test");
      mf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      mf.add(p);
      mf.pack();
      mf.setLocationRelativeTo(null);
      mf.setVisible(true);
   }
}

Ist gar nicht sooo schwer... 😉
 
Zuletzt bearbeitet:
Status
Nicht offen für weitere Antworten.

Zurück
Oben