Search code examples
javauser-interfacegridbaglayout

Java GridBagLayout components not shown in full screen


I am new with Java and trying to generate my first GUI with GridBagLayout. With the code below I get my full screen but the components "label_url_eingabe" and "url_eingabe" are not displayed (empty window). When I minimize the window, they are displayed! I get the same behaviour when I set the window size to specific values. So the components are just not shown at the initial start of my program.

package ofb_reader_package;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

public class OFB_Reader_GUI extends JFrame {

// Definition der GUI Komponenten

    // Eingabefeld URL

    JLabel label_url_eingabe;
    JTextField url_eingabe;

    //this.OFB_Reader_GUI();

// Eigenschaften Hauptfenster "OFB_Reader_GUI"  
    public OFB_Reader_GUI() {

        // Eigenschaften Hauptfenster   
        setVisible(true);
        setSize(500, 300);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
        setTitle("Mark's OFB Reader");
        setResizable(true);
        setExtendedState(JFrame.MAXIMIZED_BOTH); 
        setLayout(null);




        this.label_url_eingabe = new JLabel("URL:");
        this.url_eingabe = new JTextField(10);

        this.getContentPane().setLayout(new GridBagLayout());
        GridBagConstraints c = new GridBagConstraints();


        // POSITIONIERUNGEN MIT GRIDBAGLAYOUT

        // URL Eingabe Label und Einghabefeld 
        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridx = 0;
        c.gridy = 0;
        c.ipadx = 35;
        c.weightx = 1.0;
        c.weighty = 1.0;
        c.anchor = GridBagConstraints.NORTHWEST;
        this.getContentPane().add(this.label_url_eingabe, c);


        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridx = 1;
        c.gridy = 0;
        c.ipadx = 35;
        c.weightx = 1.0;
        c.weighty = 1.0;
        c.anchor = GridBagConstraints.NORTHWEST;
        add(this.url_eingabe, c);

    }   
}

this is the main:

package ofb_reader_package;

import javax.swing.*;



public class OFB_Reader_main_class {

    public static void main(String[] args){

        new OFB_Reader_GUI();
        new OFB_Reader_main_class();

    }
}

Solution

  • Thanks a lot for your help!! I thing I got it now:

    package ofb_reader_package;
    
    import javax.swing.*;
    
    import java.awt.*;
    
    
    
    public class OFB_Reader_GUI_and_Main  {
    
    
    
    
    
        public static void addComponentsToPane(Container pane) {
    
            JLabel label_url_eingabe;
            JTextField url_eingabe;
    
            pane.setLayout(new GridBagLayout());
            GridBagConstraints c = new GridBagConstraints();        
    
            label_url_eingabe = new JLabel("URL:");
            c.fill = GridBagConstraints.HORIZONTAL;
            c.gridx = 0;
            c.gridy = 0;
            pane.add(label_url_eingabe, c);
    
            url_eingabe = url_eingabe = new JTextField(10);
            c.fill = GridBagConstraints.HORIZONTAL;
            c.gridx = 1;
            c.gridy = 0;
            pane.add(url_eingabe, c);
    
        }
    
         private static void createAndShowGUI() {
            //Create and set up the window.
            JFrame frame = new JFrame("GridBagLayoutDemo");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
            //Set up the content pane.
            addComponentsToPane(frame.getContentPane());
    
            //Display the window.
            frame.pack();
            frame.setVisible(true);
            }   
    
    
    
    
    
    
    public static void main(String[] args){
    
        //Schedule a job for the event-dispatching thread:
        //creating and showing this application's GUI.
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }
    }