Probleme mit Spaltenbreite bei GridBagLayout

Status
Nicht offen für weitere Antworten.

jabbah

Mitglied
Hallo,

ich habe Fenster in dem ich ein GridBagLayout hab. darin sind ein Panel und verschieden Labels und Textfelder. In meinem Panel benutze ich ebenfalls GridBagLayout und in diesem sind auch Labels und TextFelder.

Das Panel dient dazu die in ihm enthaltenen Felder und Labels bei bedarf auszublenden und so das Fenster auf das nötigste zu beschränken. Dies klappt auch wunderbar.

Nur ist in meinem Panel die erste Spalte breiter als von meinem Frame selbst, wodurch die textFelder alles andere als bundig sind und das nicht schön aussieht. Wie bekomm ich es denn jetzt hin, dass die alle Spalten die gleiche Größe haben?
 

norman

Top Contributor
haben deine labels und textfelder alle die gleiche breite? wenn ein label/textfeld breiter ist als die spalte, in der es platziert wird, sollte sich die spalte verbreitern..
 

jabbah

Mitglied
die breite meiner Labels und Textfelder kann ich ja bei GridBagLayout nicht direkt beeinflussen. nur durch die Spaltenbreite. Und diese scheint sich nach dem breitesten Objekt in der Spalte zu richten. Da aber auf meinen Panel und meine Frame selbst alle Labels unterschiedliche Größen haben (hier durch unterschiedliche Wortlänge), ist natürlich die erste Spalte meines Panels, breites als die von meinem Frame selbst.

Ich hab das Problem erstmal so gelöst, dass ich ein Label mit Leerzeichen aufgefüllt habe, bis die Spalten gleich breit sind. Aber das kann ja nicht die beste Lösung darstellen.
 

jabbah

Mitglied
Code:
mannFrauButtonGroup              = new ButtonGroup();
mannFrauPanel                    = new JPanel();
mannButton                       = new JRadioButton();
frauButton                      = new JRadioButton();
vornameLabel                  = new JLabel();
vornameText                   = new AutoSelectTextField();
nachnameLabel                    = new JLabel();
nachnameText           = new AutoSelectTextField();
expandCollapsePanel = new JPanel();
emailLabel                 = new JLabel();
emailText                  = new AutoSelectTextField();
telefonLabel                 = new JLabel();
telefonText                  = new AutoSelectTextField();
handyLabel                = new JLabel();
handyText                  = new AutoSelectTextField();
strasseLabel                  = new JLabel();
strasseText              = new AutoSelectTextField();
adresseLabel               = new JLabel();
adresseText                = new AutoSelectTextField();
buttonPanel                     = new JPanel();
expandCollapseButton            = new JButton();
enterButton                = new JButton();
closeButton                     = new JButton();
resetButton                     = new JButton();
        
frame.getContentPane().setLayout(new GridBagLayout());
GridBagConstraints gridBagConstraints1;
        
int currentGridy = 0;
int currentGridx;

mannFrauPanel.setLayout(new GridBagLayout());
mannFrauPanel.setBorder( new EtchedBorder( EtchedBorder.LOWERED ) );

mannFrauButtonGroup.add( mannButton );
mannButton.setName("mannButton");
mannButton.setText("Mann");
mannButton.setBackground( Color.lightGray );
mannButton.setBorder( BorderFactory.createLineBorder( Color.green) );
mannButton.setBorderPainted( true );
mannButton.setHorizontalAlignment( SwingConstants.CENTER );
gridBagConstraints1 = new GridBagConstraints();
gridBagConstraints1.gridx = 0;
gridBagConstraints1.gridy = 0;
gridBagConstraints1.fill = GridBagConstraints.HORIZONTAL;
gridBagConstraints1.weightx = 1.0;
gridBagConstraints1.insets = new Insets(5, 5, 5, 5);
mannFrau.add(mannButton, gridBagConstraints1);
        
mannFrauButtonGroup.add( frauButton );
frauButton.setName("frauButton");
frauButton.setText("Frau");
frauButton.setBackground( Color.lightGray );
frauButton.setBorder( new LineBorder( Color.red ) );
frauButton.setBorderPainted( true );
frauButton.setHorizontalAlignment( SwingConstants.CENTER );
gridBagConstraints1 = new GridBagConstraints();
gridBagConstraints1.gridx = 1;
gridBagConstraints1.gridy = 0;
gridBagConstraints1.fill = GridBagConstraints.HORIZONTAL;
gridBagConstraints1.weightx = 1.0;
gridBagConstraints1.insets = new Insets(5, 5, 5, 5);
mannFrauPanel.add(frauButton, gridBagConstraints1);
        
gridBagConstraints1 = new GridBagConstraints();
gridBagConstraints1.gridx = 0;
gridBagConstraints1.gridy = currentGridy;
gridBagConstraints1.gridwidth = GridBagConstraints.REMAINDER;
gridBagConstraints1.fill = GridBagConstraints.HORIZONTAL;
gridBagConstraints1.weightx = 1.0;
gridBagConstraints1.insets = new Insets(5, 5, 5, 5);
frame.getContentPane().add(mannFrauPanel, gridBagConstraints1);
        
currentGridy++;

