Ping Java Peeps

P

PerfectReign

Anybody want to help out with this one?

I have two JFrames. I want to update a control on one with the information
from another. The first jframe launches the second. the second has a text
box. I then want the results of the text box applied to a JLabel on the
first.

Ideas?

Here's the code:

http://www.donutmonster.com/viewtopic.php?p=43#43
 
S

Sabine Dinis Blochberger

PerfectReign said:
Anybody want to help out with this one?

I have two JFrames. I want to update a control on one with the information
from another. The first jframe launches the second. the second has a text
box. I then want the results of the text box applied to a JLabel on the
first.

Ideas?

Here's the code:

http://www.donutmonster.com/viewtopic.php?p=43#43
Without looking at the code, you can override the constructor of frame2
to include a JFrame as a parameter.
 
R

RedGrittyBrick

PerfectReign said:
Anybody want to help out with this one?

Why set followups to alt.2600?
Why set followups to alt.2600 only?

Seen & replied to in comp.lang.java.programmer
I have two JFrames. I want to update a control on one with the information
from another. The first jframe launches the second. the second has a text
box. I then want the results of the text box applied to a JLabel on the
first.

Ideas?

Here's the code:

http://www.donutmonster.com/viewtopic.php?p=43#43


In frame1.java change
frame2 frametwo = new frame2();
to
frame2 frametwo = new frame2(this);

in the ActionListener add
if (e.getActionCommand().equals("Close")) {
if (e.getSource() instanceof JButton) {
txtOne.setText(((JButton)e.getSource()).getText());
}
}


