Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

GUI Code Placement  RSS feed

Nnnnn Sssss
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!


-------- 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();
public void go() {
frame = new JFrame("Launch another frame");
frame.setSize(300, 200);
about = new JButton("About");
about.addActionListener(new AboutListener());
public class AboutListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
JFrame popUp = new JFrame("About frame");
MyPanel panel = new MyPanel();
public class MyPanel extends JPanel {
public void paintComponent(Graphics g) {
int width = this.getWidth();
int height = this.getHeight();
g.fillRect(0, 0, width, height);
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
Ranch Hand
Posts: 15304
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!