vornameLabel.setName("vornameLabel");
vornameLabel.setText("Vorname");
gridBagConstraints1 = new GridBagConstraints();
gridBagConstraints1.gridx = 0;
gridBagConstraints1.gridy = currentGridy;
gridBagConstraints1.anchor = GridBagConstraints.WEST;
gridBagConstraints1.insets = new Insets(0, 5, 0, 0);
frame.getContentPane().add(vornameLabel, gridBagConstraints1);
        
vornameText.setColumns(10);
// vornameText.....

currentGridy++;

nachnameLabel.setName("nachnameLabel");
nachnameLabel.setText("Nachname");
gridBagConstraints1 = new GridBagConstraints();
gridBagConstraints1.gridx = 0;
gridBagConstraints1.gridy = currentGridy;
gridBagConstraints1.anchor = GridBagConstraints.WEST;
gridBagConstraints1.insets = new Insets(0, 5, 0, 0);
frame.getContentPane().add(nachnameLabel, gridBagConstraints1);
        
nachnameText.setColumns(10);
// nachenameText....

currentGridy++;

// ==> Panel für die zu auszublendenden Felder
        
expandCollapsePanel.setLayout( new GridBagLayout() );
gridBagConstraints1.gridx = 0;
gridBagConstraints1.gridy = currentGridy;
gridBagConstraints1.gridwidth = GridBagConstraints.RELATIVE;
gridBagConstraints1.fill = GridBagConstraints.LINE_END;
gridBagConstraints1.weightx = 1.0;
gridBagConstraints1.anchor = GridBagConstraints.WEST;
gridBagConstraints1.insets = new Insets( 0, 0, 5, 0 );
frame.getContentPane().add(expandCollapsePanel, gridBagConstraints1);
        
        
emailLabel.setName("emailLabel");
emailLabel.setText("Email          ");    // fill off with space
gridBagConstraints1 = new GridBagConstraints();
gridBagConstraints1.gridx = 0;
gridBagConstraints1.gridy = currentGridy;
gridBagConstraints1.anchor = GridBagConstraints.WEST;
gridBagConstraints1.insets = new Insets(0, 5, 0, 0);
expandCollapsePanel.add(expiryLabel, gridBagConstraints1);
        
emailText.setName("emailText");
emailText.setColumns(20);
email.setHorizontalAlignment( JTextField.LEFT);
gridBagConstraints1 = new GridBagConstraints();
gridBagConstraints1.gridx = 1;
gridBagConstraints1.gridy = currentGridy;
gridBagConstraints1.anchor = GridBagConstraints.WEST;
gridBagConstraints1.insets = new Insets(0, 5, 0, 0);
gridBagConstraints1.fill = GridBagConstraints.HORIZONTAL;
expandCollapsePanel.add(emailText, gridBagConstraints1);
        
currentGridy++;
        
telefonLabel.setName("telefonLabel");
telefonLabel.setText("Tel");
gridBagConstraints1 = new GridBagConstraints();
gridBagConstraints1.gridx = 0;
gridBagConstraints1.gridy = currentGridy;
gridBagConstraints1.anchor = GridBagConstraints.WEST;
gridBagConstraints1.insets = new Insets(0, 5, 10, 0);
expandCollapsePanel.add(telefonLabel, gridBagConstraints1);
        
telefonText.setName("telefonText");
// telefonText....

// ==> end of the elements which can be hide. The first row of the panel is smaller then the first row of the frame.

currentGridy++;
currentGridx = 0;
int currentGridwidth = 2;

        
strasseLabel.setName("strasseLabel");
strasseLabel.setText("Strasse ");
gridBagConstraints1 = new GridBagConstraints();
gridBagConstraints1.gridx = currentGridx;
gridBagConstraints1.gridy = currentGridy;
gridBagConstraints1.anchor = GridBagConstraints.WEST;
gridBagConstraints1.insets = new Insets(0, 5, 0, 0);
frame.getContentPane().add(strasseLabel, gridBagConstraints1);

// strasseText ....
        

currentGridy++;

adresseLabel.setName("adressLabel");
adresseLabel.setText("Adresse");
gridBagConstraints1 = new GridBagConstraints();
gridBagConstraints1.gridx = 0;
gridBagConstraints1.gridy = currentGridy;
gridBagConstraints1.anchor = GridBagConstraints.WEST;
gridBagConstraints1.insets = new Insets(0, 5, 0, 0);
frame.getContentPane().add(adresseLabel, gridBagConstraints1);
        
adresseText.setName("adresseText");
// adresseText.....
        
currentGridy++;

buttonPanel.setLayout(new GridBagLayout());
GridBagConstraints gridBagConstraints2;
        
 // .. buttons
        
gridBagConstraints1 = new GridBagConstraints();
gridBagConstraints1.gridx = 0;
gridBagConstraints1.gridy = currentGridy;
gridBagConstraints1.gridwidth = GridBagConstraints.REMAINDER;
gridBagConstraints1.fill = GridBagConstraints.HORIZONTAL;
gridBagConstraints1.weightx = 1.0;
frame.getContentPane().add(buttonPanel, gridBagConstraints1);

Ist nicht gerade wenig, ich weiß
 

SamHotte

