Win a copy of Zero to AI - A non-technical, hype-free guide to prospering in the AI era this week in the Artificial Intelligence and Machine Learning forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

GUI Code Placement

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 ]
Ranch Hand
Posts: 15304
Mac OS X IntelliJ IDE Chrome
  • 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 ]
Don't get me started about those stupid light bulbs.
    Bookmark Topic Watch Topic
  • New Topic