GeeCON Prague 2014*
The moose likes Swing / AWT / SWT and the fly likes GUI Code Placement Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "GUI Code Placement" Watch "GUI Code Placement" New topic
Author

GUI Code Placement

Nnnnn Sssss
Greenhorn

Joined: Oct 06, 2004
Posts: 1
My question deals with proper placement of code (if there is such a thing as proper placement).

In the code sample below, the About dialog is shown when a button is clicked, however the code from which the dialog is created is contained within the event listener attahced to the button! The code sample is from a skeleton file for a class project -- rules are that all code must be contained within one file.

My question is... ideally (ignoring the class project's rules)... where should the code to create the About dialog box be? Should I have created a class called AboutDialog that extends JFrame?

And what if I wanted to write code such that it wouldn't spawn additional About dialogs if one were already visible? I think I could just have a member variable of type AboutDialog and check to see if it were null every time the button to spawn an About dialog is clicked. Then, if it were null, initialize the dialog, if not, do nothing.

Whew, if you've made it this far, congrats!

Thanks
-N SCJP

-------- CODE SAMPLE -------------------------------------------------------
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;

public class App {
JFrame frame;
JButton about;
public static void main(String[] args) {
App me = new App();
me.go();
}
public void go() {
frame = new JFrame("Launch another frame");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300, 200);
about = new JButton("About");
about.addActionListener(new AboutListener());
frame.getContentPane().add(about);
frame.setVisible(true);
}
public class AboutListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
JFrame popUp = new JFrame("About frame");
popUp.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
popUp.setSize(250,200);
MyPanel panel = new MyPanel();
popUp.getContentPane().add(panel);
popUp.setVisible(true);
}
public class MyPanel extends JPanel {
public void paintComponent(Graphics g) {
int width = this.getWidth();
int height = this.getHeight();
g.setColor(Color.white);
g.fillRect(0, 0, width, height);
g.setColor(Color.red);
g.drawString("Your stuff goes here...", width/4, height/2);
}
}
}
}

[ October 06, 2004: Message edited by: Nihal Sinha ]
[ October 06, 2004: Message edited by: Nihal Sinha ]
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

What I typically do is create a custom class that extends JFrame/JDialog/JWindow (whatever frame class you want) and then call that from my action. If you need to pass it parameters of any kind, just create a constructor that accepts whatever parameters you need.

As far as, if one were already visible, you would just need to keep track of that yourself in some kind of list. And check that list for the presence of a dialog already. If it's there, just bring the other dialog to the front.

major psuedo code:



This was just something I thougt at first attempt. Another option is to make your JDialog modal so that nothing else can be pressed unil that dialog is closed (typical common use of a JDialog). If this method does not suit your needs, consider the latter.
[ October 06, 2004: Message edited by: Gregg Bolinger ]

GenRocket - Experts at Building Test Data
 
GeeCON Prague 2014
 
subject: GUI Code Placement