[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

design own JDialog and return the owner Jframe

Posted on 2005-04-25
20
Medium Priority
?
564 Views
Last Modified: 2008-01-09
hi guys,
as the topic, i want to design my own JDialog and it return value.
here is part of my code, JDialog is very strange to me, pls help

---------------
in JFrame...

addButton.addActionListener(new ActionListener() {
            public void actionPerformed( ActionEvent event ){
                if(mode ==0){
                    inputcd = new CD(addcd().getaddcd());
                }
            }
        }
        );

---------------


import javax.swing.*;
import javax.swing.event.*;
import java.awt.event.*;
import java.awt.*;

public class addcd extends JDialog{
    private JButton okButton,cancelButton;
    private JLabel lbltitle,lblplaytime,lblcomment,lblartist,lblnumberoftracks;
    private JTextField txttitle,txtplaytime,txtartist,txtnumberoftracks;
    private JTextArea tacomment;
    private CD tempcd;
    private boolean flag = false;
    /** Creates a new instance of addcd */
    public addcd() {
        Container container = getContentPane();
        container.setLayout(new GridLayout( 6, 2 ));
        lbltitle = new JLabel("Title");
        lblplaytime = new JLabel("Play Time");
        lblcomment = new JLabel("Comment");
        lblartist = new JLabel("Artist");
        lblnumberoftracks = new JLabel("Number OF Tracks");
        txttitle = new JTextField();
        txtplaytime = new JTextField();
        txtartist = new JTextField();
        txtnumberoftracks = new JTextField();
        tacomment = new JTextArea();
        okButton = new JButton("OK");
        okButton.addActionListener(new ActionListener() {
            public void actionPerformed( ActionEvent event ){
                if(inputvalid() == true){
                    flag =true;
                }
            }
        }
        );
        cancelButton = new JButton("Cancel");
        container.add(lbltitle);
        container.add(txttitle);
        container.add(lblplaytime);
        container.add(txtplaytime);
        container.add(lblartist);
        container.add(txtartist);
        container.add(lblnumberoftracks);
        container.add(txtnumberoftracks);
        container.add(lblcomment);
        container.add(tacomment);
        container.add(okButton);
        container.add(cancelButton);
        setLocation(10, 10);
        setSize(200, 100);
        setVisible(true);
        setModel(true);
    }
    public boolean inputvalid(){
        if(!(txttitle).equals("") & !(txtplaytime).equals("") & !(txtartist).equals("")& !(txtnumberoftracks).equals("")){
            flag = true;
        }
        return this.flag;
    }
    public void getcd(){
        if(this.inputvalid() == true){
            this.tempcd = new CD(txttitle.getText(),Integer.parseInt(txtplaytime.getText()), tacomment.getText(), txtartist.getText(),Integer.parseInt(txtnumberoftracks.getText()));
        }else{
           
        }
    }
    public CD getaddcd(){
        addcd addcd1 = new addcd();
        addcd1.setVisible(true);
        return this.tempcd;
    }
}



best regards,
0
Comment
Question by:Barca
  • 12
  • 8
20 Comments
 
LVL 15

Accepted Solution

by:
aozarov earned 440 total points
ID: 13860398
replace:
public addcd() {
{
with
public addcd(Frame frame)  {
super(frame);

and
inputcd = new CD(addcd().getaddcd());
with
inputcd = new CD(addcd().getaddcd(this));
0
 
LVL 15

Expert Comment

by:aozarov
ID: 13860404
and forgot this:
 public CD getaddcd(){
 addcd addcd1 = new addcd();

with
 public CD getaddcd(JFrame frame){
 addcd addcd1 = new addcd(frame);

0
 
LVL 15

Expert Comment

by:aozarov
ID: 13860412
You want to dialog to be modal on that JFrame, right?
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 3

Author Comment

by:Barca
ID: 13860462
YES
0
 
LVL 15

Expert Comment

by:aozarov
ID: 13860485
So, do the above and pass the Jframe parent to the constructor of JDialog
0
 
LVL 3

Author Comment

by:Barca
ID: 13860495
addButton.addActionListener(new ActionListener() {
            public void actionPerformed( ActionEvent event ){
                if(mode ==0){
                    cdda.addcd(addcd().getaddcd(this));
                }
            }
        }
        );


D:\LibrarySystem\src\librarysystem\LibrarySystem.java:191: cannot resolve symbol
symbol: method addcd ()
                    cdda.addcd(addcd().getaddcd(this));
1 error
0
 
LVL 3

Author Comment

by:Barca
ID: 13860516
/*
 * addcd.java
 *
 * Created on 2005年3月28日, 上午 12:19
 */

import javax.swing.*;
import javax.swing.event.*;
import java.awt.event.*;
import java.awt.*;
/**
 *
 * @author LeungKen
 */
public class addcd extends JDialog{
    private JButton okButton,cancelButton;
    private JLabel lbltitle,lblplaytime,lblcomment,lblartist,lblnumberoftracks;
    private JTextField txttitle,txtplaytime,txtartist,txtnumberoftracks;
    private JTextArea tacomment;
    private CD tempcd;
    private boolean flag = false;
    /** Creates a new instance of addcd */
    public addcd(JFrame frame) {
        super(frame);
        Container container = getContentPane();
        container.setLayout(new GridLayout( 6, 2 ));
        lbltitle = new JLabel("Title");
        lblplaytime = new JLabel("Play Time");
        lblcomment = new JLabel("Comment");
        lblartist = new JLabel("Artist");
        lblnumberoftracks = new JLabel("Number OF Tracks");
        txttitle = new JTextField();
        txtplaytime = new JTextField();
        txtartist = new JTextField();
        txtnumberoftracks = new JTextField();
        tacomment = new JTextArea();
        okButton = new JButton("OK");
        okButton.addActionListener(new ActionListener() {
            public void actionPerformed( ActionEvent event ){
                if(inputvalid() == true){
                    flag =true;
                }
            }
        }
        );
        cancelButton = new JButton("Cancel");
        container.add(lbltitle);
        container.add(txttitle);
        container.add(lblplaytime);
        container.add(txtplaytime);
        container.add(lblartist);
        container.add(txtartist);
        container.add(lblnumberoftracks);
        container.add(txtnumberoftracks);
        container.add(lblcomment);
        container.add(tacomment);
        container.add(okButton);
        container.add(cancelButton);
        setLocation(10, 10);
        setSize(200, 100);
        setVisible(true);
        //setModel(true);
    }
    public boolean inputvalid(){
        if(!(txttitle).equals("") & !(txtplaytime).equals("") & !(txtartist).equals("")& !(txtnumberoftracks).equals("")){
            flag = true;
        }
        return this.flag;
    }
    public void getcd(){
        if(this.inputvalid() == true){
            this.tempcd = new CD(txttitle.getText(),Integer.parseInt(txtplaytime.getText()), tacomment.getText(), txtartist.getText(),Integer.parseInt(txtnumberoftracks.getText()));
        }else{
           
        }
    }
    public CD getaddcd(JFrame frame){
        addcd addcd1 = new addcd(frame);
        addcd1.setVisible(true);
        return this.tempcd;
    }
}
0
 
LVL 15

Expert Comment

by:aozarov
ID: 13860551
change:
public CD getaddcd(JFrame frame){
        addcd addcd1 = new addcd(frame);
        addcd1.setVisible(true);
        return this.tempcd;
    }

to
public static CD getaddcd(JFrame frame){
        addcd addcd1 = new addcd(frame);
        addcd1.setVisible(true);
        return addcd1.getcd();
    }

and
  if(mode ==0){
                    cdda.addcd(addcd().getaddcd(this));
                }
to
  if(mode ==0){
                    cdda.addcd(addcd.getaddcd(this));
                }
0
 
LVL 3

Author Comment

by:Barca
ID: 13860592
i think i should give you more information....wait
0
 
LVL 3

Author Comment

by:Barca
ID: 13860596


import java.util.Date;

public class CD extends Item{
    private String artist;
    private int numberOfTracks;
    /** Creates a new instance of CD */
    public CD(String title, int playTime, String comment, String artist, int numberOfTracks) {
        this.title = title;
        this.playTime = playTime;
        this.comment = comment;
        this.available = true;
        this.artist = artist;
        this.numberOfTracks = numberOfTracks;
        this.id ="CD"+( new Date().getTime());
    }
    public String getArtist(){
        return this.artist;
    }
    public int getNumberOfTracks(){
        return this.numberOfTracks;
    }
    public void setArtist(String newArtist){
        this.artist = newArtist;
    }
    public void setNumberOfTracks(int newNumberOfTracks){
        this.numberOfTracks = newNumberOfTracks;
    }
    public String ToString(){
        String temp = this.id +"\t"+this.title +"\t"+ this.playTime +"\t"+this.comment +"\t"+ this.artist +"\t"+ this.numberOfTracks +"\t"+this.available+ "\n";
        return temp;
    }
}
0
 
LVL 3

Author Comment

by:Barca
ID: 13860602

import javax.swing.*;
import javax.swing.event.*;
import java.awt.event.*;
import java.awt.*;
import java.io.*;
import java.util.Vector;

public class CDDA {
    private CDVector cdvector;// = new CDVector();
    /** Creates a new instance of CDDB */
    public CDDA(){
        //JOptionPane.showMessageDialog(null,file);
        //savedb();
        readdb();
    }
    public void readdb(){
        try {
            ObjectInputStream in = new ObjectInputStream(new FileInputStream(new File("CDDB.txt")));
            this.cdvector = (CDVector)in.readObject();
            in.close();
        }
        catch ( ClassNotFoundException e ) {
            JOptionPane.showMessageDialog(null,"ClassNotFoundException...");
        }
        catch ( FileNotFoundException e ) {
            JOptionPane.showMessageDialog(null,"FileNotFoundException...");
        }
        catch ( IOException e ) {
            JOptionPane.showMessageDialog(null,"IOException...");
        }    
    }
    public void savedb(){
        try {
            ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(new File("CDDB.txt")));
            out.writeObject(this.cdvector);
            out.close();
        }
        catch ( FileNotFoundException e ) {
           JOptionPane.showMessageDialog(null,"FileNotFoundException...");
        }
        catch ( IOException e ) {
            JOptionPane.showMessageDialog(null,"IOException...");
        }  
    }
    public String addcd(CD cd){
        String result="";
        int temp=0, i=0;
        this.cdvector.addElement(cd);
        this.savedb();
        result = "CD " + cd.getTitle()+ " added to database";
        return result;
    }
    public String delcd(String id){
        int temp=0, i=0;
        String result="";
        CD tempcd = null;
        if(this.cdvector.isEmpty() == false){
            for(i = 0;i<this.cdvector.size();i++){
                tempcd = (CD)this.cdvector.elementAt(i);
                if((tempcd.getid()).equals(id)){
                    this.cdvector.removeElementAt(i);
                    result = "CD "+ id + " deleted";
                    this.savedb();
                }
            }
        }
        return result;
    }
   
    public String findcd(int mode , String name){
        int temp=0;
        String buffer="";
        CD tempcd = null;
        if(this.cdvector.isEmpty() == false){
            for(int i = 0;i<this.cdvector.size();i++){
                tempcd = (CD)this.cdvector.elementAt(i);
                if(mode == 0){
                    if((tempcd.getTitle().indexOf(name))>= 0){
                        buffer += tempcd.ToString() + "\n";
                    }
                }else if(mode ==1){
                    if((tempcd.getArtist().indexOf(name))>= 0){
                        buffer += tempcd.ToString() + "\n";
                    }
                }
            }
            return buffer;
        }
        return "No record matched";
    }
   
    public String printoutallcd(int mode){
        String array[] = new String[this.cdvector.size()];
        int temp=0;
        String allcd="";
        CD tempcd = null;
       
        if(mode == 1){
        if(this.cdvector.isEmpty() == false){
            for(int j = 0;j<this.cdvector.size();j++){
            tempcd = (CD)this.cdvector.elementAt(j);
            array[j] = tempcd.getArtist();
            }
            bubbleSort(array);
            for(int k = 0;k<array.length-1;k++){
            for(int i = 0;i<cdvector.size();i++){
                tempcd = (CD)this.cdvector.elementAt(i);
                 if((tempcd.getArtist()).equals(array[k])){
                     allcd += tempcd.ToString() + "\n";
                }
            }}
            return allcd;
        }
        else{
            return "No CD in Database";
        }
        }else if(mode == 0){
            if(this.cdvector.isEmpty() == false){
            for(int j = 0;j<this.cdvector.size();j++){
            tempcd = (CD)this.cdvector.elementAt(j);
            array[j] = tempcd.getTitle();
            }
            bubbleSort(array);
            for(int k = 0;k<array.length-1;k++){
            for(int i = 0;i<cdvector.size();i++){
                tempcd = (CD)this.cdvector.elementAt(i);
                 if((tempcd.getTitle().equals(array[k]))){
                     allcd += tempcd.ToString() + "\n";
                }
            }}
            return allcd;
            }else{
                return "No CD in Database";
            }
        }
        return "No CD in Database";
    }
    public void bubbleSort(String array2[] )
    {
        // loop to control number of passes
        for ( int pass = 1; pass < array2.length; pass++ ) {
           
            // loop to control number of comparisons
            for ( int element = 0;element < array2.length - 1;element++ ) {
               
                // compare side-by-side elements and swap them if
                // first element is greater than second element
                if ( array2[ element ].compareTo(array2[ element + 1 ]) >= 0 )
                    swap( array2, element, element + 1 );

            } // end loop to control comparisons
           
        } // end loop to control passes

    } // end method bubbleSort

    // swap two elements of an array
    public void swap( String array3[], int first, int second )
    {
        String hold;
       
        hold = array3[ first ];
        array3[ first ] = array3[ second ];
        array3[ second ] = hold;
    }
}
0
 
LVL 15

Expert Comment

by:aozarov
ID: 13860645
That is fine but you cant call a constructor:
addcd()....
This is why should either make a static method to getaddcd
e.g.
cdda.addcd(addcd.getaddcd(this));
or do

cdda.addcd(new addcd().getaddcd(this));

Also,  temcd in the addcd dialog is being intialized only when you call getcd...
0
 
LVL 3

Author Comment

by:Barca
ID: 13860785
/*
 * addcd.java
 *
 * Created on 2005&#24180;3&#26376;28&#26085;, &#19978;&#21320; 12:19
 */

import javax.swing.*;
import javax.swing.event.*;
import java.awt.event.*;
import java.awt.*;
/**
 *
 * @author LeungKen
 */
public class addcd extends JDialog{
    private JButton okButton,cancelButton;
    private JLabel lbltitle,lblplaytime,lblcomment,lblartist,lblnumberoftracks;
    private JTextField txttitle,txtplaytime,txtartist,txtnumberoftracks;
    private JTextArea tacomment;
    private CD tempcd = new CD("String title",56,"String comment", "String artist",12);
    private boolean flag = false;
    /** Creates a new instance of addcd */
    public addcd(JFrame frame) {
        super(frame);
        Container container = getContentPane();
        container.setLayout(new GridLayout( 6, 2 ));
        lbltitle = new JLabel("Title");
        lblplaytime = new JLabel("Play Time");
        lblcomment = new JLabel("Comment");
        lblartist = new JLabel("Artist");
        lblnumberoftracks = new JLabel("Number OF Tracks");
        txttitle = new JTextField();
        txtplaytime = new JTextField();
        txtartist = new JTextField();
        txtnumberoftracks = new JTextField();
        tacomment = new JTextArea();
        okButton = new JButton("OK");
        okButton.addActionListener(new ActionListener() {
            public void actionPerformed( ActionEvent event ){
                if(inputvalid() == true){
                    flag =true;
                }
            }
        }
        );
        cancelButton = new JButton("Cancel");
        container.add(lbltitle);
        container.add(txttitle);
        container.add(lblplaytime);
        container.add(txtplaytime);
        container.add(lblartist);
        container.add(txtartist);
        container.add(lblnumberoftracks);
        container.add(txtnumberoftracks);
        container.add(lblcomment);
        container.add(tacomment);
        container.add(okButton);
        container.add(cancelButton);
        setLocation(10, 10);
        setSize(200, 100);
        setVisible(true);
        //setModel(true);
    }
    public boolean inputvalid(){
        if(!(txttitle).equals("") & !(txtplaytime).equals("") & !(txtartist).equals("")& !(txtnumberoftracks).equals("")){
            flag = true;
        }
        return this.flag;
    }
    public static CD getcd(JFrame frame){
        addcd addcd1 = new addcd(frame);
        addcd1.setVisible(true);
        if(addcd1.flag == true){
            addcd1.tempcd = new CD(addcd1.txttitle.getText(),Integer.parseInt(addcd1.txtplaytime.getText()), addcd1.tacomment.getText(), addcd1.txtartist.getText(),Integer.parseInt(addcd1.txtnumberoftracks.getText()));
            return addcd1.tempcd;
        }
        return addcd1.tempcd;
    }
}
0
 
LVL 3

Author Comment

by:Barca
ID: 13860787
D:\LibrarySystem\src\librarysystem\LibrarySystem.java:191: getcd(javax.swing.JFrame) in addcd cannot be applied to (<anonymous java.awt.event.ActionListener>)
                    cdda.addcd(addcd.getcd(this));
1 error
BUILD FAILED (total time: 0 seconds)
0
 
LVL 3

Author Comment

by:Barca
ID: 13860822
or can you give me a workable sample?
0
 
LVL 15

Expert Comment

by:aozarov
ID: 13861040
Right, assuming LibrarySystem is your JFrame...
cdda.addcd(addcd.getcd(LibrarySystem.this));
0
 
LVL 3

Author Comment

by:Barca
ID: 13861078
it is work, but the record was added a intialized CD...
0
 
LVL 3

Author Comment

by:Barca
ID: 13861101
and i want to ask....why LibrarySystem.this
0
 
LVL 15

Expert Comment

by:aozarov
ID: 13861211
>> and i want to ask....why LibrarySystem.this
Because that code was inside an anonymous inner class this is the way, LibrarySystem.this, to refer to the "this" of the outer class.

>> it is work, but the record was added a intialized CD...
Not sure what you mean. your returned value is based on:
if(addcd1.flag == true){
            addcd1.tempcd = new CD(addcd1.txttitle.getText(),Integer.parseInt(addcd1.txtplaytime.getText()), addcd1.tacomment.getText(), addcd1.txtartist.getText(),Integer.parseInt(addcd1.txtnumberoftracks.getText()));
            return addcd1.tempcd;
        }
        return addcd1.tempcd;
0
 
LVL 3

Author Comment

by:Barca
ID: 13861235
oic, you solved my problem, thanks
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
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 basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses
Course of the Month20 days, 6 hours left to enroll

873 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