Solved

Launching new Frames (Swing)

Posted on 2003-12-03
8
635 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses

628 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