Solved

Launching new Frames (Swing)

Posted on 2003-12-03
8
634 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

734 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