Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Java > Other Java > Java GUI
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
Java GUI Discussions specific to programming Java GUI.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Java GUI section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old June 3rd, 2003, 05:37 PM
Registered User
 
Join Date: Jun 2003
Location: Peoria, AZ, USA.
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to pic_az
Default Dialog Class

I am fairly new to Java. Benn working with C++ for years. I created a class call MessageBox extended from Frame and implementing actionListener. I want to basically istantiate this class with the same parameters as in VisualBasic. ex. MessageBox( <message>, type of dialog, title). Type of dialog being OK, Yes/No...... No problem for OK sice it does not return a value. With Yes/no type, I want to retrun a value. I see no way of doing that. Any ideas?
Thanks,
Joey

Joey Picinich
__________________
Joey Picinich
Reply With Quote
  #2 (permalink)  
Old June 4th, 2003, 02:19 AM
Registered User
 
Join Date: Jun 2003
Location: , , United Kingdom.
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi,

The JOptionPane class in the javax.swing package does exactly what you need (http://java.sun.com/j2se/1.4.1/docs/...ptionPane.html).
You can show a dialog using JOptionPane.showOptionDialog(), which takes a number of parameters for the text, button type etc.

If you want to do your own class for this, you could use a similar idea. You can download the source code for Java from the Sun site, couldn't find exactly where, try http://wwws.sun.com/software/java2/download.html

Regards
Philip
Reply With Quote
  #3 (permalink)  
Old June 4th, 2003, 08:09 AM
Registered User
 
Join Date: Jun 2003
Location: Montevideo, Montevideo, Uruguay.
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi I apologise for my english.

But I believe that I can help you

Frame Dialogo= new Frame();
FileDialog d = new FileDialog(Dialogo,"Abrir Encuesta");
d.setVisible(true);
String fname=d.getFile();

This is a java code for open file. If you modify this code, for example add 2 buttons in frame.
After you must create action listener events for buttons.

Other java code for ErrorMessage or ErrorDialog is follow

import java.awt.*;

/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2002</p>
 * <p>Company: </p>
 * @author Eduardo Bieñkowski
 * @version 1.0
 */

public class ErrorDialog extends Dialog {
public ErrorDialog(Frame parent, boolean modal)
    {
        super(parent, modal);

        // components to the visual environment. It instantiates and initializes
        // the components. To modify the code, only use code syntax that matches
        // what Visual Cafe can generate, or Visual Cafe may be unable to back
        // parse your Java file into its visual environment.

        //{{INIT_CONTROLS
        setLayout(null);
        setVisible(false);
        setSize(249,150);
        setBackground(new Color(-4198403));
        label1 = new java.awt.Label();
        label1.setBounds(40,35,166,21);
        label1.setBackground(new Color(-4198403));
        add(label1);
        okButton = new java.awt.Button();
        okButton.setLabel("OK");
        okButton.setBounds(95,85,66,27);
        okButton.setBackground(new Color(12632256));
        add(okButton);
        setTitle("About");
        //}}

        //{{REGISTER_LISTENERS
        SymWindow aSymWindow = new SymWindow();
        this.addWindowListener(aSymWindow);
        SymAction lSymAction = new SymAction();
        okButton.addActionListener(lSymAction);
        //}}

    }

    public ErrorDialog(Frame parent, boolean modal, String titulo)
    {
        super(parent, modal);
//es la copia del constructor del visualcafe pero con el título de la etiqueta para agregarlo

        //{{INIT_CONTROLS
        setLayout(null);
        setVisible(false);
        setSize(249,150);
        setBackground(new Color(-4198403));
        label1 = new java.awt.Label(titulo);
        label1.setBounds(40,35,166,23);
        label1.setBackground(new Color(-4198403));
        add(label1);
        okButton = new java.awt.Button();
        okButton.setLabel("OK");
        okButton.setBounds(95,85,66,27);
        okButton.setBackground(new Color(12632256));
        add(okButton);
        setTitle("Error");
        //}}

        //{{REGISTER_LISTENERS
        SymWindow aSymWindow = new SymWindow();
        this.addWindowListener(aSymWindow);
        SymAction lSymAction = new SymAction();
        okButton.addActionListener(lSymAction);
        //}}

    }

    public ErrorDialog(Frame parent, String title, boolean modal)
    {
        this(parent, modal);
        setTitle(title);
    }

    public void addNotify()
    {
        // Record the size of the window prior to calling parents addNotify.
                Dimension d = getSize();

        super.addNotify();

        // Only do this once.
        if (fComponentsAdjusted)
            return;

        // Adjust components according to the insets
        setSize(getInsets().left + getInsets().right + d.width, getInsets().top + getInsets().bottom + d.height);
        Component components[] = getComponents();
        for (int i = 0; i < components.length; i++)
        {
            Point p = components[i].getLocation();
            p.translate(getInsets().left, getInsets().top);
            components[i].setLocation(p);
        }

        // Used for addNotify check.
        fComponentsAdjusted = true;
    }

    public void setVisible(boolean b)
    {
        if (b)
        {
            Rectangle bounds = getParent().getBounds();
            Rectangle abounds = getBounds();

            setLocation(bounds.x + (bounds.width - abounds.width)/ 2,
                 bounds.y + (bounds.height - abounds.height)/2);
        }

        super.setVisible(b);
    }

    //{{DECLARE_CONTROLS
    java.awt.Label label1;
    java.awt.Button okButton;
    //}}

        // Used for addNotify check.
    boolean fComponentsAdjusted = false;

    class SymWindow extends java.awt.event.WindowAdapter
    {
        public void windowClosing(java.awt.event.WindowEvent event)
        {
            Object object = event.getSource();
            if (object == ErrorDialog.this)
                AboutDialog_WindowClosing(event);
        }
    }

    void AboutDialog_WindowClosing(java.awt.event.WindowEve nt event)
    {
                dispose();
    }

    class SymAction implements java.awt.event.ActionListener
    {
        public void actionPerformed(java.awt.event.ActionEvent event)
        {
            Object object = event.getSource();
            if (object == okButton)
                okButton_Clicked(event);
        }
    }

    void okButton_Clicked(java.awt.event.ActionEvent event)
    {
        //{{CONNECTION
        // Clicked from okButton Hide the Dialog
                dispose();
        //}}
    }


}


regards
Eduardo since Uruguay

edukoski@adinet.com.uy

Edduardo B.
Montevideo
Uruguay
Reply With Quote
  #4 (permalink)  
Old June 4th, 2003, 08:50 PM
Registered User
 
Join Date: Jun 2003
Location: , , .
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to ampyx.living
Default

When I tried to create a GUI, I would like to add each actionListener to each component, the efficiency is no obvious difference between one function and multiple functions(each has one like buttons or whatever). As I felt, it is more convinient for you and other programmer to maintain your codes.
Your idea is to create a Dialog to display information, type and options? Isn't it? If that, you can use JOptionPane (it is the way java offers).
A great help for us is Java API Specification. It is quite helpful!
(I am also a beginner, I 'd like to hear any experience from you guys.)
Reply With Quote
  #5 (permalink)  
Old June 5th, 2003, 03:34 PM
Registered User
 
Join Date: Jun 2003
Location: Berlin, , Germany.
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

hi pic_az

philip is right, best way is to use JOptionPane.
JOptionPane has 4 methods you can use.

1.) showMessageDialog //no return-value
2.) showConfirmDialog // returns an integer-value
3.) showOptionDialog // returns an integer-value
4.) showInputDialog //returns a String

