Link to home
Start Free TrialLog in
Avatar of SEstudent
SEstudent

asked on

connecting interfaces


Hi..

If I want to connect to interfaces with each othre..

so if I click the button (e.g manager)..I will be able to see the manager interface..

How can I do that?
Avatar of girionis
girionis
Flag of Greece image

 Are you talking with interfaces with regards to Java terminology or interfaces as in "user interface"?
Avatar of SEstudent
SEstudent

ASKER


I mean user interface..
 Just call the class that implements the manager interface when the user clicks on a button. Add an actionListener to the button and then when an ActionEvent if fired use it to check for the button. If it is the button you want then load the class.

  Take a look here for more info of how to use buttons and events: http://java.sun.com/docs/books/tutorial/uiswing/components/button.html
Use the CardLayout layout manager in the panel.. when the button is pressed, how the right card..
this is a sample code.. note that it is not tested..

//create the master panel
JPanel masterPanel=new JPanel();
//create the layout manager
CardLayout cl=new CardLayout();
cl.addLayoutComponent(managerPanel, "manager");
cl.addLayoutComponent(employeePanel, "Employee");
//add the layout to the master panel
masterPanel.setLayout(cl);

//when the button is pressed
cl.show(masterPanel, "manager");

thanks all for your comments..

but what if I have already created two user interfaces in two different classes..

The first is called "ManagerUI" .. and the second class in "EmployeeUI"..

what do I have to add to the actionlistener in order to be able to show the other class UI only?
 Add the actionListener to the button. When a button is pressed an action event will be send to the actionPerformed method. You need to check the event and see which button is pressed. If the button is the one for the ManagerUI then load that class otherwise load the other one. Take a look at the link I posted it contains several examples of how you can do it.

can you help me please with the code that will load the class to me?

now I have these two classes:

package helpdesk;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import com.borland.jbcl.layout.*;

public class Frame1 extends JFrame {
  JPanel contentPane;
  JToggleButton btnManager = new JToggleButton();
  XYLayout xYLayout1 = new XYLayout();
  JButton btnEmployee = new JButton();
  JLabel lblPosition = new JLabel();
  ManagerUI mngr;
  EmployeeUI empl;

  //Construct the frame
  public Frame1() {
    try {
      jbInit();
    }
    catch(Exception e) {
      e.printStackTrace();
    }
  }
  //Component initialization
  private void jbInit() throws Exception  {
    contentPane = (JPanel) this.getContentPane();
    btnManager.setText("Manager");
    btnManager.addActionListener(new Frame1_btnManager_actionAdapter(this));
    contentPane.setLayout(xYLayout1);
    this.setSize(new Dimension(400, 300));
    this.setTitle("Frame Title");
    btnEmployee.setText("Employee");
    btnEmployee.addActionListener(new Frame1_btnEmployee_actionAdapter(this));
    lblPosition.setText("Choose Your Position");
    contentPane.add(btnManager, new XYConstraints(146, 102, 112, -1));
    contentPane.add(btnEmployee,   new XYConstraints(147, 156, 113, 24));
    contentPane.add(lblPosition,   new XYConstraints(25, 30, 115, 26));
    mngr = new ManagerUI(this, true);
    empl = new EmployeeUI(this, true);
  }
  //Overridden so we can exit when window is closed
  protected void processWindowEvent(WindowEvent e) {
    super.processWindowEvent(e);
    if (e.getID() == WindowEvent.WINDOW_CLOSING) {
      System.exit(0);
    }
  }

  void btnManager_actionPerformed(ActionEvent e) {
    mngr.setName("Manager");
    mngr.show();

  }

  void btnEmployee_actionPerformed(ActionEvent e) {
    empl.setName("Employee");
    empl.show();
  }
}

class Frame1_btnManager_actionAdapter implements java.awt.event.ActionListener {
  Frame1 adaptee;

