Solved

Launching new Frames (Swing)

Posted on 2003-12-03
8
626 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
 
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

744 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now