• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 307
  • Last Modified:

How are Observer and Observable used?

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
keviin555
Asked:
keviin555
  • 5
  • 3
  • 2
1 Solution
 
CEHJCommented:
You've identified the correct pattern. What's the problem?
0
 
for_yanCommented:
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
 
keviin555Author Commented:
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
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

 
for_yanCommented:
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
 
for_yanCommented:
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
 
CEHJCommented:
>>and a secondary window where the text is sent line by line .

to where?
0
 
keviin555Author Commented:
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
 
for_yanCommented:

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
 
keviin555Author Commented:
It's work!! perfect :) thank you for_yan
0
 
for_yanCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

  • 5
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now