Solved

Launching new Frames (Swing)

Posted on 2003-12-03
8
632 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Jaspersoft Studio is a plugin for Eclipse that lets you create reports from a datasource.  In this article, we'll go over creating a report from a default template and setting up a datasource that connects to your database.
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
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:

749 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