Top Contributor
Hmm. Ist momentan a weng unübersichtlich, der Code, aber mal auf die Schnelle:
1. für jedes Element neue Constraints benutzen (scheint der Fall zu sein
2. für alle Elemente, die gleich breit sein sollen, GridBagConstraints.fill auf GridBagConstraints.HORIZONTAL setzen
3. schauen, dass .width für alle gleich ist (am besten =1)
4. schauen, dass .weightx für alle gleich ist
5. dito für ipadx und anchor.
dann sollte es eigentlich funktionieren.

Ich benutze eigentlich fast nur weight und fill, damit muss man ein bisschen rumspielen, weil der Rest teilweise komische Ergebnisse erzielt ;-)
 

jabbah

Mitglied
eigentlich hab ich ja all diese fünf punkte befolgt. aber das Problem scheint ja zu sein, dass in meine Frame in GridBagConstrains_A hat, dessen breite der ersten spalte durch das wort Telefon bestimmt wird. in diese spalte wird meine panel mit dem GridBagConstrains_B geadded, dessen breite der ersten spalte durch das wort Email bestimmt wird.
 

SamHotte

Top Contributor
Da mir bei deinem Code so einige Klassen zum compilieren fehlen, kann ich leider auch nicht wirklich weiterhelfen.
 

André Uhres

Top Contributor
jabbah hat gesagt.:
...in diese spalte wird meine panel mit dem GridBagConstrains_B geadded,
dessen breite der ersten spalte durch das wort Email bestimmt wird.
Das Wort Email hängt in der Luft. emailLabel wird nämlich nirgends eingefügt.
Mit so nem Bandwurm der nicht mal kompilierbar ist kann man eh nix anfangen.
Versuch ein KSKB zu erstellen (klick bitte auf den Link unter meinem Namen).
Gruß,
André
http://www.java-forum.org/de/viewtopic.php?p=171842#171842
 

André Uhres

Top Contributor
Wenn man den Code, nachdem man in kompilierbar gemacht hat, endlich ausführen kann, kommt dies raus:


Das erste Bild ist mit mit dem Panel.
Beim zweiten ist der Panel ausgeblendet.
Wo ist jetzt das Problem zu sehen?
 

jabbah

Mitglied
ich hab es jetzt nochmal bei mir laufen lassen. und wenn du mir verrätst, wie ich es schaffe bilder anzuzeigen, dann kann ich dir das Problem zeigen
 

jabbah

Mitglied
vielen dank für den hinweis mit den bildern.

mein dialog sind so aus bei nachfolgendem Quellcode:



Code:
import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;




public class EnterOrderDialog{

    
    static public void main( String[] args ) {

      
        EnterOrderDialog orderDialog = new EnterOrderDialog(  0 );
    }




    private JFrame                  	frame;
    private JRadioButton                mannButton;
    private JRadioButton                frauButton;
    private ButtonGroup                 mannfrauButtonGroup;
    private JPanel                      mannfrauPanel;
    private JLabel                      VornameLabel;
    private JTextField      		    VornameText;
    private JLabel                      NachnameLabel;
    private JTextField         			NachnameText;
    private JPanel                      expandCollapsePanel;
    private JLabel                      telLabel;
    private JTextField         			telText;
    private JLabel                      strasseLabel;
    private JTextField         			strasseText;
    private JLabel                      HausnummerLabel;
    private JTextField         			HausnummerText;
    private JPanel                      buttonPanel;
    private JButton                     expandCollapseButton;
    private JButton                     enterOrderButton;
    private JButton                     closeButton;
    private JButton                     resetButton;
    private int                         dialogMode;



    public EnterOrderDialog( int               dialogMode ) {

        this.dialogMode        = dialogMode;

        initFrame( );
         frame.setVisible( true );

        
    }

    


    /** Main initialization for frame
     *  including title, size, listeners, controls, etc...
     */
    private void initFrame(  ) {

        GraphicsEnvironment   graphicsEnvironment   = GraphicsEnvironment.getLocalGraphicsEnvironment();
        GraphicsDevice        graphicsDevice        = graphicsEnvironment.getDefaultScreenDevice();
        GraphicsConfiguration graphicsConfiguration = graphicsDevice.getDefaultConfiguration();
        frame = new JFrame( );
        frame.setDefaultCloseOperation( WindowConstants.DO_NOTHING_ON_CLOSE );

        
    
        initComponents();
        
        
//    

        frame.pack();
        frame.setResizable( false );

 
        
    }



