1

So, I have a class Window which extends JFrame, which is the larger window.

enter image description here

Then I have class MathematicalFunvtions which also extends JFrame, which is the smaller window.

I want to combine them together in another class "Test" so they work together.

public static void main(String args[]) {       
    Window mainwindow;
    mainwindow = new Window();
    mainwindow.setVisible(true);


    MathematicalFunctions functions;
    functions = new MathematicalFunctions(mainwindow);
}

My way to do this is to give a reference of Window object as an argument to MathematicalFunctions and let it do the work.

  1. Question is... is it a good way of programming things?

  2. When I send mainwindow to functions I would like to know inside that object, when main window is moved... so this object can track it and adjust to so they are always together.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Tomasz Mularczyk
  • 34,501
  • 19
  • 112
  • 166
  • Please use proper naming for class names. `window` should be `Window` because it can be confused with a variable. – Mr. Polywhirl Jan 13 '15 at 11:17
  • 1
    See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/q/9554636/418556) – Andrew Thompson Jan 13 '15 at 11:27
  • Also, it's usually better to *use* a `JFrame`, rather than *extend* one. – kiheru Jan 13 '15 at 11:30
  • When you click poperties or options in some program then opens another window, photoshop layout is few windows on each side interacting together... so they are not JFrames? – Tomasz Mularczyk Jan 13 '15 at 14:26
  • Okey... I see it should be better JDialog. However I stil dont know how to get movement of JFrame object in another class... – Tomasz Mularczyk Jan 13 '15 at 14:35
  • Maybe you want to call [`setType(Window.Type.UTILITY)`](http://docs.oracle.com/javase/8/docs/api/java/awt/Window.html#setType-java.awt.Window.Type-) on your secondary window (see [`Window.Type.UTILITY`](http://docs.oracle.com/javase/8/docs/api/java/awt/Window.Type.html#UTILITY))… – Holger Jan 13 '15 at 14:54

1 Answers1

1

I didnt give up on research and finally i get it... i put this code in constructor of MathematicalFunctions:

mainwindow.addComponentListener(new java.awt.event.ComponentAdapter() {
        @Override
        public void componentMoved(java.awt.event.ComponentEvent evt) {
            GlowneComponentMoved(evt);
        }
    });

//and the listener....
private void GlowneComponentMoved(ComponentEvent evt) 
{
      this.setLocation( (Okno_Glowne.getX()+ Okno_Glowne.getWidth()+10), Okno_Glowne.getY());   
}

and it works how it should be. Every time i move bigger window, the small one follows :)

Whats more... Also Ive made MathematicalFunction class JDialog extension

public class MathematicalFunctions extends javax.swing.JDialog

and the test class looks like before

public static void main(String args[])
{

   oknoo mainwindow;
   mainwindow = new oknoo();
   mainwindow.setVisible(true);


   MathematicalFunctions funkcje;
   funkcje = new MathematicalFunctions(OknoGlowne);
}

;)

Tomasz Mularczyk
  • 34,501
  • 19
  • 112
  • 166