Digital Sauce
Digital Sauce

Reputation: 5

My jFrame is not appearing

Bellow is my code. Fort some reason my jFrame will not appear. Has anyone appearance this before? Dose it have anything to do with java.awt.EventQueue.invokeLater(new Runnable() I'm new to java and the GUI reaching out to InvokeLater seems like it would prevent it from loading at all because I can't see anything that would then tell it to load later.

    package accountBuilderGUI;


import java.io.File;
import java.io.IOException;

import jxl.Cell;
import jxl.CellType;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;


public class accountBuilderGUI extends javax.swing.JFrame {

/**
 * Creates new form accountBuilderGUI
 */
public accountBuilderGUI() {
    initComponents();
}

/**
 * This method is called from within the constructor to initialize the form.
 * WARNING: Do NOT modify this code. The content of this method is always
 * regenerated by the Form Editor.
 */
@SuppressWarnings("unchecked")
public class ReadExcel {

private String inputFile;

public void setInputFile(String inputFile) {
this.inputFile = inputFile;
}

public void read() throws IOException  {
File inputWorkbook = new File(inputFile);
Workbook w;
try {
  w = Workbook.getWorkbook(inputWorkbook);
  // Get the first sheet
  Sheet sheet = w.getSheet(0);
  // Loop over first 10 column and lines

  for (int j = 0; j < sheet.getColumns(); j++) {
    for (int i = 0; i < sheet.getRows(); i++) {
      Cell cell = sheet.getCell(j, i);
      CellType type = cell.getType();
      if (type == CellType.LABEL) {
        System.out.println("I got a label "
            + cell.getContents());
      }

      if (type == CellType.NUMBER) {
        System.out.println("I got a number "
            + cell.getContents());
      }

    }
  }
} catch (BiffException e) {
  e.printStackTrace();
}
}
}
// <editor-fold defaultstate="collapsed" desc="Generated Code">                          
private void initComponents() {

    welcomeMessage = new javax.swing.JLabel();
    excelFilePath = new javax.swing.JTextField();
    Start = new javax.swing.JButton();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    setTitle("accountBuilderGUI");

    welcomeMessage.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
    welcomeMessage.setText("Welcome to Blackburns time clock account creater!");

    excelFilePath.setText("Please enter the file path to the excell document.");
    excelFilePath.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            excelFilePathActionPerformed(evt);
        }
    });

    Start.setText("Start");
    Start.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            StartActionPerformed(evt);
        }
    });

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(excelFilePath, javax.swing.GroupLayout.PREFERRED_SIZE, 461, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGroup(layout.createSequentialGroup()
                    .addGap(193, 193, 193)
                    .addComponent(Start))
                .addGroup(layout.createSequentialGroup()
                    .addGap(38, 38, 38)
                    .addComponent(welcomeMessage)))
            .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addContainerGap()
            .addComponent(welcomeMessage)
            .addGap(18, 18, 18)
            .addComponent(excelFilePath, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addGap(18, 18, 18)
            .addComponent(Start)
            .addContainerGap(28, Short.MAX_VALUE))
    );

    pack();
}// </editor-fold>                        

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

}                                             

private void StartActionPerformed(java.awt.event.ActionEvent evt) {                                      
     String filePathToExcel = excelFilePath.getText();
     ReadExcel test = new ReadExcel();
     test.setInputFile(filePathToExcel);

     try{
     test.read();
     }catch(IOException e){
     e.printStackTrace();
     }
}                                     

public static void main(String args[]) {
    /* Set the Nimbus look and feel */
    //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
    /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
     * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
     */
    try {
        for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
            if ("Nimbus".equals(info.getName())) {
                javax.swing.UIManager.setLookAndFeel(info.getClassName());
                break;
            }
        }
    } catch (ClassNotFoundException ex) {
        java.util.logging.Logger.getLogger(accountBuilderGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (InstantiationException ex) {
        java.util.logging.Logger.getLogger(accountBuilderGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (IllegalAccessException ex) {
        java.util.logging.Logger.getLogger(accountBuilderGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (javax.swing.UnsupportedLookAndFeelException ex) {
        java.util.logging.Logger.getLogger(accountBuilderGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    }
    //</editor-fold>

    /* Create and display the form */
    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            new accountBuilderGUI().setVisible(true);
        }
    });
}
// Variables declaration - do not modify                     
private javax.swing.JButton Start;
private javax.swing.JTextField excelFilePath;
private javax.swing.JLabel welcomeMessage;
// End of variables declaration                   
}

Upvotes: 0

Views: 161

Answers (1)

UFL1138
UFL1138

Reputation: 642

This runs fine on my Windows 7/Java 7 machine. I downloaded the jxl library and added it to my build path, and it runs the window as expected.

Per the EventQueue documentation:

public static void invokeLater(Runnable runnable)
Causes runnable to have its run method called in the dispatch thread of the system EventQueue. This will happen after all pending events are processed.

So you don't need to call it "later"; it will be run automatically and in the order that Runnables are added to the Event Queue.

Upvotes: 1

Related Questions