mrdarb
mrdarb

Reputation: 101

Java GUI is blank

I began building a GUI using net-beans GUI builder and since then I have decided to try rebuild it without the GUI builder, as I think it is probably bad practise and doesn't really allow me to learn anything with swing.

I've tried to start rebuilding the GUI but when I run the program a blank GUI without any features pops up, was wondering if anyone could help me as to why it is doing this? My program consists of two classes used for separate windows (Frames(?)).

My code is as follows:

package com.company;
public class Form1 extends javax.swing.JFrame {

private javax.swing.JButton btnComboBox;
private javax.swing.JComboBox<String> comboOne;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;

public Form1() {
    initComponents();
}

private void initComponents() {

    jLabel2 = new javax.swing.JLabel();
    comboOne = new javax.swing.JComboBox<>();
    jLabel3 = new javax.swing.JLabel();
    btnComboBox = new javax.swing.JButton();

    jLabel2.setText("SELECT PRINTER:");

    comboOne.setModel(new javax.swing.DefaultComboBoxModel<>(new String[]{"Printer 1", "Printer 2", "Printer 3", "Printer 4"}));

    comboOne.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            comboOneActionPerformed(evt);
        }
    });

    btnComboBox.setText("GO");
    btnComboBox.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            btnComboBoxActionPerformed(evt);
        }
    });

}

private void comboOneActionPerformed(java.awt.event.ActionEvent evt) {

    Object selected = comboOne.getSelectedItem().toString();

}

private void btnComboBoxActionPerformed(java.awt.event.ActionEvent evt) {

    // Opens a new form, converts the object selected in combobox too a string
    // Passes string to openMe on form 2

    Form2 f2 = new Form2(comboOne.getSelectedItem().toString());

}

public static void main(String args[]) {

    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            new Form1().setVisible(true);
        }
    });

}

}

Form 2:

package com.company;

import javax.swing.*;

public class Form2 extends javax.swing.JFrame {

private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel3;
private javax.swing.JScrollPane jScrollPane2;
private javax.swing.JTextArea jta;

public Form2() {
    initComponents();
}
public Form2(String message) {

    initComponents();
    jta.append(" Printer selected: " + message + "\n");
    this.setVisible(true);
}

private void initComponents() {

    jLabel1 = new javax.swing.JLabel();
    jLabel3 = new javax.swing.JLabel();
    jta = new javax.swing.JTextArea();
    jScrollPane2 = new javax.swing.JScrollPane();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

    jLabel1.setText("Console");

    jLabel3.setFont(new java.awt.Font("Tahoma", 1, 11));
    jLabel3.setText("");

    jta.setColumns(20);
    jta.setRows(50);
    jScrollPane2.setViewportView(jta);

}

public void openMe(String message) {

    /*java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            new Form2().setVisible(true);

        }
    });*/

    System.out.println("----- Console Output ------");
    System.out.println("---------------------------");
    System.out.println("Printer selected: " + message);
    System.out.println("---------------------------");
    System.out.println("\n");
    System.out.println("\n");

}
}

Any help appreciated as always

Upvotes: 1

Views: 752

Answers (1)

Sanjeev Saha
Sanjeev Saha

Reputation: 2652

Please make the following changes and see the results:

Step 1:

Insert following lines as last statements of the method private void initComponents() of Form1.java:

private void initComponents() {

  // existing code

  this.setLayout(new FlowLayout(java.awt.FlowLayout.CENTER, 15, 15));       
  this.add(jLabel2);
  this.add(comboOne);
  this.add(btnComboBox);
}

Step 2:

Replace the existing definition by the following one in Form1.java:

private void btnComboBoxActionPerformed(java.awt.event.ActionEvent evt) {
 Form2 f2 = new Form2(comboOne.getSelectedItem().toString());       
 f2.setSize(250, 300);
 f2.setLocationRelativeTo(this);
 f2.setVisible(true);
}

Step 3:

Replace existing main method of Form1.java by following one:

public static void main(String args[]) {

    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            Form1 form1 = new Form1();
                form1.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
                form1.setSize(300, 200);
                form1.setLocationRelativeTo(null);
                form1.setVisible(true);
            }
        });    
}

Step 4:

Search jLabel3.setText("") in Form2.java and replace it by jLabel3.setText("Output").

Step 5:

Insert following lines as last statements of method private void initComponents() of Form2.java:

private void initComponents() {

  // existing code

  this.setLayout(new BorderLayout());       
  JPanel topPanel=new JPanel();
  this.setLayout(new FlowLayout(java.awt.FlowLayout.CENTER, 15, 15));

  this.add(jLabel1);
  this.add(jLabel3);

  this.add(topPanel, BorderLayout.NORTH);
  this.add(jScrollPane2, BorderLayout.CENTER);
}

The output of Form1.java on my system is:

enter image description here

And the output of Form2.java on my system is:

enter image description here

Upvotes: 3

Related Questions