chiapa
chiapa

Reputation: 4412

How can I show Netbeans' output in a form?

I know the output can be saved into a file but I was wondering if the output results from Netbeans can be displayed in a form, inside a textbox or something.

Is it possible?

Thanks,

Chiapa

Upvotes: 0

Views: 2351

Answers (1)

Joffutt4
Joffutt4

Reputation: 1458

This is a very simplified example, but yes. It is possible to have your results be displayed in a form.

What you need to do is create a new form (I used a JFrame Form). From there, I added in a JTextPane to the form.

That should be all that you need on your form, just to display results.

After that, you will just need to set the textPane's text to your results and run the program.

This is what my code looks like.

public class ResultsForm extends javax.swing.JFrame {

/**
 * Creates new form ResultsForm
 */
public ResultsForm() {
    initComponents();
    this.TextArea.setText("No input");
    this.TextArea.setEditable(false);
}

//This is the constructor that takes in your results and places is
//in the form
public ResultsForm(String results){
    initComponents();
    this.TextArea.setText(results);
    this.TextArea.setEditable(false);
}

/**
 * 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")
// <editor-fold defaultstate="collapsed" desc="Generated Code">                          
private void initComponents() {

    jScrollPane2 = new javax.swing.JScrollPane();
    TextArea = new javax.swing.JTextPane();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

    jScrollPane2.setViewportView(TextArea);

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addGap(64, 64, 64)
            .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 292, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addContainerGap(64, Short.MAX_VALUE))
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addGap(60, 60, 60)
            .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 142, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addContainerGap(98, Short.MAX_VALUE))
    );

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

/**
 * @param args the command line arguments
 */
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(ResultsForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
    java.util.logging.Logger.getLogger(ResultsForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
    java.util.logging.Logger.getLogger(ResultsForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
    java.util.logging.Logger.getLogger(ResultsForm.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() {
            //This is where you should place your running code
    String dataToOutput = "This should be on the form";
            //This is where you pass in the results to the form
    new ResultsForm(dataToOutput).setVisible(true);
    }
});
}
    // Variables declaration - do not modify                     
    private javax.swing.JTextPane TextArea;
    private javax.swing.JScrollPane jScrollPane2;
    // End of variables declaration                   
}

Also, here is a link to a very basic tutorial for creating forms within Netbeans: Form Creation Tutorial

Upvotes: 2

Related Questions