    /** Initialization of visible components
     *  like labels, input fields, tables, etc...
     */
    private void initComponents() {

        mannfrauButtonGroup              = new ButtonGroup();
        mannfrauPanel                        = new JPanel();
        mannButton                            = new JRadioButton();
        frauButton                              = new JRadioButton();
        VornameLabel                         = new JLabel();
        VornameText                    	      = new JTextField();
        NachnameLabel                	    = new JLabel();
        NachnameText                  	     = new JTextField();
        expandCollapsePanel               = new JPanel();
        telLabel                 		   = new JLabel();
        telText                  		    = new JTextField();
        strasseLabel      			   	 = new JLabel();
        strasseText       				 = new JTextField();
        HausnummerLabel   				 = new JLabel();
        HausnummerText    				 = new JTextField();
        buttonPanel                      = new JPanel();
        expandCollapseButton             = new JButton();
        enterOrderButton                 = new JButton();
        closeButton                      = new JButton();
        resetButton                      = new JButton();
        
        frame.getContentPane().setLayout(new GridBagLayout());
        GridBagConstraints gridBagConstraints1;
        
        int currentGridy = 0;
        int currentGridx;

        mannfrauPanel.setLayout(new GridBagLayout());
        mannfrauPanel.setBorder( new EtchedBorder( EtchedBorder.LOWERED ) );

        mannfrauButtonGroup.add( mannButton );
        mannButton.setName("mannButton");
        mannButton.setText("mann");
        mannButton.setBackground( Color.lightGray );
        mannButton.setBorder( BorderFactory.createLineBorder( Color.green) );
        mannButton.setBorderPainted( true );
        mannButton.setHorizontalAlignment( SwingConstants.CENTER );
        gridBagConstraints1 = new GridBagConstraints();
        gridBagConstraints1.gridx = 0;
        gridBagConstraints1.gridy = 0;
        gridBagConstraints1.fill = GridBagConstraints.HORIZONTAL;
        gridBagConstraints1.weightx = 1.0;
        gridBagConstraints1.insets = new Insets(5, 5, 5, 5);
        mannfrauPanel.add(mannButton, gridBagConstraints1);
        
        mannfrauButtonGroup.add( frauButton );
        frauButton.setName("frauButton");
        frauButton.setText("frau");
        frauButton.setBackground( Color.lightGray );
        frauButton.setBorder( new LineBorder( Color.red ) );
        frauButton.setBorderPainted( true );
        frauButton.setHorizontalAlignment( SwingConstants.CENTER );
        gridBagConstraints1 = new GridBagConstraints();
        gridBagConstraints1.gridx = 1;
        gridBagConstraints1.gridy = 0;
        gridBagConstraints1.fill = GridBagConstraints.HORIZONTAL;
        gridBagConstraints1.weightx = 1.0;
        gridBagConstraints1.insets = new Insets(5, 5, 5, 5);
        mannfrauPanel.add(frauButton, gridBagConstraints1);
        
        gridBagConstraints1 = new GridBagConstraints();
        gridBagConstraints1.gridx = 0;
        gridBagConstraints1.gridy = currentGridy;
        gridBagConstraints1.gridwidth = GridBagConstraints.REMAINDER;
        gridBagConstraints1.fill = GridBagConstraints.HORIZONTAL;
        gridBagConstraints1.weightx = 1.0;
        gridBagConstraints1.insets = new Insets(5, 5, 5, 5);
        frame.getContentPane().add(mannfrauPanel, gridBagConstraints1);
        
        currentGridy++;

        VornameLabel.setName("VornameLabel");
        VornameLabel.setText("Size");
        gridBagConstraints1 = new GridBagConstraints();
        gridBagConstraints1.gridx = 0;
        gridBagConstraints1.gridy = currentGridy;
        gridBagConstraints1.anchor = GridBagConstraints.WEST;
        gridBagConstraints1.insets = new Insets(0, 5, 0, 0);
        frame.getContentPane().add(VornameLabel, gridBagConstraints1);
        
        VornameText.setColumns(10);
        VornameText.setName("VornameText");
        VornameText.setHorizontalAlignment( JTextField.RIGHT );
        gridBagConstraints1 = new GridBagConstraints();
        gridBagConstraints1.gridx = 1;
        gridBagConstraints1.gridy = currentGridy;
        gridBagConstraints1.anchor = GridBagConstraints.WEST;
        gridBagConstraints1.insets = new Insets(0, 5, 0, 0);
        gridBagConstraints1.fill = GridBagConstraints.HORIZONTAL;
        frame.getContentPane().add(VornameText, gridBagConstraints1);
        
        currentGridy++;

        NachnameLabel.setName("NachnameLabel");
        NachnameLabel.setText("Limit Price");
        gridBagConstraints1 = new GridBagConstraints();
        gridBagConstraints1.gridx = 0;
        gridBagConstraints1.gridy = currentGridy;
        gridBagConstraints1.anchor = GridBagConstraints.WEST;
        gridBagConstraints1.insets = new Insets(0, 5, 0, 0);
        gridBagConstraints1.fill = GridBagConstraints.HORIZONTAL;
        frame.getContentPane().add(NachnameLabel, gridBagConstraints1);
        
        NachnameText.setName("NachnameText");
        NachnameText.setColumns(10);
        NachnameText.setEnabled( true );
        NachnameText.setHorizontalAlignment( JTextField.RIGHT );
        gridBagConstraints1 = new GridBagConstraints();
        gridBagConstraints1.gridx = 1;
        gridBagConstraints1.gridy = currentGridy;
        gridBagConstraints1.anchor = GridBagConstraints.WEST;
        gridBagConstraints1.insets = new Insets(0, 5, 0, 0);
        gridBagConstraints1.fill = GridBagConstraints.HORIZONTAL;
        frame.getContentPane().add(NachnameText, gridBagConstraints1);
        
        currentGridy++;

        expandCollapsePanel.setLayout( new GridBagLayout() );
        gridBagConstraints1.gridx = 0;
        gridBagConstraints1.gridy = currentGridy;
        gridBagConstraints1.gridwidth = GridBagConstraints.RELATIVE;
        gridBagConstraints1.fill = GridBagConstraints.LINE_END;
        gridBagConstraints1.weightx = 1.0;
        gridBagConstraints1.anchor = GridBagConstraints.WEST;
        gridBagConstraints1.insets = new Insets( 0, 0, 5, 0 );
        frame.getContentPane().add(expandCollapsePanel, gridBagConstraints1);
        
        telLabel.setName("telLabel");
        telLabel.setText("Tel");
        gridBagConstraints1 = new GridBagConstraints();
        gridBagConstraints1.gridx = 0;
        gridBagConstraints1.gridy = currentGridy;
        gridBagConstraints1.anchor = GridBagConstraints.WEST;
        gridBagConstraints1.insets = new Insets(0, 5, 10, 0);
        expandCollapsePanel.add(telLabel, gridBagConstraints1);
        
        telText.setName("telText");
        telText.setColumns(8);
        gridBagConstraints1 = new GridBagConstraints();
        gridBagConstraints1.gridx = 1;
        gridBagConstraints1.gridy = currentGridy;
        gridBagConstraints1.anchor = GridBagConstraints.WEST;
        gridBagConstraints1.insets = new Insets(0, 5, 10, 0);
        gridBagConstraints1.fill = GridBagConstraints.HORIZONTAL;
        expandCollapsePanel.add(telText, gridBagConstraints1);
      
        currentGridy++;
        currentGridx = 0;

        HausnummerLabel.setName("HausnummerLabel");
        HausnummerLabel.setText("hausnummer");
        gridBagConstraints1 = new GridBagConstraints();
        gridBagConstraints1.gridx = 0;
        gridBagConstraints1.gridy = currentGridy;
        gridBagConstraints1.anchor = GridBagConstraints.WEST;
        gridBagConstraints1.insets = new Insets(0, 5, 0, 0);
        frame.getContentPane().add( HausnummerLabel, gridBagConstraints1 );
        
        HausnummerText.setName("HausnummerText");
        HausnummerText.setColumns(10);
        HausnummerText.setHorizontalAlignment( JTextField.LEFT );
        gridBagConstraints1 = new GridBagConstraints();
        gridBagConstraints1.gridwidth = 2;
        gridBagConstraints1.gridx = 1;
        gridBagConstraints1.gridy = currentGridy;
        gridBagConstraints1.anchor = GridBagConstraints.WEST;
        gridBagConstraints1.insets = new Insets(0, 5, 0, 5);
        gridBagConstraints1.fill = GridBagConstraints.HORIZONTAL;
       frame.getContentPane().add( HausnummerText, gridBagConstraints1 );
        
        
        
        currentGridy++;
        currentGridx = 0;
        int currentGridwidth = 2;

        // XXX fill the text of strasseLabel with spaces thats why the 1st row is shorter then the 1st row of the expandCollapsePanel.
        // XXX And this will diplaced the text fields
        strasseLabel.setName("strasseLabel");
        strasseLabel.setText("strasse ");
        gridBagConstraints1 = new GridBagConstraints();
        gridBagConstraints1.gridx = currentGridx;
        gridBagConstraints1.gridy = currentGridy;
        gridBagConstraints1.anchor = GridBagConstraints.WEST;
        gridBagConstraints1.insets = new Insets(0, 5, 0, 0);
        frame.getContentPane().add(strasseLabel, gridBagConstraints1);
        
        currentGridx++;

        strasseText.setName("strasseText");
        strasseText.setColumns(10);
        gridBagConstraints1 = new GridBagConstraints();
        gridBagConstraints1.gridwidth = currentGridwidth;
        gridBagConstraints1.gridx = currentGridx;
        gridBagConstraints1.gridy = currentGridy;
        gridBagConstraints1.anchor = GridBagConstraints.WEST;
        gridBagConstraints1.insets = new Insets(0, 5, 0, 5);
        gridBagConstraints1.fill = GridBagConstraints.HORIZONTAL;
        frame.getContentPane().add(strasseText, gridBagConstraints1);
        
        currentGridy++;

        gridBagConstraints1 = new GridBagConstraints();
        gridBagConstraints1.gridx = 0;
        gridBagConstraints1.gridy = currentGridy;
        gridBagConstraints1.gridwidth = GridBagConstraints.REMAINDER;
        gridBagConstraints1.fill = GridBagConstraints.HORIZONTAL;
        gridBagConstraints1.insets = new Insets(20, 0, 10, 0);
        frame.getContentPane().add( new JSeparator(), gridBagConstraints1);
        
        currentGridy++;

        buttonPanel.setLayout(new GridBagLayout());
        GridBagConstraints gridBagConstraints2;
        
        expandCollapseButton.setName( "expandCollapseButton" );
        expandCollapseButton.setText( "Expand" );
        expandCollapseButton.setHorizontalTextPosition( AbstractButton.LEFT );
        gridBagConstraints2 = new GridBagConstraints();
        gridBagConstraints2.gridx = 0;
        gridBagConstraints2.gridy = 2;
        gridBagConstraints2.fill = GridBagConstraints.HORIZONTAL;
        gridBagConstraints2.weightx = 1.0;
        gridBagConstraints2.insets = new Insets(10, 10, 10, 20);
        buttonPanel.add(expandCollapseButton, gridBagConstraints2);
        
        enterOrderButton.setName( "enterOrderButton" );
        enterOrderButton.setText( "eingabe" );
        //gridBagConstraints2 = new GridBagConstraints();
        gridBagConstraints2.gridx = 1;
        gridBagConstraints2.gridy = 2;
        gridBagConstraints2.fill = GridBagConstraints.HORIZONTAL;
        gridBagConstraints2.weightx = 1.0;
        gridBagConstraints2.insets = new Insets(10, 10, 10, 10);
        buttonPanel.add(enterOrderButton, gridBagConstraints2);
        
        resetButton.setName("resetButton");
        resetButton.setText("Reset");
        gridBagConstraints2 = new GridBagConstraints();
        gridBagConstraints2.gridx = 2;
        gridBagConstraints2.gridy = 2;
        gridBagConstraints2.fill = GridBagConstraints.HORIZONTAL;
        gridBagConstraints2.weightx = 1.0;
        gridBagConstraints2.insets = new Insets(10, 10, 10, 10);
        buttonPanel.add(resetButton, gridBagConstraints2);
        
        closeButton.setName("closeButton");
        closeButton.setText("Close");
        gridBagConstraints2 = new GridBagConstraints();
        gridBagConstraints2.gridx = 3;
        gridBagConstraints2.gridy = 2;
        gridBagConstraints2.fill = GridBagConstraints.HORIZONTAL;
        gridBagConstraints2.weightx = 1.0;
        gridBagConstraints2.insets = new Insets(10, 10, 10, 10);
        buttonPanel.add(closeButton, gridBagConstraints2);

        gridBagConstraints1 = new GridBagConstraints();
        gridBagConstraints1.gridx = 0;
        gridBagConstraints1.gridy = currentGridy;
        gridBagConstraints1.gridwidth = GridBagConstraints.REMAINDER;
        gridBagConstraints1.fill = GridBagConstraints.HORIZONTAL;
        gridBagConstraints1.weightx = 1.0;
        frame.getContentPane().add(buttonPanel, gridBagConstraints1);
        
        currentGridy++;

        gridBagConstraints1 = new GridBagConstraints();
        gridBagConstraints1.gridx = 0;
        gridBagConstraints1.gridy = currentGridy;
        gridBagConstraints1.gridwidth = GridBagConstraints.REMAINDER;
        gridBagConstraints1.fill = GridBagConstraints.HORIZONTAL;
        gridBagConstraints1.insets = new Insets(10, 0, 5, 0);
        frame.getContentPane().add( new JSeparator(), gridBagConstraints1);

    }
}
 

