[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

How are Observer and Observable used?

Posted on 2011-05-11
10
Medium Priority
?
296 Views
Last Modified: 2012-05-11
Hello
It is about creating a program that has a main window where you type text, and a secondary window where the text is sent line by line .
The main window contains an editable text box where the user will type the line to send, and a submit button.
It will also close the program.
So i try to use observer and observable to creat the two windows but the first class extends observer and its extends also jframe!!!
have you any solution to creat this to windows ?
0
Comment
Question by:keviin555
  • 5
  • 3
  • 2
10 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 35741695
You've identified the correct pattern. What's the problem?
0
 
LVL 47

Expert Comment

by:for_yan
ID: 35741870
The pattern is correct and you can lookup Obsrerver and Observable API, etc,
 but in order to have such interaction between two windows
you really don't need that. You just register say ActionListener on one textFiled or on both pass references to each other and
have a method which would type the line into another window element.
You can even register keyListener and ech every charcater if you want
0
 

Author Comment

by:keviin555
ID: 35741945
So it's not necessary to use observer?!!
Can you give me an exemple please how can i use this method that type the line ?!
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 47

Expert Comment

by:for_yan
ID: 35742012
This is the simplistic sketch when you captuer action event on the first JFrame and use
setText method of the second frame, which you created in the constructior of the first
to mosify textfield conatent in the ssecond frame


public class MyClass1 extends JFrame implements ActionListener {
private JTextField txt;
private JBUtton btn;
private MyClass2 mc;

public MyClass1() {

txt = new JTextField...
btn = new JButton(...
btn.addActionListener(this);
this.getContentPane().add(txt);
this.getContentPane().add(btn);




mc = new MyClass2();

mc.setVisible(true);

this.sertVisible(true);

}

public void actionPerformed(ActionEvent ae){
mc.setText(txt.getText());

}


}


}


---------

public class MyClass2 extends JFrame implements  {
private JTextField txt;
private JBUtton btn;
private MyClass1 mc;

public MyClass2() {

txt = new JTextField...
btn = new JButton(...
btn.addActionListener(this);
this.getContentPane().add(txt);
this.getContentPane().add(btn);



}



}

public void setText(String s){
txt.setText(s);
}
}








0
 
LVL 47

Expert Comment

by:for_yan
ID: 35742028
In general nothing prevents you from passing to constructor of the second frame the reference to the
instance of your first frame and then you'll be able to modify textfield
of the first frame based on the typing and pressing the button on the second frame
This is all possible.
and ifg you even don't want the button - you can follow every stroke
on the first textField with key listener and send this tstroke to the
textfield of the second frame

Just with events you should be able to do  any of these  interactions
pretty simple.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 35742047
>>and a secondary window where the text is sent line by line .

to where?
0
 

Author Comment

by:keviin555
ID: 35742661
to the textFeild of the second windows
I tried this one but there is an error that i cant find !!

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JScrollPane;
import javax.swing.JSeparator;
import javax.swing.JTextField;
import javax.swing.JFrame;


public class ClasseOfTheTxtEdit extends JFrame implements ActionListener
{
	private JTextField txt;
	private JButton btn;
	private AffichageDeTexte mc;
	/**
	 * @param args
	 */
	public ClasseOfTheTxtEdit (){
			super();
			
			build();//On initialise notre fenêtre
		}
	private void build() {

		setTitle("fenêtre de l'Exercice 2"); //On donne un titre à l'application
		setSize(400,300); //On donne une taille à notre fenêtre
		setLocationRelativeTo(null); //On centre la fenêtre sur l'écran
		setResizable(true); //On interdit la redimensionnement de la fenêtre
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //On dit à l'application de se fermer 
		
		txt.setPreferredSize(new Dimension(300,200));
		btn.setPreferredSize(new Dimension(100,30));

		txt = new JTextField();
		btn = new JButton("envoyer");
		//btn.addActionListener(this);
		this.getContentPane().add(txt);
		this.getContentPane().add(btn);

	


		mc = new AffichageDeTexte();

		mc.setVisible(true);

		this.setVisible(true);

	}
	public void actionPerformed(ActionEvent e) {
		
		  if(e.getSource().equals(btn))
			  mc.setText(txt.getText());
	


		
	}
	
public static void main(String[] args) {
		
		SwingUtilities.invokeLater(new Runnable(){
			public void run(){
				//On crée une nouvelle instance de notre fenetre
				ClasseOfTheTxtEdit fenetre = new ClasseOfTheTxtEdit ();
				fenetre.setVisible(true);//On la rend visible
			}
		});


	
}
	
	
}

Open in new window


and this is the code of the second wiindows that display the text line by line :

import javax.swing.AbstractButton;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JScrollPane;
import javax.swing.JSeparator;
import javax.swing.JTextField;
import javax.swing.JFrame;

public class DisplayLineByLine extends JFrame //implements ActionListener
{
	
	JTextField txt ;
	
	public DisplayLineByLine (){

	JTextField txt = new JTextField();
	setSize(400,300); //On donne une taille à notre fenêtre
	//setLocationRelativeTo(null); //On centre la fenêtre sur l'écran
	setResizable(true); //On interdit la redimensionnement de la fenêtre
	setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //On dit à l'application de se fermer lors du 
	
	txt.setPreferredSize(new Dimension(300,200));
	this.getContentPane().add(txt);
	
	


	}

	public void setText(String s){
	txt.setText(s);
	}

}

Open in new window

And this is the error:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
	at Ecercice2.build(Ecercice2.java:81)
	at Ecercice2.<init>(Ecercice2.java:72)
	at Ecercice2$1.run(Ecercice2.java:115)
	at java.awt.event.InvocationEvent.dispatch(Unknown Source)
	at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
	at java.awt.EventQueue.access$000(Unknown Source)
	at java.awt.EventQueue$1.run(Unknown Source)
	at java.awt.EventQueue$1.run(Unknown Source)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
	at java.awt.EventQueue.dispatchEvent(Unknown Source)
	at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
	at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
	at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
	at java.awt.EventDispatchThread.run(Unknown Source)

Open in new window


0
 
LVL 47

Accepted Solution

by:
for_yan earned 2000 total points
ID: 35742977

Try this.
Works for me.

import javax.swing.*;
import javax.swing.JFrame;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;


public class ClasseOfTheTxtEdit extends JFrame implements ActionListener
{
	private JTextField txt;
	private JButton btn;
	private DisplayLineByLine mc;
	/**
	 * @param
	 */
	public ClasseOfTheTxtEdit (){
			super();

			build();//On initialise notre fenêtre
		}
	private void build() {

		setTitle("fenêtre de l'Exercice 2"); //On donne un titre à l'application
		setSize(400,300); //On donne une taille à notre fenêtre
		setLocationRelativeTo(null); //On centre la fenêtre sur l'écran
		setResizable(true); //On interdit la redimensionnement de la fenêtre
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //On dit à l'application de se fermer

		//txt.setPreferredSize(new Dimension(300,200));
	//	btn.setPreferredSize(new Dimension(100,30));

		txt = new JTextField(20);
		btn = new JButton("envoyer");
		btn.addActionListener(this);

        Container c =    this.getContentPane();
        c.setLayout(new FlowLayout());
		c.add(txt);
		c.add(btn);




		mc = new DisplayLineByLine();

		mc.setVisible(true);

        this.setSize(300,300);

		this.setVisible(true);

	}
	public void actionPerformed(ActionEvent e) {

		  if(e.getSource().equals(btn))
			  mc.setText(txt.getText());




	}

public static void main(String[] args) {

		SwingUtilities.invokeLater(new Runnable(){
			public void run(){
				//On crée une nouvelle instance de notre fenetre
				ClasseOfTheTxtEdit fenetre = new ClasseOfTheTxtEdit ();
				fenetre.setVisible(true);//On la rend visible
			}
		});



}


}

Open in new window


import javax.swing.AbstractButton;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JScrollPane;
import javax.swing.JSeparator;
import javax.swing.JTextField;
import javax.swing.JFrame;
import java.awt.*;

public class DisplayLineByLine extends JFrame //implements ActionListener
{
	
	JTextField txt ;

	public DisplayLineByLine (){

	 txt = new JTextField(20);
	setSize(400,300); //On donne une taille à notre fenêtre
	//setLocationRelativeTo(null); //On centre la fenêtre sur l'écran
	setResizable(true); //On interdit la redimensionnement de la fenêtre
	setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //On dit à l'application de se fermer lors du

	//txt.setPreferredSize(new Dimension(300,200));
	this.getContentPane().add(txt);




	}

	public void setText(String s){
	txt.setText(s);
	}

}

Open in new window

0
 

Author Comment

by:keviin555
ID: 35743004
It's work!! perfect :) thank you for_yan
0
 
LVL 47

Expert Comment

by:for_yan
ID: 35743014
So in this way you can do connection in bothe directions,
you'll only need to pass reference to the first frame in the constructor of the second frame,
then you'll be able  to write to textfield in the first fram from the second frame also
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This video teaches viewers about errors in exception handling.
Suggested Courses
Course of the Month20 days, 13 hours left to enroll

865 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question