In frame2.java change
public frame2(){
to
public frame2(ActionListener listener){

change
button2.addActionListener(this);
to
button2.addActionListener(listener);


Above coding style based on that used in OP's source :-(
UNTESTED - CAVEAT EMPTOR

There's probably better ways to do it and there's lots of other
improvements I think you could and should make to your code. For
example, you could pass an Action instead of an ActionListener, this
would make the code a bit cleaner. I hope the above helps you get started.
 
P

PerfectReign

Why set followups to alt.2600?
Why set followups to alt.2600 only?

My newsreader (Knode) did that. Also, if I don't check c.l.j.p in a few
months, I'll see the reply in a.2600.
Seen & replied to in comp.lang.java.programmer



In frame1.java change
frame2 frametwo = new frame2();
to
frame2 frametwo = new frame2(this);

in the ActionListener add
if (e.getActionCommand().equals("Close")) {
if (e.getSource() instanceof JButton) {
txtOne.setText(((JButton)e.getSource()).getText());
}
}


In frame2.java change
public frame2(){
to
public frame2(ActionListener listener){

change
button2.addActionListener(this);
to
button2.addActionListener(listener);


Above coding style based on that used in OP's source :-(
UNTESTED - CAVEAT EMPTOR

There's probably better ways to do it and there's lots of other
improvements I think you could and should make to your code. For
example, you could pass an Action instead of an ActionListener, this
would make the code a bit cleaner. I hope the above helps you get started.

Thanks! Will get started.
 
P

PerfectReign

In frame2.java change
public frame2(){
to
public frame2(ActionListener listener){

change
button2.addActionListener(this);
to
button2.addActionListener(listener);


Above coding style based on that used in OP's source :-(
UNTESTED - CAVEAT EMPTOR

There's probably better ways to do it and there's lots of other
improvements I think you could and should make to your code. For
example, you could pass an Action instead of an ActionListener, this
would make the code a bit cleaner. I hope the above helps you get started.

Thank you - I did this and ended up with something bizarre.

http://donutmonster.com/stuff/2007/20070913_java_frames.jpg

The first frame is loaded twice.

Of course, then the button on the second frame doesn't seem to work.

Ideas??

I'll post the code inline, since the classes are not that long.

frame1.java

import javax.swing.*;
import java.awt.*;
import javax.swing.border.LineBorder;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;


public class frame1 extends JFrame implements ActionListener{

frame2 frametwo = new frame2(this);

private static frame1 mainForm = new frame1();

public static void main(String args[]){
new frame1();

}

public frame1(){

frametwo.setVisible(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container content = getContentPane();
content.setBackground(Color.white);
content.setLayout(new FlowLayout());
JButton button1 = new JButton("Click Me");
button1.addActionListener(this);


JLabel lblOne = new JLabel(" ");

lblOne.setBorder(new LineBorder(Color.green, 3));

content.add(lblOne);

content.add(button1);

JButton button2 = new JButton("Exit");
button2.addActionListener( this );
content.add(button2);
this.setSize(300, 300);

setVisible(true);

}

public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("Exit")) {


System.exit(0);
}

if (e.getActionCommand().equals("Click Me")) {

frametwo.setVisible(true);
}
}


public frame1 getMainForm() {
return mainForm;
}
}

------------------------------------------------------------


frame2.java

/*
Java test for multi-class update.

This is the second frame called from the first.
*/

import javax.swing.*;
import java.awt.*;
import javax.swing.border.LineBorder;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;


public class frame2 extends JFrame implements ActionListener{

JTextField txtOne = new JTextField(20);
static ActionListener listener; // = new ActionListener();


public static void main(String args[]){
new frame2( listener );


}

public frame2( ActionListener listener ){

setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
Container content = getContentPane();
content.setLayout(new FlowLayout());





content.add(txtOne);



JButton button2 = new JButton("Close");
//button2.addActionListener(new ButtonListener());
//button2.addActionListener(this);
button2.addActionListener(listener);

content.add(button2);


this.setSize(300, 300);
setVisible(true);

}


public void actionPerformed(ActionEvent e) {
//if (e.getActionCommand().equals("Close")) {

String blah = this.txtOne.getText();

System.out.println(blah);
// frame1.lblOne.setText(blah);
setVisible(false);
}

/* public Form1 getFrame1(){
return this.frame1;
}
public void setFrame1(Form1 frame1){
this.frame1 = frame1;
}
*/

}
 
A

Andrew Thompson

PerfectReign said:
...
public class frame1 extends JFrame implements ActionListener{

frame2 frametwo = new frame2(this);

private static frame1 mainForm = new frame1();

..remove this reference to an instance of frame1, then..
...
public frame1 getMainForm() {

..remove this line, and replace it with..
return mainForm;

..the following.

// we just need to return the reference to 'this'
return this;
...

Note that using the usual nomenclature helps people to
understand the code, and thereby help you. Common
nomenclature would suggest the names of the classes
should be Frame1 and Frame2, though in any non-test
situation, I am hoping you might come up with better
names than '1' or '2'.

HTH

--
Andrew Thompson
http://www.athompson.info/andrew/

Message posted via JavaKB.com
http://www.javakb.com/Uwe/Forums.aspx/java-general/200709/1
 
R

RedGrittyBrick

PerfectReign said:
http://donutmonster.com/stuff/2007/20070913_java_frames.jpg

The first frame is loaded twice.

Of course, then the button on the second frame doesn't seem to work.

Ideas??

I'll post the code inline, since the classes are not that long.

frame1.java

import javax.swing.*;
import java.awt.*;
import javax.swing.border.LineBorder;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;


public class frame1 extends JFrame implements ActionListener{

frame2 frametwo = new frame2(this);

private static frame1 mainForm = new frame1();

In the above line you create your first instance of class frame1. You
don't need mainForm.
Remove the above line
public static void main(String args[]){
new frame1();

In the above line you create your *second* instance of class frame1.
The fact you now have two instances of frame1 should be no surprise.
}

public frame1(){

frametwo.setVisible(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container content = getContentPane();
content.setBackground(Color.white);
content.setLayout(new FlowLayout());
JButton button1 = new JButton("Click Me");
button1.addActionListener(this);


JLabel lblOne = new JLabel(" ");

lblOne.setBorder(new LineBorder(Color.green, 3));

content.add(lblOne);

content.add(button1);

JButton button2 = new JButton("Exit");
button2.addActionListener( this );
content.add(button2);
this.setSize(300, 300);

setVisible(true);

}

public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("Exit")) {


System.exit(0);
}

if (e.getActionCommand().equals("Click Me")) {

frametwo.setVisible(true);
}

Missing:
if (e.getActionCommand().equals("Close")) {
lblOne.setText(((JTextField)getSource).getText());
}


Your IDE should tell you the following method is never used. I;d remove
it, its just a pointless confusing distraction to clutter your example
with unused methods.
public frame1 getMainForm() {
return mainForm;
}

}

------------------------------------------------------------


frame2.java

/*
Java test for multi-class update.

This is the second frame called from the first.
*/

import javax.swing.*;
import java.awt.*;
import javax.swing.border.LineBorder;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;


public class frame2 extends JFrame implements ActionListener{

public class frame2 extends JFrame {

JTextField txtOne = new JTextField(20);
static ActionListener listener; // = new ActionListener();

remove that line
// static ActionListener listener; // = new ActionListener();
public static void main(String args[]){
new frame2( listener );


}

public frame2( ActionListener listener ){

setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
Container content = getContentPane();
content.setLayout(new FlowLayout());





content.add(txtOne);



JButton button2 = new JButton("Close");
//button2.addActionListener(new ButtonListener());
//button2.addActionListener(this);
button2.addActionListener(listener);

content.add(button2);


this.setSize(300, 300);
setVisible(true);

}

Remove the following method ...
/*
public void actionPerformed(ActionEvent e) {
//if (e.getActionCommand().equals("Close")) {

String blah = this.txtOne.getText();

System.out.println(blah);
// frame1.lblOne.setText(blah);
setVisible(false);
}

*/
.... up to here

/* public Form1 getFrame1(){
return this.frame1;
}
public void setFrame1(Form1 frame1){
this.frame1 = frame1;
}
*/

}


Other notes.

When posting code, just remove any commented out code. It doesn't help
us understand your current problem (I understand why you comment bits
out, I do it too, but I clean it out when posting).

Please follow usual Java conventions when writing code for posting. It
makes it easier for people like me to speed-read code if your classes
have names starting with a capital letter. "frame1" looks like an
instance name. I'd write "Frame1" instead.

Generally when comparing a String variable to a String constant it is
usually safer (from NPE) to write it the other way around:
if ("Close".equals(e.getActionCommand()))

It probably doesn't matter, but if I intend to use the results of
e.getActionCommand() more than once, I always do
String command = e.getActionCommand() and use the command variable.

It is useful to create contants to be used as ActionCommands. There is
less chance of having "Close" in one place and "close" in another.
1)
public static final String CLOSE = "Close";
....
new JButton(CLOSE);
....
if (CLOSE.equals(command)) { ...

2) Better
Use an enum

3) Best?
Use Actions

I know its a concocted example but I find variable names like lblone and
blah make comprehension more difficult. I'd invent something based on
some typical teaching example like recording names and addresses, or
students and courses, or shops and products, ...


Hope that helps
 
P

PerfectReign

.remove this reference to an instance of frame1, then..
..

.remove this line, and replace it with..


.the following.

// we just need to return the reference to 'this'
return this;
..

Note that using the usual nomenclature helps people to
understand the code, and thereby help you.

I hope I was using that.
Common
nomenclature would suggest the names of the classes
should be Frame1 and Frame2, though in any non-test
situation, I am hoping you might come up with better
names than '1' or '2'.

Heh - the actual names of the classes I'm trying to figure out how to
integrate are standard names: frmMain, frmOptions, frmGroups, clsData,
clsNews...

....names I've been using since the early '90s when I started doing OO with
Dataflex then VB. :p

these two classes are *very* simplistic so I can ensure the issue is taken
care of in the real project. I don't want to confuse anyone with real
code.

Thanks! I'll try this out...
 
L

Lew

RedGrittyBrick said:
Generally when comparing a String variable to a String constant it is
usually safer (from NPE) to write it the other way around:
if ("Close".equals(e.getActionCommand()))

The only problem with that approach is that it removes null as an out-of-band
value and makes it equivalent to, say, "". If the null-ness actually
mattered, you would check for the value being null first, then compare to the
test value.

Or more generally you'd write
String command = e.getActionCommand();
if ( command != null && command.equals( "Close" ))

This lets you treat null as in-band for now but easily change to out-of-band
later, and maintains the code's self-documentary quality in explicitly showing
that you didn't ignore null.

Remember, ! "".equals( null ).
 
P

PerfectReign

In the above line you create your first instance of class frame1. You
don't need mainForm.
Remove the above line

Yep! I eventually figured that one out.

(Not bad for a manager, IMO!)


Missing:
if (e.getActionCommand().equals("Close")) {
lblOne.setText(((JTextField)getSource).getText());
}

Okay, will add and see how it goes.
Your IDE should tell you the following method is never used

I'm not using an IDE. Just the text editor, Kate. It has java syntax
highlighting.



Remove the following method ...

Okay, will do.


Other notes.

When posting code, just remove any commented out code.

Okay, will do. My apologies.

Please follow usual Java conventions when writing code for posting. It
makes it easier for people like me to speed-read code if your classes
have names starting with a capital letter. "frame1" looks like an
instance name. I'd write "Frame1" instead.

Will do. I realize now my class names don't follow Java conventions.

Generally when comparing a String variable to a String constant it is
usually safer (from NPE) to write it the other way around:
if ("Close".equals(e.getActionCommand()))

It probably doesn't matter, but if I intend to use the results of
e.getActionCommand() more than once, I always do
String command = e.getActionCommand() and use the command variable.

It is useful to create contants to be used as ActionCommands. There is
less chance of having "Close" in one place and "close" in another.
1)
public static final String CLOSE = "Close";
...
new JButton(CLOSE);
...
if (CLOSE.equals(command)) { ...

2) Better
Use an enum

3) Best?
Use Actions

I know its a concocted example but I find variable names like lblone and
blah make comprehension more difficult. I'd invent something based on
some typical teaching example like recording names and addresses, or
students and courses, or shops and products, ...


Hope that helps

Yes, it does. Thank you. (Actually lblOne is designed to show it is a
label. I always use the prefix in my variable in my real coding.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top