i m trying to write a gui java program.
i have a main screen, i declared my object in the main screen class.
in the main screen, i will call 2 jpanel. from different class
how can i make the 2 jpanel use the same object i declared on my main screen?

Edited 6 Years Ago by chern4ever: n/a

Are you asking how you will call these two panels to be displayed in the frame or about passing values down to these panels? Sorry, but your problem explanation is very unclear.

i noe how to call these 2 panels to display at the jframe.
i m trying to do a atm program.
the main jframe is the machine.
panel 1 is screen, 2 is keypad
in jframe i declared
Keypad _keypad = new Keypad()
Screen _screen = new Screen()
then i add(_keypad) and add(_screen)

in jpanel _keypad, after user pressing a specified button, i wan it to call _screen.display(String s) so that the jpanel _screen will recieve and display which number the user entered.

sorry for the confusion.

Edited 6 Years Ago by chern4ever: n/a

Set a listener from frame listening for keyboard panel activities and provide a public method from screen panel that frame can simply populate.

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
 
public class Tester extends JFrame {
	
    Keypad _keypad = new Keypad();
	Screen _screen = new Screen();
	
	public Tester(){
		setSize(350,400);
		
		Container container = getContentPane();
		container.setLayout(new GridLayout(2,1));
		
		container.add(_screen);
		container.add(_keypad);

		setVisible(true);
		setLocationRelativeTo(null);
	}

	public static void main(String[] args) {
		new Tester();
    }
}
import java.awt.*;
import java.applet.*;
import javax.swing.*;
import java.awt.event.*;

public class Keypad extends JPanel implements ActionListener{
	
	JButton _btnNum[] = new JButton[10];
	JButton numdot = new JButton(".")	;
	JButton numback = new JButton("Backspace");
	JButton btnEnter = new JButton("Enter");
	JButton btnCancel = new JButton("Cancel");
	
	public Keypad() {
		setLayout(new GridLayout(5,3));
		
		for(int i=0;i<10;i++){
			_btnNum[i] = new JButton(""+i);
			add(_btnNum[i]);
			_btnNum[i].addActionListener(this);
		}				
		add(numdot);
		add(numback);
		add(btnEnter);
		add(new JLabel());
		add(btnCancel);

		numdot.addActionListener(this);
		numback.addActionListener(this);
		btnEnter.addActionListener(this);
		btnCancel.addActionListener(this);
	}

	public void actionPerformed(ActionEvent e){
		String numberEntered="";
		
		for(int i=0;i<10;i++){
			if(e.getSource()==_btnNum[i])
			numberEntered+=i;
			_screen.getDisplay(numberEntered);
			// display number entered to screen textarea
		}
		if(e.getSource()==numdot){
			numberEntered+=".";
			// display number entered to screen textarea
		}
		else if(e.getSource()==numback){
			int len = numberEntered.length();
			numberEntered=numberEntered.substring(0,len-1);
			// display number entered to screen textarea
		}		
		else if(e.getSource()==btnEnter){
			// display number entered to screen textarea
			
		}
	}
}
import java.awt.*;
import java.applet.*;
import javax.swing.*;
import java.awt.event.*;

public class Screen extends JPanel{

	JTextArea display = new JTextArea("Test");
	JButton btnOptions[] = new JButton[6];

    public Screen() {
    	setLayout(new BorderLayout());
    	add(display,BorderLayout.CENTER);
    	
    	JPanel west = new JPanel( new GridLayout(3,1) );
    	for(int i=0;i<3;i++){
    		btnOptions[i] = new JButton("Options "+(i+1));
    		west.add(btnOptions[i]);
    	}
    	add(west,BorderLayout.WEST);
    	
    	JPanel east = new JPanel( new GridLayout(3,1) );
    	for(int i=3;i<6;i++){
    		btnOptions[i] = new JButton("Options "+(i+1));
    		east.add(btnOptions[i]);
    	}
    	add(east,BorderLayout.EAST);
    		
    }
    
    public void getDisplay(String numberEnter){
    	display.setText(numberEnter);
    }
    
}

