Results 1 to 6 of 6
  1. #1
    newicons is offline Member
    Join Date
    Jun 2010
    Posts
    3
    Rep Power
    0

    Default Help: passing data from multiple JFrames

    Hi, hoping an expert can help me here. See the image of what I am trying to achieve,..



    I have created 3 jFrames with jButton options. And I want the 4th jFrame to display (or compute) the combination of buttons selected. So the question is, how do I pass the selected data from different frames/components (example here from the top 3 jFrames) into other frames/components (the bottom frame)?

    This is assuming the 4th jFrame can display the selection upon any clicks, Or it can display after all 3 buttons are selected from each 1-3 Frames. Does not matter.

    Appreciate your time and help. cheers. :)

  2. #2
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    6

    Default

    if all JButtons share the same ActionsListener then inside the ActionListener you can check which button was clicked and then compute and display your button combination in the 4th JFrame.

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,529
    Blog Entries
    7
    Rep Power
    20

    Default

    There's a nice and simple design pattern for that purpose: the mediator. Your pink component is the mediator here. It has three separate listeners connected to the three green components. It also has three Strings:

    Java Code:
    private String actor;
    private String action;
    private String actee;
    ... and it has a display method:

    Java Code:
    if (actor != null && action != null && actee != null)
       doDisplay(actor+" "+action+" "+actee);
    The three listeners fill in the corresponding values, i.e. actor, action or actee and call the display method afterwards.

    kind regards,

    Jos

  4. #4
    newicons is offline Member
    Join Date
    Jun 2010
    Posts
    3
    Rep Power
    0

    Default

    many thanks j2me64 & Jos.
    this is the first time im introduced mediator pattern. googled and spent hours trying to figure out the complexity of how it links classes together. once i get it right i'll share the codes for others. cheers.

  5. #5
    newicons is offline Member
    Join Date
    Jun 2010
    Posts
    3
    Rep Power
    0

    Default

    Finally got it working. Thank you again for your help! Here are my codes to share. And also due credit to http://www.avajava.com which I learned the mediator pattern.


    RunFrames.java
    package components;

    import javax.swing.JDesktopPane;
    import javax.swing.JFrame;
    import java.awt.event.*;
    import java.awt.*;


    public class RunFrames extends JFrame {
    Mediator mediator = new Mediator();
    JDesktopPane desktop;
    ActorFrame actorframe = new ActorFrame(mediator);
    ActionFrame actionframe = new ActionFrame(mediator);
    ActeeFrame acteeframe = new ActeeFrame(mediator);
    LogFrame logframe = new LogFrame(mediator);

    public RunFrames() {
    super("Demo");
    setBounds(50, 50, 600, 400);
    desktop = new JDesktopPane();
    setContentPane(desktop);
    desktop.setDragMode(JDesktopPane.OUTLINE_DRAG_MODE );

    desktop.add(actorframe);
    desktop.add(actionframe);
    desktop.add(acteeframe);
    desktop.add(logframe);
    actorframe.setVisible(true);
    actionframe.setVisible(true);
    acteeframe.setVisible(true);
    logframe.setVisible(true);

    }


    public static void main(String[] args) {
    javax.swing.SwingUtilities.invokeLater(new Runnable() {
    public void run() {
    RunFrames mainframe = new RunFrames();
    mainframe.setDefaultCloseOperation(JFrame.EXIT_ON_ CLOSE);
    mainframe.setVisible(true);
    }
    });
    }
    }


    Mediator.java
    package components;

    // Learnings from Mediator Pattern - Web Tutorials - avajava.com
    // MEDIATOR PATTERN

    public class Mediator {

    private String actorSelected = null;
    private String actionSelected = null;
    private String acteeSelected = null;

    ActorFrame actorFrame;
    ActionFrame actionFrame;
    ActeeFrame acteeFrame;
    LogFrame logFrame;

    public void registerActorFrame(ActorFrame actorFrame) {
    this.actorFrame = actorFrame;
    }
    public void registerActionFrame(ActionFrame actionFrame) {
    this.actionFrame = actionFrame;
    }
    public void registerActeeFrame(ActeeFrame acteeFrame) {
    this.acteeFrame = acteeFrame;
    }
    public void registerLogFrame(LogFrame logFrame) {
    this.logFrame = logFrame;
    }

    public Mediator() {
    }

    public String theCompleteAction(String actor, String action, String actee) {
    String joinedString = actor + " " + action + " " + actee;
    logFrame.typeoutText("COMPLETE: " +joinedString);
    return joinedString;
    }

    public String checkActor(String actor) {
    actorSelected = actor;
    logFrame.typeoutText(actorSelected + " selected.");
    if (actorSelected=="Cancel") {actorSelected=null;}
    if (actorSelected != null && actionSelected != null && acteeSelected != null)
    {
    return theCompleteAction(actorSelected,actionSelected,act eeSelected);}
    return null;
    }

    public String checkAction(String action) {
    actionSelected = action;
    logFrame.typeoutText(actionSelected + " selected.");
    if (actionSelected=="Cancel") {actionSelected=null;}
    if (actorSelected != null && actionSelected != null && acteeSelected != null)
    {
    return theCompleteAction(actorSelected,actionSelected,act eeSelected);}
    return null;
    }

    public String checkActee(String actee) {
    acteeSelected = actee;
    logFrame.typeoutText(acteeSelected + " selected.");
    if (acteeSelected=="Cancel") {acteeSelected=null;}
    if (actorSelected != null && actionSelected != null && acteeSelected != null)
    {
    return theCompleteAction(actorSelected,actionSelected,act eeSelected);}
    return null;
    }
    }


    ActorFrame.java
    package components;

    import java.util.Enumeration;
    import javax.swing.JInternalFrame;
    import javax.swing.JPanel;
    import javax.swing.JToggleButton;
    import javax.swing.ButtonGroup;
    import javax.swing.BorderFactory;
    import javax.swing.border.Border;
    import javax.swing.AbstractButton;
    import java.awt.event.*;
    import java.awt.*;

    public class ActorFrame extends JInternalFrame {

    Mediator mediator1;

    public ActorFrame(Mediator mediator) {
    super("Actors",true,true,true,true);
    setSize(150,150);
    setLocation(30,30);
    this.mediator1 = mediator;
    this.mediator1.registerActorFrame(this);

    // start adding buttons....
    JPanel panel = new JPanel(new GridLayout(0, 1));
    Border border = BorderFactory.createTitledBorder("Options");
    panel.setBorder(border);
    final ButtonGroup group = new ButtonGroup();

    String[] anArray;
    anArray = new String[2];
    anArray[0] = "Tarzan";
    anArray[1] = "Jane";


    ActionListener aListener = new ActionListener() {
    public void actionPerformed(ActionEvent event) {
    Enumeration elements = group.getElements();
    while (elements.hasMoreElements()) {
    AbstractButton button = (AbstractButton) elements
    .nextElement();
    if (button.isSelected()) {
    mediator1.checkActor(button.getText());
    break;
    }
    }
    group.setSelected(null, true);
    }
    };

    int count = 0;
    while (count < 2) {
    AbstractButton button1 = new JToggleButton(anArray[count]);
    button1.addActionListener(aListener);
    panel.add(button1);
    group.add(button1);
    count++;
    }
    AbstractButton button1 = new JToggleButton("Cancel");
    button1.addActionListener(aListener);
    panel.add(button1);
    group.add(button1);
    // end adding buttons....
    setContentPane(panel);

    }
    }


    ActionFrame.java
    package components;

    import java.util.Enumeration;
    import javax.swing.JInternalFrame;
    import javax.swing.JPanel;
    import javax.swing.JButton;
    import javax.swing.JToggleButton;
    import javax.swing.ButtonGroup;
    import javax.swing.BorderFactory;
    import javax.swing.border.Border;
    import javax.swing.AbstractButton;
    import java.awt.event.*;
    import java.awt.*;

    public class ActionFrame extends JInternalFrame {

    Mediator mediator2;

    public ActionFrame(Mediator mediator) {
    super("Actions",true,true,true,true);
    setSize(150,150);
    setLocation(210,30);
    this.mediator2 = mediator;
    this.mediator2.registerActionFrame(this);

    // start adding buttons....
    JPanel panel = new JPanel(new GridLayout(0, 1));
    Border border = BorderFactory.createTitledBorder("Options");
    panel.setBorder(border);
    final ButtonGroup group = new ButtonGroup();

    String[] anArray;
    anArray = new String[2];
    anArray[0] = "punches";
    anArray[1] = "kicks";

    ActionListener aListener = new ActionListener() {
    public void actionPerformed(ActionEvent event) {
    Enumeration elements = group.getElements();
    while (elements.hasMoreElements()) {
    AbstractButton button = (AbstractButton) elements
    .nextElement();
    if (button.isSelected()) {
    mediator2.checkAction(button.getText());
    break;
    }
    }
    group.setSelected(null, true);
    }
    };

    int count = 0;
    while (count < 2) {
    AbstractButton button1 = new JToggleButton(anArray[count]);
    button1.addActionListener(aListener);
    panel.add(button1);
    group.add(button1);
    count++;
    }
    AbstractButton button1 = new JToggleButton("Cancel");
    button1.addActionListener(aListener);
    panel.add(button1);
    group.add(button1);

    // end adding buttons....

    setContentPane(panel);

    }
    }


    ActeeFrame.java
    package components;

    import java.util.Enumeration;
    import javax.swing.JInternalFrame;
    import javax.swing.JPanel;
    import javax.swing.JToggleButton;
    import javax.swing.ButtonGroup;
    import javax.swing.BorderFactory;
    import javax.swing.border.Border;
    import javax.swing.AbstractButton;
    import java.awt.event.*;
    import java.awt.*;

    public class ActeeFrame extends JInternalFrame {

    Mediator mediator3;


    public ActeeFrame(Mediator mediator) {

    super("Actee",true,true,true,true);
    setSize(150,150);
    setLocation(390,30);
    this.mediator3 = mediator;
    this.mediator3.registerActeeFrame(this);

    // start adding buttons....
    JPanel panel = new JPanel(new GridLayout(0, 1));
    Border border = BorderFactory.createTitledBorder("Options");
    panel.setBorder(border);
    final ButtonGroup group = new ButtonGroup();

    String[] anArray;
    anArray = new String[2];
    anArray[0] = "Panther";
    anArray[1] = "Gorilla";

    ActionListener aListener = new ActionListener() {
    public void actionPerformed(ActionEvent event) {
    Enumeration elements = group.getElements();
    while (elements.hasMoreElements()) {
    AbstractButton button = (AbstractButton) elements
    .nextElement();
    if (button.isSelected()) {
    mediator3.checkActee(button.getText());
    break;
    }
    }
    group.setSelected(null, true);
    }
    };


    int count = 0;
    while (count < 2) {
    AbstractButton button1 = new JToggleButton(anArray[count]);
    button1.addActionListener(aListener);
    panel.add(button1);
    group.add(button1);
    count++;
    }
    AbstractButton button1 = new JToggleButton("Cancel");
    button1.addActionListener(aListener);
    panel.add(button1);
    group.add(button1);

    // end adding buttons....

    setContentPane(panel);

    }
    }


    LogFrame.java
    package components;

    import java.util.Enumeration;
    import javax.swing.*;
    import javax.swing.JInternalFrame;
    import javax.swing.JPanel;
    import javax.swing.JTextArea;
    import javax.swing.JScrollPane;
    import javax.swing.JToggleButton;
    import javax.swing.ButtonGroup;
    import javax.swing.BorderFactory;
    import javax.swing.border.Border;
    import javax.swing.AbstractButton;
    import java.awt.event.*;
    import java.awt.*;

    public class LogFrame extends JInternalFrame {

    Mediator mediator4;
    protected JTextArea textArea;
    private final static String newline = "\n";

    public LogFrame(Mediator mediator) {
    super("Action Log",true,true,true,true);
    setSize(300,150);
    setLocation(30,210);
    this.mediator4 = mediator;
    this.mediator4.registerLogFrame(this);

    // start adding text area....
    JPanel panel = new JPanel(new GridLayout(0, 1));
    Border border = BorderFactory.createTitledBorder("Options");
    panel.setBorder(border);

    textArea = new JTextArea(5, 20);
    textArea.setEditable(false);
    JScrollPane scrollPane = new JScrollPane(textArea);

    panel.add(scrollPane);
    // end adding text area....

    setContentPane(panel);
    }

    public void typeoutText(String text) {
    textArea.append(text + newline);
    textArea.setCaretPosition(textArea.getDocument().g etLength());
    }
    }

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,529
    Blog Entries
    7
    Rep Power
    20

    Default

    A small warning: don't compare Strings for equality with the == operator; use the String.equals( ... ) method instead.

    kind regards,

    Jos

Similar Threads

  1. Replies: 4
    Last Post: 11-13-2009, 12:30 PM
  2. Passing data between dialogs
    By Wintery in forum New To Java
    Replies: 7
    Last Post: 01-02-2009, 05:35 AM
  3. Need help passing data between classes
    By bri1547 in forum New To Java
    Replies: 3
    Last Post: 07-21-2008, 04:19 AM
  4. passing data dynamically
    By abhiN in forum Advanced Java
    Replies: 1
    Last Post: 01-22-2008, 09:43 AM
  5. passing dynamic data
    By abhiN in forum Web Frameworks
    Replies: 0
    Last Post: 01-17-2008, 01:16 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •