Solved

Launching new Frames (Swing)

Posted on 2003-12-03
8
630 Views
Last Modified: 2013-11-23
I want to change the style of an application of mine. I changed the main window to be a relatively small JFrame, and through its buttons, to launch other separate screens (as modal windows/jframes).

I saw that my code (the events and action listeners parts) are going to get v complex soon. I need a very general skeleton/description on how to proceed from scratch. Also, can I have those extra frames running as separate threads?

Note: those extra frames I have in mind, I'm thinking totally separate, not excel/word-style contained within the main window (like in that SwingDemo example). Not a must; just a style preference.

TIA
0
Comment
Question by:piou
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 15

Expert Comment

by:jimmack
ID: 9865965
It looks like you're mixing up some concepts here.

>> (as modal windows/jframes)

If the windows are modal, the idea is that they block the Swing thread for the main window (so you couldn't press any other buttons to open further windows.

>> can I have those extra frames running as separate threads?

If you run them in separate thread, the modal idea goes out of the window (Sorry.  What a dreadful pun :-( )

Your main concern seems to be the action listeners.  Each window (JFrame/JDialog) should manage it's own action events.  You could do this by implementing the ActionListener interface:

public class MyOtherFrame extends JFrame implements ActionListener

or you could use anonymous listeners for each JButton/JMenuItem etc.:

JButton myButton = new JButton("Button");
myButton.addActionListener(new ActionListener()
{
    public void actionPerformed(ActionEvent e)
    {
        System.out.println("Button has been pressed");
    }
});

The main problem you might face with anonymous listeners is the scope of attributes that they can deal with.
0
 
LVL 35

Expert Comment

by:girionis
ID: 9865967
 You can use a jtabbedpane and have each frame (or panel) in a separate tab. Then in each tab you can have subtabs and so on.

> Also, can I have those extra frames running as separate threads?

  Yes you can.
0
 

Author Comment

by:piou
ID: 9867758
Ok, let's drop the thread issue. Let say I want to launch a new JFrame by pressing a button.

Should it be like this:

button's action listener() {
  newframe = new JFrame();
  call some lengthy method to prepare the panels, functionality etc... (time consuming?)
  newframe.put contents, pack, set visible etc
}

or is there another (better) approach?
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 15

Expert Comment

by:jimmack
ID: 9867769
Create the new frame in a separate method (if it's not too complex), or create a new class that extends JFrame if you want to include lots of elements (eg. if it's a standard form of some sort).
0
 
LVL 3

Expert Comment

by:InNoCenT_Ch1ld
ID: 9871755
i used to create application in such way as <piou> intend to do back to some ages ago.

piou>>
perhaps what you can do is, create on MainFrame that contains some buttons that will trigger other frame.
inside the MainFrame, initialize the "other" frames by passing a reference of database object(which connect to the database, and perform some simple method).

For each and every "other" frames, create a new Listener class for them.

so whenever you need to perform some operation related to the database in any of your "other" frames, you can just perform it locally (without have to go back to the main class,bla bla..)

Example:

//MainFrame
public class MainFrame extends JFrame{
 Database db = new Database();
 OtherFrame oFrame = new OtherFrame();

 public MainFrame(){}

}

//Database
public class Database{

  //connec to database

}

//OtherFrame
public class OtherFrame extends JFrame{

   OtherFrame(Database db){
     OtherFrameListener listener = new OtherFrameListener(db);
     JButton b = new JButton("hehe");
     b.addActionListener(db);
     getContentPane().add(b, BorderLayout.NORTH);
   }
}

//OtherFrameListener
public class OtherFrameListener implements ActionListener{

  public void actionPerformed(ActionEvent e){}
}


i dont know what other experts will say, but i find it very easy to monitor, maintain my data flow ;)

-Ch1ld-
0
 
LVL 3

Accepted Solution

by:
InNoCenT_Ch1ld earned 100 total points
ID: 9871790
for the OtherFrame and its Listener class, you can do like this:

//OtherFrame----------------------------------------------------------------------
public class OtherFrame extends JFrame{

   OtherFrame(Database db){

     OtherFrameListener listener = new OtherFrameListener(db, this);
     JButton b = new JButton("hehe");
     b.addActionListener(listener);
     JButton c = new JButton("hehehe");
     c.addActionListener(listener);

     getContentPane().add(b, BorderLayout.NORTH);
     getContentPane().add(c, BorderLayout.NORTH);
   }
}

//OtherFrameListener-----------------------------------------------------------------
public class OtherFrameListener implements ActionListener{
  OtherFrame of;
  Database dtb;

  public OtherFrameListener(Database db, JFrame frame)
  {
    of = frame;
    dtb = db;
  }

  public void actionPerformed(ActionEvent e){
    if(e.getSource() == of.b){}
    if(e.getSource() == of.c){}
  }
}

hope this helps ;)
-Ch1ld-
0
 

Author Comment

by:piou
ID: 9873118
Thank you all.
0
 
LVL 3

Expert Comment

by:InNoCenT_Ch1ld
ID: 9873196
;-)

-Ch1ld-
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
jar file executable 12 52
tomcat not starting 6 45
Facing this issue for maven proxy setting 2 12
going to wrong jsp page 2 21
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question