norman

Top Contributor
[off-topic] mit ALT + DRUCK kannst du ein screenshot vom aktuellen (aktiven) Fenster machen, dann brauchst du nichts ausschneiden und es gibt keine hässlichen weißen ränder :wink: [/off-topic]
 

jabbah

Mitglied
nur leider bring mich der ratschlag über ränderfreie screenshoots meinem problem nicht ein stückchen näher :cry:
 

André Uhres

Top Contributor
Mach einen Panel pro Spalte.
Code:
import java.awt.*;
import javax.swing.*;
/*
 * GBLTest.java
 */
public class GBLTest extends JFrame {
    public GBLTest() {
        GridBagConstraints gridBagConstraints;

        jLabel1 = new JLabel();
        jTextField1 = new JTextField();
        jPanel1 = new JPanel();
        jLabel2 = new JLabel();
        jPanel2 = new JPanel();
        jTextField2 = new JTextField();
        jLabel3 = new JLabel();
        jTextField3 = new JTextField();

        getContentPane().setLayout(new GridBagLayout());

        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        jLabel1.setText("jLabel1");
        gridBagConstraints = new GridBagConstraints();
        gridBagConstraints.anchor = GridBagConstraints.WEST;
        getContentPane().add(jLabel1, gridBagConstraints);

        jTextField1.setText("jTextField1");
        gridBagConstraints = new GridBagConstraints();
        gridBagConstraints.anchor = GridBagConstraints.WEST;
        getContentPane().add(jTextField1, gridBagConstraints);

        jPanel1.setLayout(new GridBagLayout());

        jLabel2.setText("jLabel2");
        jPanel1.add(jLabel2, new GridBagConstraints());

        gridBagConstraints = new GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.anchor = GridBagConstraints.WEST;
        getContentPane().add(jPanel1, gridBagConstraints);

        jPanel2.setLayout(new GridBagLayout());

        jTextField2.setText("jTextField2jTextField2");
        jPanel2.add(jTextField2, new GridBagConstraints());

        gridBagConstraints = new GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.anchor = GridBagConstraints.WEST;
        getContentPane().add(jPanel2, gridBagConstraints);

        jLabel3.setText("jLabel3jLabel3");
        gridBagConstraints = new GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.anchor = GridBagConstraints.WEST;
        getContentPane().add(jLabel3, gridBagConstraints);

        jTextField3.setText("jTextField3");
        gridBagConstraints = new GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.anchor = GridBagConstraints.WEST;
        getContentPane().add(jTextField3, gridBagConstraints);

        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-400)/2, (screenSize.height-300)/2, 400, 300);
    }
    public static void main(String args[]) {new GBLTest().setVisible(true);}
    private JLabel jLabel1;
    private JLabel jLabel2;
    private JLabel jLabel3;
    private JPanel jPanel1;
    private JPanel jPanel2;
    private JTextField jTextField1;
    private JTextField jTextField2;
    private JTextField jTextField3;
}
Wie du siehst kann man das Beispiel auf weniger als 100 Zeilen reduzieren.
Das ist viel übersichtlicher als ein Beispiel von über 300 Zeilen.
Und die Lösung des Problems findet man viel leichter und schneller.
Das meine ich mit KSKB.
 