example of my code. in class keypad, after user click a button, i need to call a method in class screen.

Here you go...

public interface Observer{
	public void update(String string);
}
public interface Subject{
	public void registerObserver(Observer o);
	public void removeObserver(Observer o);
	public void notifyObservers();
}
import java.awt.*;
import java.applet.*;
import javax.swing.*;
import java.awt.event.*;

public class Screen extends JPanel implements Observer{

	JTextArea display = new JTextArea("Test");
	JButton btnOptions[] = new JButton[6];
	private Subject atmKeypad;

    public Screen(Subject atmKeypad) {
    	this.atmKeypad = atmKeypad;
    	this.atmKeypad.registerObserver(this);
    	
    	setLayout(new BorderLayout());
    	add(display,BorderLayout.CENTER);
    	
    	JPanel west = new JPanel( new GridLayout(3,1) );
    	for(int i=0;i<3;i++){
    		btnOptions[i] = new JButton("Options "+(i+1));
    		west.add(btnOptions[i]);
    	}
    	add(west,BorderLayout.WEST);
    	
    	JPanel east = new JPanel( new GridLayout(3,1) );
    	for(int i=3;i<6;i++){
    		btnOptions[i] = new JButton("Options "+(i+1));
    		east.add(btnOptions[i]);
    	}
    	add(east,BorderLayout.EAST);
    		
    }
    
    public void update(String string){
    	getDisplay(string);
    }
    
    public void getDisplay(String numberEnter){
    	display.setText(numberEnter);
    }    
}
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.ArrayList;

public class Keypad extends JPanel implements ActionListener, Subject{
	
	JButton _btnNum[] = new JButton[10];
	JButton numdot = new JButton(".")	;
	JButton numback = new JButton("Backspace");
	JButton btnEnter = new JButton("Enter");
	JButton btnCancel = new JButton("Cancel");
	
	private String displayValue;
	private ArrayList observer;
	
	public Keypad() {
		observer = new ArrayList();
		setLayout(new GridLayout(5,3));
		
		for(int i=0;i<10;i++){
			_btnNum[i] = new JButton(""+i);
			add(_btnNum[i]);
			_btnNum[i].addActionListener(this);
		}				
		add(numdot);
		add(numback);
		add(btnEnter);
		add(new JLabel());
		add(btnCancel);

		numdot.addActionListener(this);
		numback.addActionListener(this);
		btnEnter.addActionListener(this);
		btnCancel.addActionListener(this);
	}

	public void actionPerformed(ActionEvent e){
		String numberEntered="";
		
		for(int i=0;i<10;i++){
			if(e.getSource()==_btnNum[i])
			numberEntered+=i;			
		}
		if(e.getSource()==numdot){
			numberEntered+=".";
		}
		else if(e.getSource()==numback){
			int len = numberEntered.length();
			numberEntered=numberEntered.substring(0,len-1);
		}		
		else if(e.getSource()==btnEnter){}
		setDisplayValue(numberEntered);
		notifyObservers();
	}
	
	private void setDisplayValue(String string){
		displayValue = string;
	}
	
	private String getDisplayValue(){
		return displayValue;
	}
	
	public void registerObserver(Observer o){
		observer.add(o);
	}
	
	public void removeObserver(Observer o){
		int i = observer.indexOf(o);
		if(i >= 0) observer.remove(i);
	}
	
	public void notifyObservers(){
		for(int i = 0; i < observer.size(); i++){
			Observer obs = (Observer) observer.get(i);
			obs.update(getDisplayValue());
		}
	}
}
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
 
public class Tester extends JFrame {
	
    Keypad _keypad = new Keypad();
	Screen _screen = new Screen(_keypad);
	
	public Tester(){
		setSize(350,400);
		
		Container container = getContentPane();
		container.setLayout(new GridLayout(2,1));
		
		container.add(_screen);
		container.add(_keypad);

		setVisible(true);
		setLocationRelativeTo(null);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}

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

Do not forget to improve processing inside of actionPerformed method of Keypad!

This article has been dead for over six months. Start a new discussion instead.