Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Ping Java Peeps

Reply
Thread Tools

Ping Java Peeps

 
 
PerfectReign
Guest
Posts: n/a
 
      09-11-2007
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


--
www.perfectreign.com
 
Reply With Quote
 
 
 
 
Sabine Dinis Blochberger
Guest
Posts: n/a
 
      09-12-2007
PerfectReign wrote:

> 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.


--
Sabine Dinis Blochberger

Op3racional
www.op3racional.eu
 
Reply With Quote
 
 
 
 
RedGrittyBrick
Guest
Posts: n/a
 
      09-12-2007
PerfectReign wrote:
> 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.
 
Reply With Quote
 
PerfectReign
Guest
Posts: n/a
 
      09-12-2007
on Wednesday 12 September 2007 03:17 am, someone posing as RedGrittyBrick
took a rock and etched into the cave:

> PerfectReign wrote:
>> Anybody want to help out with this one?

>
> 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
>
>>
>> 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.


Thanks! Will get started.



--
www.perfectreign.com
 
Reply With Quote
 
PerfectReign
Guest
Posts: n/a
 
      09-13-2007
on Wednesday 12 September 2007 03:17 am, someone posing as RedGrittyBrick
took a rock and etched into the cave:



> 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/2...ava_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_CLOS E);
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;
}
*/

}
--
www.perfectreign.com
 
Reply With Quote
 
Andrew Thompson
Guest
Posts: n/a
 
      09-14-2007
PerfectReign wrote:
>on Wednesday 12 September 2007 03:17 am, someone posing as RedGrittyBrick


>frame1.java

...
>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.asp...neral/200709/1

 
Reply With Quote
 
RedGrittyBrick
Guest
Posts: n/a
 
      09-14-2007
PerfectReign wrote:
>
> http://donutmonster.com/stuff/2007/2...ava_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_CLOS E);
> 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


 
Reply With Quote
 
PerfectReign
Guest
Posts: n/a
 
      09-14-2007
on Thursday 13 September 2007 11:37 pm, someone posing as Andrew Thompson
took a rock and etched into the cave:

> PerfectReign wrote:
>>on Wednesday 12 September 2007 03:17 am, someone posing as RedGrittyBrick

>
>>frame1.java

> ..
>>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.


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.

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...


>
> HTH
>


--
www.perfectreign.com
 
Reply With Quote
 
Lew
Guest
Posts: n/a
 
      09-14-2007
RedGrittyBrick wrote:
> 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 ).

--
Lew
 
Reply With Quote
 
PerfectReign
Guest
Posts: n/a
 
      09-14-2007
on Friday 14 September 2007 02:47 am, someone posing as RedGrittyBrick took
a rock and etched into the cave:

> PerfectReign wrote:
>>
>> http://donutmonster.com/stuff/2007/2...ava_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


Yep! I eventually figured that one out.

(Not bad for a manager, IMO!)


<sniP>

>> if (e.getActionCommand().equals("Click Me")) {
>>
>> frametwo.setVisible(true);
>> }

>
> 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.


<snip>

>
> 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.)


--
www.perfectreign.com
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
I do ping others and viceversa but, cannot ping myself jorgeantibes Wireless Networking 0 05-15-2009 11:37 AM
VRRP : I am unable to ping the virtual address, I can only ping thebackup addresses. ATM Cisco 2 11-13-2008 09:50 PM
Can Ping Switch but Can't Ping Rtr (behind it) Bob Simon Cisco 8 01-19-2005 05:31 PM
ping ping Why gruffydd Computer Support 3 12-29-2004 05:09 PM
Can not ping myself, but can ping others =?Utf-8?B?V0pQQw==?= Wireless Networking 6 12-26-2004 05:56 AM



Advertisments