jabbah

Mitglied
hab das Problem gelöst. ist zwar nicht gerade ein wirklich zufriedenstellendes ergebnis, das es dann auch noch probleme mit den größen der Textfelder gab. und einige textfelder dann unter bestimmten doch immer sichtbar sein sollten und woanders gehidet werden können.
im moment setze ich die sichtbarkeit jedes elementes einzeln. nicht gerade schön, aber stressfreier
 
Status
Nicht offen für weitere Antworten.
Ähnliche Java Themen
  Titel Forum Antworten Datum
Juelin Probleme bei Stage.close() AWT, Swing, JavaFX & SWT 23
XWing Swing Image anzeigen und probleme mit klassen AWT, Swing, JavaFX & SWT 3
E repaint Probleme AWT, Swing, JavaFX & SWT 13
mananana Mögliche probleme die in einer GUI passieren Können AWT, Swing, JavaFX & SWT 6
S GridBagLayout - Probleme mit Bilderanzeige AWT, Swing, JavaFX & SWT 3
I Probleme beim Drucken auf einen PDF-Drucker AWT, Swing, JavaFX & SWT 8
J Probleme mit idividueller Tablecell AWT, Swing, JavaFX & SWT 0
D JavaFX Probleme beim nachtäglichen hinzufügen der jfx dependency AWT, Swing, JavaFX & SWT 7
J Probleme mit InputDialog AWT, Swing, JavaFX & SWT 4
D JavaFX TextArea Probleme bei langen Zeilen AWT, Swing, JavaFX & SWT 1
G JavaFX SplitPane Anwendung - Controller Probleme AWT, Swing, JavaFX & SWT 5
K Probleme bei der Erstellung und Ausführung einer Jar Datei AWT, Swing, JavaFX & SWT 2
B Probleme Action Listener Taschenrechner AWT, Swing, JavaFX & SWT 27
pph080560 JavaFX Probleme mit FX AWT, Swing, JavaFX & SWT 3
M Probleme mit OpenJDK AWT, Swing, JavaFX & SWT 6
B 2D-Grafik paintcomponent Probleme beim zeichnen AWT, Swing, JavaFX & SWT 10
B Swing Probleme mit dem Layout AWT, Swing, JavaFX & SWT 1
L JavaFX Probleme beim Installieren JavaFX11 / JavaFX12 -- Eclipse 2019-03 AWT, Swing, JavaFX & SWT 3
Fiedlerdan Image-Pfad Probleme nach Export aus Eclipse AWT, Swing, JavaFX & SWT 31
H JFreeChart - DemoDataSetFactory Probleme AWT, Swing, JavaFX & SWT 1
H LayoutManager Probleme mit Positionierung/Abständen der Komponenten AWT, Swing, JavaFX & SWT 14
A Probleme mit gridheight (GridBagLayout) AWT, Swing, JavaFX & SWT 6
U Opaque Probleme AWT, Swing, JavaFX & SWT 3
H JavaFX Probleme Beim Wechseln der scene als .fxml AWT, Swing, JavaFX & SWT 7
F JavaFX Probleme beim automatischen Konvertieren AWT, Swing, JavaFX & SWT 4
S Probleme mit JComboboxen(?) AWT, Swing, JavaFX & SWT 18
S Swing Probleme mit MigLayout AWT, Swing, JavaFX & SWT 2
C Probleme mit createImage AWT, Swing, JavaFX & SWT 1
J Probleme mit contex Menu (javafx) AWT, Swing, JavaFX & SWT 1
J Probleme bei GameofLife AWT, Swing, JavaFX & SWT 24
S JavaFx - Button ActionEvent Probleme AWT, Swing, JavaFX & SWT 3
T Swing Probleme mit repaint() bzw. JScrollPane AWT, Swing, JavaFX & SWT 7
ImperatorMing JavaFX Probleme mit WindowEvent AWT, Swing, JavaFX & SWT 0
ImperatorMing JavaFX Probleme mit WindowEvent AWT, Swing, JavaFX & SWT 5
J LayoutManager GridBagLayout, probleme mit Anordnung von Objekten AWT, Swing, JavaFX & SWT 6
T Java FX Probleme beim befüllen eines Tableviews AWT, Swing, JavaFX & SWT 5
S AWT Probleme beim Zeichnen AWT, Swing, JavaFX & SWT 3
A Swing Probleme mit dem adden von JButtons zur JScrollPane AWT, Swing, JavaFX & SWT 2
D Swing Probleme mit dem Resizing AWT, Swing, JavaFX & SWT 7
G Probleme mit TextArea AWT, Swing, JavaFX & SWT 5
G JFrame Probleme AWT, Swing, JavaFX & SWT 2
K Probleme beim JPasswordField AWT, Swing, JavaFX & SWT 11
G Cardlayout Refresh Probleme AWT, Swing, JavaFX & SWT 2
J Swing Probleme mit ListSelectionListener(), Inhalte der JList werden gelöscht? AWT, Swing, JavaFX & SWT 6
D JavaFX Probleme bei Service-Klasse beim ändern der GUI AWT, Swing, JavaFX & SWT 8
K Probleme beim zeichnen mit paintComponent() AWT, Swing, JavaFX & SWT 1
M JButton Probleme AWT, Swing, JavaFX & SWT 14
L Probleme mit Programm AWT, Swing, JavaFX & SWT 13
blazingblade komischerweise probleme mit jtextfield.gettext() AWT, Swing, JavaFX & SWT 9
Xanny 2D-Grafik Beginner! Probleme mit Swing, Gprahics class und paint AWT, Swing, JavaFX & SWT 13
Sin137 LayoutManager GridBagLayout Probleme AWT, Swing, JavaFX & SWT 6
H Netbeans Designer: Probleme mit JPanel und JFrame AWT, Swing, JavaFX & SWT 2
M Swing Probleme mit Frame.pack() AWT, Swing, JavaFX & SWT 1
C Java FX Probleme beim Schließen einer Stage AWT, Swing, JavaFX & SWT 11
M Swing JProgressbar und Outoputstream probleme AWT, Swing, JavaFX & SWT 2
S Swing Probleme mit transparenz der Hintergrundfarbe und JRadioButtons AWT, Swing, JavaFX & SWT 2
Z Probleme mit JPanel's AWT, Swing, JavaFX & SWT 6
T Probleme mit Anzeige von Elementen im JPanel AWT, Swing, JavaFX & SWT 1
Shams Probleme bei dem Hinzufügen von Komponenten zu einem JFrame AWT, Swing, JavaFX & SWT 3
A Swing Probleme mit JScrollPane AWT, Swing, JavaFX & SWT 6
M Layout-Probleme unter Swing AWT, Swing, JavaFX & SWT 5
H Swing Probleme beim erstellen eines neuen Objektes durch einen Button AWT, Swing, JavaFX & SWT 10
J JavaFX JavaFX Probleme bei der Anzeige von Text AWT, Swing, JavaFX & SWT 18
A Probleme mit TilledBorder("***") AWT, Swing, JavaFX & SWT 4
F Bildschirmschoner Probleme mit Preview AWT, Swing, JavaFX & SWT 8
X Panel Probleme (Tetris) AWT, Swing, JavaFX & SWT 8
N JTable probleme AWT, Swing, JavaFX & SWT 5
B Probleme bei ImageIO.read (?!) AWT, Swing, JavaFX & SWT 9
P JFrame Location-/Size-Probleme AWT, Swing, JavaFX & SWT 5
U LayoutManager Probleme mit Layouts AWT, Swing, JavaFX & SWT 5
C 3 kleine Probleme... AWT, Swing, JavaFX & SWT 13
L NullpointerException und Probleme mit repaint() AWT, Swing, JavaFX & SWT 11
A Probleme mit 2 JFrames in einem Programm AWT, Swing, JavaFX & SWT 7
K LayoutManager Probleme mit 2 Komponenten AWT, Swing, JavaFX & SWT 9
C Probleme mit Buttons und einem ActionListener AWT, Swing, JavaFX & SWT 2
M Probleme mit Verkleinern eines GUI AWT, Swing, JavaFX & SWT 7
B Swing label.setText() macht probleme AWT, Swing, JavaFX & SWT 5
B ImageIcon - Probleme mit dem Dateipfad AWT, Swing, JavaFX & SWT 5
H JTree Probleme AWT, Swing, JavaFX & SWT 9
F Probleme mit (Graphics g) II AWT, Swing, JavaFX & SWT 4
F Probleme mit (Graphics g) AWT, Swing, JavaFX & SWT 3
K 2D-Grafik .GIF macht mir Probleme AWT, Swing, JavaFX & SWT 14
B Probleme bei Sortierung einer Tabelle mit DefaultTableModel AWT, Swing, JavaFX & SWT 6
T JTable Graphik probleme AWT, Swing, JavaFX & SWT 3
H GridBagLayout macht Probleme... AWT, Swing, JavaFX & SWT 4
vandread Swing Probleme mit jTextField im zusammenspiel mit einem Einblendeffekt (inkl. KSKB) AWT, Swing, JavaFX & SWT 6
S Swing Lauftext Performance Probleme, in größerer Anwendung AWT, Swing, JavaFX & SWT 6
B Infolabel Probleme mit Paint() ? AWT, Swing, JavaFX & SWT 8
P Probleme mit setIconImage AWT, Swing, JavaFX & SWT 8
U Swing Probleme mit Asynchronen Prozessen und Swing: AWT, Swing, JavaFX & SWT 3
das-mo Probleme mit GridBagLayout AWT, Swing, JavaFX & SWT 6
N 2D-Grafik 2 Probleme beim zeichnen AWT, Swing, JavaFX & SWT 18
L Probleme mit JPanel AWT, Swing, JavaFX & SWT 8
D Probleme bei Übertrag von ArrayList in Jtable AWT, Swing, JavaFX & SWT 2
G kleinere Probleme mit GUI AWT, Swing, JavaFX & SWT 2
G Mehrere Probleme mit Java's GUI AWT, Swing, JavaFX & SWT 6
M Swing Erste Schritte mit der GUI, viele Probleme AWT, Swing, JavaFX & SWT 36
J GUI-Aktualisierungs-Probleme AWT, Swing, JavaFX & SWT 4
B Performance-Probleme AWT, Swing, JavaFX & SWT 17
I Probleme if-Anweisung AWT, Swing, JavaFX & SWT 26

Ähnliche Java Themen

Neue Themen


Oben