Results 1 to 6 of 6
- 02-25-2012, 12:42 AM #1
Member
- Join Date
- Feb 2012
- Posts
- 66
- Rep Power
- 0
GUI Passing JTextField value to Logic class upon Button Click
Hi all,
I'm attempting to take input from a JTextField and pass it to a logic class upon Button click to do further work using the string entered. To test I created the calculation1 method within my logic class and am calling it from actionPerformed. I want to understand 1. what isn't working here and 2. how to build on the understanding of Java I have to sucessfuly accomplish stated goal. Here's what I have so far:
Java Code:package application; import java.awt.BorderLayout; import java.awt.Container; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.BoxLayout; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JTabbedPane; import javax.swing.JTextField; import javax.swing.event.DocumentEvent; import javax.swing.event.DocumentListener; import java.io.*; //import java.io.IOException; //import java.io.InputStreamReader; import java.util.Properties; public class gui implements ActionListener { JButton btProc = null; JFrame frame = null; JTextField txtTransRec = new JTextField("D:\\MIQ-Transactions\\TransReceived\\", 35); JTextField txtTransHis = new JTextField("X:\\MOCC USER\\U1\\TRANSDATA HISTORY\\", 35); JTextField txtOutput = new JTextField("X:\\MOCC USER\\U2\\JAVA_DEV\\DUP_LOGS\\", 35); JTextField txtDatabase = new JTextField(20); public static void main(String[] args) { gui app = new gui(); app.build(); }//end main class Logic { //private String modifier; public String received; private String history; private String output; public Logic(String txtTransRec, String txtTransHis, String txtOutput) { //modifier = txtTransRec; received = txtTransRec; history = txtTransHis; output = txtOutput; } public void setTransRec(String rec) { received = rec; System.out.println(rec); } public String getTransRec() { return received; } public void setTransHis(String his) { history = his; } public String getTransHis() { return history; } public void setLogOut(String out){ output = out; } public String getLogOut() { return output; } // Some business operations: public void calculation1(String rec) { System.out.println(received); } } public void build(){ final Logic bl = new Logic(null, null, null); // If you want something to happen whenever // a JTextField changes, add this listener: class ModL implements DocumentListener { public void changedUpdate(DocumentEvent e) { } public void insertUpdate(DocumentEvent e) { bl.setTransRec(getValue(txtTransRec)); bl.setTransHis(getValue(txtTransHis)); bl.setLogOut(getValue(txtOutput)); } private String getValue(JTextField txtTransRec) { try { return (txtTransRec.getText()); } catch (NumberFormatException e) { return null; } } public void removeUpdate(DocumentEvent e) { bl.setTransRec(getValue(txtTransRec)); } } JFrame frame = new JFrame("File Check 0.1 Beta"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container contentPane = frame.getContentPane(); //import tab as object & create tabs JTabbedPane tb = new JTabbedPane(); JPanel tbDisp = new JPanel(); JPanel tbConf = new JPanel(); //create labels and text fields for Configure tab JPanel fprec = new JPanel(); JPanel fphis = new JPanel(); JPanel fpServer = new JPanel(); JPanel fpDatabase = new JPanel(); JLabel rec = new JLabel("Trans Rec:"); JLabel his = new JLabel("Trans His:"); JLabel flOutput = new JLabel("Output Loc:"); fprec.add(rec); fprec.add(txtTransRec); fphis.add(his); fphis.add(txtTransHis); fpServer.add(flOutput); fpServer.add(txtOutput); //create Configure tab tb.add(tbConf,"DiffCheck"); //tbDisp.setLayout(new BoxLayout(tbDisp, BoxLayout.Y_AXIS)); //tbConf.setLayout(new BoxLayout(tbConf, BoxLayout.LINE_AXIS)); tbConf.add(fprec); tbConf.add(fphis); tbConf.add(fpServer); tbConf.add(fpDatabase); btProc = new JButton("Process"); btProc.addActionListener(this); tbConf.add(new JButton("Process")); //create "DateCheck" tab tb.add(tbDisp,"DateCheck"); //more to be added later //create frame size, add tabs, and make visible contentPane.add(tb,BorderLayout.CENTER); //frame.getContentPane().add(BorderLayout.EAST, tbConf); frame.setSize(550, 600); frame.setLocationRelativeTo(null); frame.setVisible(true); } //@Override public void actionPerformed(ActionEvent e) { Logic L1 = new Logic(txtTransRec.getText(), txtTransHis.getText(), txtOutput.getText()); L1.calculation1(txtTransRec.getText()); } }//end ComboBox()
Last edited by Redefine12; 02-25-2012 at 12:47 AM.
-
Re: GUI Passing JTextField value to Logic class upon Button Click
So first can you tell us -- what is or isn't working? Also, that's a lot of code. Can you describe a bit what each part is supposed to be doing and what it's not doing? And finally, what is your specific question?
- 02-25-2012, 01:11 AM #3
Member
- Join Date
- Feb 2012
- Posts
- 66
- Rep Power
- 0
Re: GUI Passing JTextField value to Logic class upon Button Click
Sure, here's what I've got:
Gui class contains the Text Fields. I want values entered in these fields to be passed to the Logic class when the "Process" button is clicked. actionPerformed should execute the calculation1 method from the logic class. Currently, calculation1 should just print the JTextField value as a test to see if the values are being passed from the gui to the logic portions. The ModL class should update the text field values when the text strings are manipulated.
The goal here is to create a GUI that accepts JTextField entries, and passes these strings to a seperate class that uses these String values to perform further tasks. If you run the code, you'll see the gui is fairly simple. What I'm struggling with is getting the values passed from the gui into the bit that performs further operations.
-
Re: GUI Passing JTextField value to Logic class upon Button Click
Thanks that helps.
...What I'm struggling with is getting the values passed from the gui into the bit that performs further operations.
- 02-25-2012, 02:44 AM #5
Member
- Join Date
- Feb 2012
- Posts
- 66
- Rep Power
- 0
Re: GUI Passing JTextField value to Logic class upon Button Click
"What exactly isn't working?"
When I call the calculation1 method via my Logic object L1 within the actionPerformed class I'd expect it to print the txtTransRec String to console after clicking the Process JButton. Nothing is printing to console. I don't know how else to test whether the text String values are being passed to the Logic class.
-
Re: GUI Passing JTextField value to Logic class upon Button Click
OK, you seem to have created two buttons that have "Process" as the text. One you add an ActionListener to, the other you don't. Guess which one you add to the GUI?
Java Code:btProc = new JButton("Process"); btProc.addActionListener(this); tbConf.add(new JButton("Process"));
Similar Threads
-
Executing .class files on Windows with a double click or right click
By Norm in forum Forum LobbyReplies: 2Last Post: 06-22-2011, 02:19 PM -
return value on a button click
By wotupduck in forum New To JavaReplies: 1Last Post: 03-24-2011, 02:09 PM -
Button click GUI question
By ZambonieDrivor in forum New To JavaReplies: 2Last Post: 11-29-2010, 08:48 AM -
SaveAs button click
By kasiram.p@gmail.com in forum AWT / SwingReplies: 2Last Post: 07-06-2010, 09:35 AM -
How can I display on Button click?
By ntagrafix in forum New To JavaReplies: 3Last Post: 11-04-2009, 01:05 AM
Bookmarks