User not logged in - login - register
Home Calendar Books School Tool Photo Gallery Message Boards Users Statistics Advertise Site Info
go to bottom | |
 Message Boards » » Java Question Page [1]  
BlackSheep
Suspended
1575 Posts
user info
edit post

Hey guys,

I'm having a bit of a problem.

I have an application that is using a main frame1 and a second frame2 that is called from frame1.

I need to set a textBox in Frame1 based on input from Frame2 but i can't figure out how to do it.

any help would be appreciated.

Quote :
"
public static void main(String args[]) {

public void run() {

new Frame1().setVisible(true);
}

}


"


Quote :
"
public class Frame1 extends javax.swing.JFrame implements Observer{


/** Creates new form UI */

public Frame1() {
initComponents();
}

.
.
.
private void findVIPBtnActionPerformed(java.awt.event.ActionEvent evt) {
Frame2 myFrame2=new Frame2();

.
.
.
private void methodToGet VariableA from Frame2(){
//This is what I'm having trouble with
}

}

"


Quote :
"
public class Frame2 extends javax.swing.JFrame{
.
.
.
public Frame2() {
initComponents();
}

.
.
.
private void configBtnActionPerformed(java.awt.event.ActionEvent evt) {
set a VarableA
this.dispose();

"

4/7/2011 1:30:47 PM

lewisje
All American
9196 Posts
user info
edit post

Is VariableA declared to be public, or does it have a public "getter" method (the better design pattern)?

If it is, you should be able to access it, ideally by calling that "getter" method:

public class Frame1 extends javax.swing.JFrame implements Observer{
//stuff
Frame2 myFrame2 = new Frame2();
//more stuff
private void methodNeedingVariableA(){
//stuff
a = myFrame2.getA();
//more stuff
}
//still more stuff
}
//...
public class Frame2 extends javax.swing.JFrame{
//stuff
private string variableA;
//more stuff
public string getA(){return variableA;}
//still more stuff
}
also plz use the code tags rather than quote

[Edited on April 7, 2011 at 1:45 PM. Reason : stuff

4/7/2011 1:44:43 PM

BlackSheep
Suspended
1575 Posts
user info
edit post

The problem I'm having is that I want to get the variable from Frame2 only if the config button is hit (which also disposes of Frame2).


In Frame1 I have this code:


private void findVIPBtnActionPerformed(java.awt.event.ActionEvent evt) {

Frame2 myFrame2=new Frame2();

this.vipAddressField.setText(myFrame2.returnVariableA().toString());
.
.


The problem I'm having is that the textField is being set before the config button is hit. How do I fix?

4/7/2011 2:12:39 PM

Calrizzian
Starting Lineup
53 Posts
user info
edit post

It's been a few years since I've used swing so there are probably better answers but here goes.


Frame2 myFrame2=new Frame2();

this.vipAddressField.setText(myFrame2.returnVariableA().toString());


You're creating a Frame2() object and assigning it to myFrame2. Now myFrame2 is a hidden JFrame window waiting to be used but not attached to any other object. When you call myFrame2.returnVariableA().toString(), you're getting the default value prior to and independent of any action that is happening in Frame2. The way you have your code setup, you would need to wait for Frame2() to do it's job before trying to get the value of returnVariableA(). To make this easy, you should be using a JDialog to get user input from a separate window.

http://download.oracle.com/javase/tutorial/uiswing/components/dialog.html


OR

Building off of lewisje's answer, you could overload the Frame2 constructor to accept a JFrame object as its "parent frame"
.
<PSEUDOCODE>

class Frame1{
private variable_set_by_frame2;
JFrame myFrame2 = new Frame2(this);

public set_function_used_by_frame2(someVar){
variable_set_by_frame2 = someVar;
}
}

class Frame2{
public Frame2(JFrame parentFrame){
myParentFrame = parentFrame;
}

private void configBtnActionPerformed(java.awt.event.ActionEvent evt) {
myParentFrame.set_function_used_by_frame2(VariableA);
this.dispose();
}
}

4/8/2011 12:04:58 AM

 Message Boards » Tech Talk » Java Question Page [1]  
go to top | |
Admin Options : move topic | lock topic

© 2024 by The Wolf Web - All Rights Reserved.
The material located at this site is not endorsed, sponsored or provided by or on behalf of North Carolina State University.
Powered by CrazyWeb v2.38 - our disclaimer.