Results 1 to 4 of 4
Thread: Passing data between 3 classes
- 04-10-2011, 07:13 PM #1
Member
- Join Date
- Apr 2011
- Posts
- 3
- Rep Power
- 0
Passing data between 3 classes
This is one example that I've made. But I fail to transmit data from the class InputTekst over the class Kontrolor to class GUI.
I need help, or some advice.
Java Code:public static void main(String[] args) { Kontrolor kontrola = new Kontrolor(); GUI gui1 = new GUI(kontrola); } } public class GUI { JTextField tfTekst2; JButton bAdd; JPanel windowContent; JFrame frame; Kontrolor Kot; String tekst; public GUI (final Kontrolor Kot) { windowContent = new JPanel(); FlowLayout fl1 = new FlowLayout(); windowContent.setLayout(fl1); tfTekst2 = new JTextField(30); bAdd = new JButton("Add"); windowContent.add(tfTekst2); windowContent.add(bAdd); bAdd.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { Kot.doAdd(); tfTekst2.setText(InputTekst.showInputTekst()); } }); frame = new JFrame(); frame.setContentPane(windowContent); frame.pack(); frame.setVisible(true); } } public class Kontrolor { public Kontrolor () { } public void doAdd() { InputTekst iTekst = new InputTekst(); iTekst.setVisible(true); } } public class InputTekst extends JDialog { JTextField tf1; JButton bOk; boolean ok; public InputTekst() { setLayout(new FlowLayout()); tf1 = new JTextField(20); add(tf1); bOk = new JButton("OK"); add(bOk); pack(); bOk.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent event) { ok = true; setVisible(false); } }); } public String showInputTekst() { String displayTekst = tf1.getText(); return displayTekst; } }
-
Let me offer you a couple of suggestions:
Your JDialog would be better off being called as a Modal JDialog since this way you'll when the user is done using it since program flow will begin once again in the code that called the dialog right after the call on the dialog to setVisible(true). Then you can extract the dialog's text from its JTextField. To make this happen, you will need to pass the main JFrame into the JDialog's constructor. So your main GUI will need an accessor method for the JFrame field something like:
and the JDialog class will need a constructor that accepts a GUI object. Something like:Java Code:public JFrame getFrame() { return frame; }
Java Code:class InputTekst extends JDialog { JTextField tf1; JButton bOk; boolean ok; public InputTekst(GUI gui) { super(gui.getFrame(), "Input Tekst", true); // to create a modal JDialog
I'd also give the GUI class a public method to allow the controller to set the JTextField's text. Something like:
Java Code:public void tfTekstSetTekst(String text) { tfTekst2.setText(text); }
The control class will need access to both the GUI and the InputTekst dialog, and I'd do this with mutator methods:
Java Code:class Kontrolor { private InputTekst iTekst; private GUI gui; public void setInputTekst(InputTekst inputTekst) { this.iTekst = inputTekst; } public void setGui(GUI gui) { this.gui = gui; }
Then the controller can do it all in its doAdd method:
Java Code:public void doAdd() { if (gui == null || iTekst == null) { return; } iTekst.setVisible(true); // displays the modal JDialog. // code stops here until the JDialog is no longer visible // Now the dialog has been dealt with and we can extract the text String text = iTekst.getInputTekst(); gui.tfTekstSetTekst(text); }
So putting it all together, it would look something like so:
Java Code:import java.awt.*; import java.awt.event.*; import javax.swing.*; public class TestGUI { public static void main(String[] args) { Kontrolor kontrola = new Kontrolor(); GUI gui = new GUI(); InputTekst inputTekst = new InputTekst(gui); gui.setKontrolor(kontrola); kontrola.setGui(gui); kontrola.setInputTekst(inputTekst); } } class GUI { JTextField tfTekst2; JButton bAdd; JPanel windowContent; JFrame frame; String tekst; public GUI() { windowContent = new JPanel(); FlowLayout fl1 = new FlowLayout(); windowContent.setLayout(fl1); tfTekst2 = new JTextField(30); tfTekst2.setEditable(false); tfTekst2.setFocusable(false); bAdd = new JButton("Add"); windowContent.add(tfTekst2); windowContent.add(bAdd); frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setContentPane(windowContent); frame.pack(); frame.setVisible(true); } public void setKontrolor(final Kontrolor kot) { bAdd.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { if (kot != null) { kot.doAdd(); } } }); } public void tfTekstSetTekst(String text) { tfTekst2.setText(text); } public JFrame getFrame() { return frame; } } class Kontrolor { private InputTekst iTekst; private GUI gui; public void setInputTekst(InputTekst inputTekst) { this.iTekst = inputTekst; } public void setGui(GUI gui) { this.gui = gui; } public void doAdd() { if (gui == null || iTekst == null) { return; } iTekst.setVisible(true); // displays the modal JDialog. // code stops here until the JDialog is no longer visible // Now the dialog has been dealt with and we can extract the text String text = iTekst.getInputTekst(); gui.tfTekstSetTekst(text); } } @SuppressWarnings("serial") class InputTekst extends JDialog { JTextField tf1; JButton bOk; boolean ok; public InputTekst(GUI gui) { super(gui.getFrame(), "Input Tekst", true); setLayout(new FlowLayout()); tf1 = new JTextField(20); add(tf1); bOk = new JButton("OK"); add(bOk); pack(); bOk.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent event) { ok = true; setVisible(false); } }); } public String getInputTekst() { String displayTekst = tf1.getText(); return displayTekst; } }
- 04-11-2011, 08:28 PM #3
Member
- Join Date
- Apr 2011
- Posts
- 3
- Rep Power
- 0
Thanks, you really helped me, not only with the code but also with helpful suggestions.
Thanks again!
-
Similar Threads
-
Passing objects to classes
By Catfish1 in forum New To JavaReplies: 3Last Post: 11-30-2010, 05:27 PM -
passing an array between classes
By gisler in forum New To JavaReplies: 10Last Post: 04-10-2009, 10:31 PM -
help with passing objects between classes
By aruna1 in forum New To JavaReplies: 7Last Post: 03-22-2009, 02:41 PM -
Passing values between classes problem.
By alin_ms in forum New To JavaReplies: 8Last Post: 12-12-2008, 06:49 PM -
Need help passing data between classes
By bri1547 in forum New To JavaReplies: 3Last Post: 07-21-2008, 04:19 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks