returning string, boolean and integer values using jtextfield and jcheckbox
Author: Paul |
07/18/09 23:50 |
Reply |
Hello,
The GUI i am making is a subclass and needs to return values to the main class after a run button is pressed. I am wondering if anyone would be helpful enough to post some code snippets of how i would implement actionlisteners for this.
The values needed are :
boolean useFood = false; // needs to be true when jcheckbox is ticked.
int withdrawAmmount; // jtextfield will determine this value.
static String foodType; // jtextfield will determine this string.
The code i have at the moment:
import java.awt.Component;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.WindowConstants;
import org.jdesktop.layout.GroupLayout;
import org.jdesktop.layout.LayoutStyle;
import javax.swing.SwingUtilities;
/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit http://www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class scriptGUI extends javax.swing.JFrame {
private JButton runButton;
private JTextField foodName;
private AbstractAction startAction;
private JLabel withdrawLabel;
private JTextField jTextField1;
private JLabel FoodLabel;
private JCheckBox useFood;
/**
* Auto-generated main method to display this JFrame
*/
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
NewJFrame inst = new NewJFrame();
inst.setLocationRelativeTo(null);
inst.setVisible(true);
}
});
}
public scriptGUI() {
super();
initGUI();
}
private void initGUI() {
try {
GroupLayout thisLayout = new GroupLayout((JComponent)getContentPane());
getContentPane().setLayout(thisLayout);
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
{
runButton = new JButton();
runButton.setText("Start");
runButton.setAction(getStartAction());
}
{
foodName = new JTextField();
}
{
useFood = new JCheckBox();
useFood.setText("Use Food?");
}
{
FoodLabel = new JLabel();
FoodLabel.setText("Food Name");
}
{
jTextField1 = new JTextField();
}
{
withdrawLabel = new JLabel();
withdrawLabel.setText("withdraw X ?");
}
thisLayout.setVerticalGroup(thisLayout.createSequentialGroup()
.add(0, 8, Short.MAX_VALUE)
.add(useFood, GroupLayout.PREFERRED_SIZE, 19, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(LayoutStyle.RELATED)
.add(withdrawLabel, GroupLayout.PREFERRED_SIZE, 17, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(LayoutStyle.RELATED)
.add(jTextField1, GroupLayout.PREFERRED_SIZE, 25, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(LayoutStyle.RELATED)
.add(FoodLabel, GroupLayout.PREFERRED_SIZE, 11, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(LayoutStyle.RELATED)
.add(foodName, GroupLayout.PREFERRED_SIZE, 25, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(LayoutStyle.RELATED)
.add(runButton, GroupLayout.PREFERRED_SIZE, 22, GroupLayout.PREFERRED_SIZE)
.addContainerGap());
thisLayout.setHorizontalGroup(thisLayout.createSequentialGroup()
.addContainerGap(47, 47)
.add(thisLayout.createParallelGroup()
.add(GroupLayout.LEADING, useFood, 0, 156, Short.MAX_VALUE)
.add(GroupLayout.LEADING, runButton, 0, 156, Short.MAX_VALUE)
.add(thisLayout.createSequentialGroup()
.add(35)
.add(thisLayout.createParallelGroup()
.add(GroupLayout.LEADING, thisLayout.createSequentialGroup()
.add(jTextField1, GroupLayout.PREFERRED_SIZE, 85, GroupLayout.PREFERRED_SIZE)
.add(0, 11, Short.MAX_VALUE))
.add(GroupLayout.LEADING, thisLayout.createSequentialGroup()
.add(foodName, 0, 85, Short.MAX_VALUE)
.add(11))
.add(thisLayout.createSequentialGroup()
.addPreferredGap(jTextField1, withdrawLabel, LayoutStyle.INDENT)
.add(thisLayout.createParallelGroup()
.add(GroupLayout.LEADING, thisLayout.createSequentialGroup()
.add(withdrawLabel, 0, 87, Short.MAX_VALUE)
.addPreferredGap(LayoutStyle.RELATED))
.add(GroupLayout.LEADING, FoodLabel, 0, 90, Short.MAX_VALUE))))
.add(25)))
.addContainerGap(34, 34));
thisLayout.linkSize(new Component[] {foodName, jTextField1}, GroupLayout.VERTICAL);
thisLayout.linkSize(new Component[] {foodName, jTextField1}, GroupLayout.HORIZONTAL);
pack();
} catch (Exception e) {
e.printStackTrace();
}
}
private AbstractAction getStartAction() {
if(startAction == null) {
startAction = new AbstractAction("Start", null) {
public void actionPerformed(ActionEvent evt) {
//close and intitialise booleans + ints
}
};
}
return startAction;
}
}
|
|