1

I am creating a java application which consists of two frames(JFrame1 and JFrame2) JFrame1 has a grid 6x6 button; and JFrame2 has 6 radio buttons representing colours. How can I link the two frames so that when a button in JFrame1 is clicked, JFrame2 pops up and when a colour is chosen from that the JFrame2 closes and the clicked button gets the respective colour?

BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
Aang Namo
  • 11
  • 1
  • 1
  • 6
  • 1
    Please do not re-invent the wheel. There is already [JColorChooser](http://docs.oracle.com/javase/1.5.0/docs/api/javax/swing/JColorChooser.html) to do what you are looking for – ring bearer Mar 28 '12 at 22:14
  • I am to limit the user to select only six colors. Is it possible to do that if i use JColorChooser? – Aang Namo Mar 28 '12 at 22:32
  • Check this link for a working [Example](http://stackoverflow.com/questions/9860001/java-6x6-grid-colouring-game/9860374#9860374), You should be using a `JDialog` instead of a `JFrame` for choosing colours, One `JFrame` as far as possible, in a given application is the way to go. – nIcE cOw Mar 29 '12 at 05:50

3 Answers3

2

It is better to have one JFrame for every application. Use one for the 6x6 JButtons and create a modal JDialog for your color JRadioButtons.

  1. The color selection JDialog should have a public getSelectedColor() method in order to return the selected color to the caller class.
  2. Instantiate the ColorDialog in main and do not set it visible.
  3. The ActionListener of each JButton should make the modal JDialog visible.
  4. The RadioButton ActionPerformed should set the selected color and make the JDialog invisible.
  5. Call getSelectedColor() and apply the returned color to your JButton.
Costis Aivalis
  • 13,680
  • 3
  • 46
  • 47
0

In your frame1's button action listner, you can do something like this

public void actionPerformed(ActionEvent e) {
   Frame2 frame = new Frame2(this);
   frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   frame.setVisible(true);
}

where "this" refers to the frame1 object. This is you can access its jTEXTFIELD and jBUTTONs from the second frame. So naturally you have store it in Frame1 object declared in your second class.

Suppose you have a clickable color field in frame2 object, once you click on it, you should trigger a function that get the input field from frame1 (using your locale object reference) and store it in it. something like this:

public void actionPerformed(ActionEvent e) {
    frame1.getMyTextField().setText(WHAT_THE_CLICKED_ON);
    this.close();
}

Sorry if I made any syntax errors, it's been a long time I didnt work with java :)

Rorchackh
  • 2,113
  • 5
  • 22
  • 38
0

Just create another class, let us say FrameMananger, then use the singleton pattern to manage them.

Then in any class, you can use FrameManager.getFrame1() to get the frame1, same as the frame2. You can add logic judgement inside, like dynamically dispose some frame or only create them when needed.

This issue is fairly common concept when you create a game and try to navigate between your every view(like the show score panel from everywhere).

public class FrameManager
{
    Frame1 frame1;
    Frame1 frame2;

    public static Frame1 getFrame1()
    {
        if(frame1 == null)
            frame1 = new Frame1();
        return frame1;
    }

    public static Frame1 getFrame2()
    {
        if(frame2 == null)
            frame2 = new Frame1();
        return frame2;
    }

    public class Frame1 extends JFrame
    {

    }

    public class Frame2 extends JFrame
    {

    }
}
chenyi1976
  • 1,104
  • 9
  • 17