Now, on to what I need help with.
I'm currently working on my very first fully function Java Swing GUI Application, using Matisse on NetBeans 5.0.
I am writing your basic "electronic magic 8-ball" GUI, with the goal that the user types in a yes/no question in a JTextField, and clicks a JButton, and the program displays, in a separate JTextPane, an answer randomly chosen from a pool of pre-composed answers.
I have programmed a few basic functions into the GUI, but I can't get the last few bumps rolled out, so to speak. I have a fully functional exit button, and the "Ask..." button (the JButton that's supposed to generate the random answer in the separate JTextPane) has only one answer it can display in the JTextPane. I can't seem to figure out just how to make the JTextPane display randomly selected answers when the "Ask..." JButton is clicked. I can only figure out how to display a single answer.
Here is what I have for code, so far (most of it was automatically generated by NetBeans' Matisse GUI Builder):
/*
* cyberspaceoracleui.java
*
* Created on October 31, 2006, 1:37 PM
*/
package my.cyberspaceoracleui;
/**
*
* @author falador@eastlink.ca
*/
public class cyberspaceoracleui extends javax.swing.JFrame {
/** Creates new form cyberspaceoracleui */
public cyberspaceoracleui() {
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.
*/
//
private void initComponents() {
jLabel1 = new javax.swing.JLabel()
jLabel2 = new javax.swing.JLabel()
jLabel3 = new javax.swing.JLabel()
jTextField1 = new javax.swing.JTextField()
jButton1 = new javax.swing.JButton()
jScrollPane1 = new javax.swing.JScrollPane()
jTextPane1 = new javax.swing.JTextPane()
jButton2 = new javax.swing.JButton()
jLabel4 = new javax.swing.JLabel()
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE)
setTitle("The CyberSpace Oracle")
jLabel1.setText("Greetings, my dear.")
jLabel2.setText("What queries dost thou haveth for me this fine day?")
jLabel3.setText("Please ask only yes/no questions.")
jButton1.setText("Ask...")
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt)
}
})
jTextPane1.setEditable(false)
jScrollPane1.setViewportView(jTextPane1)
jButton2.setText("Farewell")
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt)
}
})
jLabel4.setText("Your answer will be displayed below...")
org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane())
getContentPane().setLayout(layout)
layout.setHorizontalGroup(
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(layout.createSequentialGroup()
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(layout.createSequentialGroup()
.addContainerGap(10, Short.MAX_VALUE)
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(org.jdesktop.layout.GroupLayout.TRAILING, jTextField1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE)
.add(org.jdesktop.layout.GroupLayout.TRAILING, jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE)
.add(org.jdesktop.layout.GroupLayout.TRAILING, jButton2)))
.add(layout.createSequentialGroup()
.addContainerGap()
.add(jLabel4))
.add(layout.createSequentialGroup()
.add(10, 10, 10)
.add(jLabel1))
.add(layout.createSequentialGroup()
.add(10, 10, 10)
.add(jLabel2))
.add(layout.createSequentialGroup()
.add(10, 10, 10)
.add(jLabel3))
.add(layout.createSequentialGroup()
.add(10, 10, 10)
.add(jButton1)))
.addContainerGap())
)
layout.linkSize(new java.awt.Component[] {jButton1, jButton2}, org.jdesktop.layout.GroupLayout.HORIZONTAL)
layout.setVerticalGroup(
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(layout.createSequentialGroup()
.addContainerGap()
.add(jLabel1)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(jLabel2)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(jLabel3)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(jTextField1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(jButton1)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(jLabel4)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 115, Short.MAX_VALUE)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(jButton2)
.addContainerGap())
)
layout.linkSize(new java.awt.Component[] {jButton1, jButton2}, org.jdesktop.layout.GroupLayout.VERTICAL)
pack()
}//
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
jTextField1.setText("")
jTextPane1.setText("I'm sorry, my dear, but I am just not mentally prepared to answer that question, right now. Please ask again later.")
}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
System.exit(0)
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new cyberspaceoracleui().setVisible(true)
}
})
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTextField jTextField1;
private javax.swing.JTextPane jTextPane1;
// End of variables declaration
}
* cyberspaceoracleui.java
*
* Created on October 31, 2006, 1:37 PM
*/
package my.cyberspaceoracleui;
/**
*
* @author falador@eastlink.ca
*/
public class cyberspaceoracleui extends javax.swing.JFrame {
/** Creates new form cyberspaceoracleui */
public cyberspaceoracleui() {
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.
*/
//
private void initComponents() {
jLabel1 = new javax.swing.JLabel()
jLabel2 = new javax.swing.JLabel()
jLabel3 = new javax.swing.JLabel()
jTextField1 = new javax.swing.JTextField()
jButton1 = new javax.swing.JButton()
jScrollPane1 = new javax.swing.JScrollPane()
jTextPane1 = new javax.swing.JTextPane()
jButton2 = new javax.swing.JButton()
jLabel4 = new javax.swing.JLabel()
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE)
setTitle("The CyberSpace Oracle")
jLabel1.setText("Greetings, my dear.")
jLabel2.setText("What queries dost thou haveth for me this fine day?")
jLabel3.setText("Please ask only yes/no questions.")
jButton1.setText("Ask...")
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt)
}
})
jTextPane1.setEditable(false)
jScrollPane1.setViewportView(jTextPane1)
jButton2.setText("Farewell")
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt)
}
})
jLabel4.setText("Your answer will be displayed below...")
org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane())
getContentPane().setLayout(layout)
layout.setHorizontalGroup(
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(layout.createSequentialGroup()
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(layout.createSequentialGroup()
.addContainerGap(10, Short.MAX_VALUE)
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(org.jdesktop.layout.GroupLayout.TRAILING, jTextField1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE)
.add(org.jdesktop.layout.GroupLayout.TRAILING, jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE)
.add(org.jdesktop.layout.GroupLayout.TRAILING, jButton2)))
.add(layout.createSequentialGroup()
.addContainerGap()
.add(jLabel4))
.add(layout.createSequentialGroup()
.add(10, 10, 10)
.add(jLabel1))
.add(layout.createSequentialGroup()
.add(10, 10, 10)
.add(jLabel2))
.add(layout.createSequentialGroup()
.add(10, 10, 10)
.add(jLabel3))
.add(layout.createSequentialGroup()
.add(10, 10, 10)
.add(jButton1)))
.addContainerGap())
)
layout.linkSize(new java.awt.Component[] {jButton1, jButton2}, org.jdesktop.layout.GroupLayout.HORIZONTAL)
layout.setVerticalGroup(
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(layout.createSequentialGroup()
.addContainerGap()
.add(jLabel1)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(jLabel2)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(jLabel3)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(jTextField1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(jButton1)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(jLabel4)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 115, Short.MAX_VALUE)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(jButton2)
.addContainerGap())
)
layout.linkSize(new java.awt.Component[] {jButton1, jButton2}, org.jdesktop.layout.GroupLayout.VERTICAL)
pack()
}//
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
jTextField1.setText("")
jTextPane1.setText("I'm sorry, my dear, but I am just not mentally prepared to answer that question, right now. Please ask again later.")
}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
System.exit(0)
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new cyberspaceoracleui().setVisible(true)
}
})
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTextField jTextField1;
private javax.swing.JTextPane jTextPane1;
// End of variables declaration
}
Here's a preview screenshot of my GUI as it looks so far. (thought it might help you help me).:

Any tips, help or advice you could give me would be greatly appreciated!