here is little example:
/**
*Creates an Dialog-Frame with OK and CANCEL Button, an the an WARNING-
*Symbol.
*/
int selection = JOptionPane.showConfirmDialog(parent, "//your message", "//Dialog-Title", JOptionPane.OK_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE);
if(selection == JOptionPane.OK_OPTION){
//what is to do when the OK-Button is pressed
}


/**
*Creates an Dialog-Frame with OK Button, an the an WARNING-
*Symbol. No Return-Value!
*/
JOptionPane.showConfirmDialog(parent, "//your message", "//Dialog-Title", JOptionPane.WARNING_MESSAGE);

more possible Message-Types are:
JOptionPane.ERROR_MESSAGE
JOptionPane.INFORMATION_MESSAGE
JOptionPane.QUESTION_MESSAGE
JOptionPane.PLAIN_MESSAGE

ok that´s all

my english is not the best, greetings from Berlin (Germany)
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
replace Word dialog with my designing dialog doublelucky Visual C++ 0 March 19th, 2007 02:27 AM
Scale Dialog Class; Pages 838/9; Ch16 Nick Y BOOK: Ivor Horton's Beginning Visual C++ 2005 0 July 25th, 2006 05:02 AM
Regarding Class Library (.dll) from class file manish.sharma04 BOOK: Wrox's ASP.NET 2.0 Visual Web Developer 2005 Express Edition Starter ISBN: 978-0-7645-8807-5 1 March 3rd, 2006 06:32 AM
Capturing toolbar events inside a dialog class pazzuzu Visual C++ 0 October 13th, 2004 01:35 AM
How to include c# class and vb class in the .vbprj umeshayk VS.NET 2002/2003 2 January 8th, 2004 11:08 PM



All times are GMT -4. The time now is 05:16 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.