  Frame1_btnManager_actionAdapter(Frame1 adaptee) {
    this.adaptee = adaptee;
  }
  public void actionPerformed(ActionEvent e) {
    adaptee.btnManager_actionPerformed(e);
  }
}

class Frame1_btnEmployee_actionAdapter implements java.awt.event.ActionListener {
  Frame1 adaptee;

  Frame1_btnEmployee_actionAdapter(Frame1 adaptee) {
    this.adaptee = adaptee;
  }
  public void actionPerformed(ActionEvent e) {
    adaptee.btnEmployee_actionPerformed(e);
  }
}

===================================

package helpdesk;

import javax.swing.*;
import com.borland.jbcl.layout.*;
import java.awt.*;

public class ManagerUI extends JDialog {

  JPanel jPanel1 = new JPanel();
  XYLayout xYLayout1 = new XYLayout();
  JLabel lblCompNum = new JLabel();
  JComboBox jComboBox1 = new JComboBox();
  JLabel lblTech = new JLabel();
  JComboBox jComboBox2 = new JComboBox();
  JButton btnComplaints = new JButton();
  JScrollPane jScrollPane1 = new JScrollPane();
  JTextArea txtComplaints = new JTextArea();
  JScrollPane jScrollPane2 = new JScrollPane();
  JTextArea txtMDetail = new JTextArea();
  JLabel lblSelect = new JLabel();
  JButton btnChange = new JButton();
  JButton btnShowDetail = new JButton();

  public ManagerUI(JFrame frame, boolean modal) {
    super(frame, modal);
    enableEvents(AWTEvent.WINDOW_EVENT_MASK);

    try {
      jbInit();
    }
    catch(Exception e) {
      e.printStackTrace();
    }
  }
  public ManagerUI(){
   this(null,false);
 }

  private void jbInit() throws Exception {
    lblCompNum.setText("Choose a Complaint");
    xYLayout1.setWidth(450);
    xYLayout1.setHeight(643);
    jPanel1.setLayout(xYLayout1);
    lblTech.setText("Choose a Technician");
    btnComplaints.setText("Show Complaints");
    txtComplaints.setText("");
    txtMDetail.setText("");
    lblSelect.setText("Select a complaint to");
    btnChange.setText("Change Status");
    btnShowDetail.setText("Show Details");
    jPanel1.add(lblCompNum, new XYConstraints(100, 36, 103, 20));
    jPanel1.add(jComboBox1,   new XYConstraints(226, 36, 124, 22));
    jPanel1.add(lblTech,  new XYConstraints(101, 82, 101, 23));
    jPanel1.add(jComboBox2,  new XYConstraints(227, 84, 125, 22));
    jPanel1.add(jScrollPane1, new XYConstraints(22, 190, 406, 152));
    jPanel1.add(btnComplaints,  new XYConstraints(163, 139, 134, 28));
    jPanel1.add(jScrollPane2, new XYConstraints(23, 418, 405, 153));
    jScrollPane2.getViewport().add(txtMDetail, null);
    jScrollPane1.getViewport().add(txtComplaints, null);
    jPanel1.add(lblSelect,  new XYConstraints(22, 350, 105, 22));
    jPanel1.add(btnShowDetail,    new XYConstraints(297, 376, -1, 26));
    jPanel1.add(btnChange, new XYConstraints(94, 377, 106, 24));
  }

public void setName(String na){
    this.setTitle(na);
  }
}

=========================

and when I click on the Manager button from the first class..I could only see the title bar of the manager interface..

could any one help me with this please..
 Try to add the jpanel to the dialog itself. Try this:

  this.getContentPane().add(jpanel);
ASKER CERTIFIED SOLUTION
Avatar of yasser_helmy
yasser_helmy

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial

thanks alot..

 I create the HelpDeskUI Using application so I have (frame1)..but I create the ManagerUI by openning a class and use the design to drag different components..

I can't find dialog area in here..

waiting for your answers
thanks in advance,,

now it works..thanks alot :)