Link to home
Start Free TrialLog in
Avatar of Barca
BarcaFlag for Hong Kong

asked on

design own JDialog and return the owner Jframe

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,
ASKER CERTIFIED SOLUTION
Avatar of aozarov
aozarov

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of aozarov
aozarov

and forgot this:
 public CD getaddcd(){
 addcd addcd1 = new addcd();

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

You want to dialog to be modal on that JFrame, right?
Avatar of Barca

ASKER

YES
So, do the above and pass the Jframe parent to the constructor of JDialog
Avatar of Barca

ASKER

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
Avatar of Barca

ASKER

/*
 * 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;
    }
}
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));
                }
Avatar of Barca

ASKER

i think i should give you more information....wait
Avatar of Barca

ASKER



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;
    }
}
Avatar of Barca

ASKER


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;
    }
}
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...
Avatar of Barca

ASKER

/*
 * 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;
    }
}
Avatar of Barca

ASKER

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)
Avatar of Barca

ASKER

or can you give me a workable sample?
Right, assuming LibrarySystem is your JFrame...
cdda.addcd(addcd.getcd(LibrarySystem.this));
Avatar of Barca

ASKER

it is work, but the record was added a intialized CD...
Avatar of Barca

ASKER

and i want to ask....why LibrarySystem.this
>> 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;
Avatar of Barca

ASKER

oic, you solved my problem, thanks