Leander
Leander

Reputation: 1395

Java window is empty

I have a weird problem. I create this window, but its blank until i minimize it and maximize it again. Is this a common Problem? Here is my code(Sry because of the long code....i have only deleted the action listeners, i was afraid i could delete the error).

public class Gui_preferences extends JFrame{
private static final long serialVersionUID = -2375617814937747645L;
private JTextField savingPathField;
private JTextField schoolField;
private JTextField classStageField;
private JFormattedTextField classdurationField;
private FunctionsSchedule fkSchedule;
JFormattedTextField schoolbeginningformattedTextField;
boolean firstopening;
JLabel notClosingLabel;
private JTextField formsFolderNameField;
private JTextField subjectsFolderNameField;

public Gui_preferences(boolean first, FunctionsSchedule fktSchedule) {
    this.fkSchedule = fktSchedule;
    setPreferredSize(new Dimension(650, 420));
    setSize(650, 420);
    setResizable(false);
    setAlwaysOnTop(true);
    setLocationRelativeTo(null);
    setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    setVisible(true);

    JScrollPane scrollPane = new JScrollPane();
    getContentPane().add(scrollPane, BorderLayout.CENTER);

    JPanel panel = new JPanel();
    panel.setPreferredSize(new Dimension(500, 420));
    panel.setMinimumSize(new Dimension(500, 420));
    scrollPane.setViewportView(panel);
    panel.setLayout(new MigLayout("", "[][][grow][][][]", "[11.00][][12.00][][16.00][][17.00][][18.00][][16.00][24.00][15.00][24.00][15.00][][19.00][]"));

    JLabel savingPathLabel = new JLabel("New label");
    panel.add(savingPathLabel, "cell 0 1");

    savingPathField = new JTextField();
    savingPathField.setEditable(false);
    panel.add(savingPathField, "cell 2 1,growx");
    savingPathField.setColumns(10);

    JButton newSavingPath = new JButton("New button");
    panel.add(newSavingPath, "cell 4 1");

    JLabel school = new JLabel("New label");
    panel.add(school, "cell 0 3");

    schoolField = new JTextField();
    schoolField.addFocusListener(new FocusAdapter() {
    panel.add(schoolField, "cell 2 3,growx");
    schoolField.setColumns(10);

    JLabel classStage = new JLabel("New label");
    panel.add(classStage, "cell 0 5");
    classStageField = new JTextField();
    panel.add(classStageField, "cell 2 5,growx");
    classStageField.setColumns(10);

    JLabel classduration = new JLabel("New label");
    panel.add(classduration, "cell 0 9");

    classdurationField = new JFormattedTextField(createFormatter("###"));
    panel.add(classdurationField, "cell 2 9,growx");
    classdurationField.setColumns(10);

    JLabel schoolbeginning = new JLabel("New label");
    panel.add(schoolbeginning, "cell 0 7");

    schoolbeginningformattedTextField = new JFormattedTextField(createFormatter("##:##"));
    panel.add(schoolbeginningformattedTextField, "cell 2 7,growx");

    JLabel TrayIcon = new JLabel("New label");
    panel.add(TrayIcon, "cell 0 11");

    JCheckBox TrayIconCheckBox = new JCheckBox("New check box");
    panel.add(TrayIconCheckBox, "cell 2 11");

    JLabel otherFolderName = new JLabel("New label");
    panel.add(otherFolderName, "cell 0 13");

    formsFolderNameField = new JTextField();
    panel.add(formsFolderNameField, "cell 2 13,growx");
    formsFolderNameField.setColumns(10);

    JLabel subjectsFolderName = new JLabel("New label");
    panel.add(subjectsFolderName, "cell 0 15");

    subjectsFolderNameField = new JTextField();
    panel.add(subjectsFolderNameField, "cell 2 15,growx");
    subjectsFolderNameField.setColumns(10);

    notClosingLabel = new JLabel("New label");
    notClosingLabel.setFont(new Font("Tahoma", Font.PLAIN, 13));
    panel.add(notClosingLabel, "cell 2 17");

    //own code
    firstopening = first;
    //Labels & Co get the names
    savingPathLabel.setText(NonserData.exprs.getString("saving_Path")+": ");
    school.setText(NonserData.exprs.getString("school")+": ");
    classStage.setText(NonserData.exprs.getString("classStage")+": ");
    classduration.setText(NonserData.exprs.getString("classduration")+": ");
    TrayIcon.setText(NonserData.exprs.getString("TrayIcon")+": ");
    schoolbeginning.setText(NonserData.exprs.getString("schoolbeginning")+": ");
    TrayIconCheckBox.setText(NonserData.exprs.getString("enabled"));
    newSavingPath.setText(NonserData.exprs.getString("choose"));
    otherFolderName.setText(NonserData.GUI.getString("forms_folder_name")+": ");
    subjectsFolderName.setText(NonserData.GUI.getString("subject_Folder_name")+": ");

    //Checkbox activation/deactivation
    TrayIconCheckBox.setSelected(SerData.trayIconenabled);


    notClosingLabel.setEnabled(false);
    if(first)
    {
        setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
        notClosingLabel.setEnabled(true);
        notClosingLabel.setText(NonserData.GUI.getString("not_cosing_before_filled"));
    }
    if(!first)
    {
        notClosingLabel.setVisible(false);
        notClosingLabel.setEnabled(false);
    }
    fillTextBoxes();
    repaint();
}

Upvotes: 2

Views: 220

Answers (1)

GETah
GETah

Reputation: 21409

You should call setVisible(true); once all your components are added to the form not before. Also, remove the superfluous repaint() call at the end.

EDIT Added comment from Andrew. Note that pack() or validate() should be called before setVisible(true) for a reliably laid out GUI. Thanks @Andrew Thompson

Upvotes: 6

Related Questions