Barca
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.addActionListene r(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,lblco mment,lbla rtist,lbln umberoftra cks;
private JTextField txttitle,txtplaytime,txtar tist,txtnu mberoftrac ks;
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(lblnumberoft racks);
container.add(txtnumberoft racks);
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).equal s("")){
flag = true;
}
return this.flag;
}
public void getcd(){
if(this.inputvalid() == true){
this.tempcd = new CD(txttitle.getText(),Inte ger.parseI nt(txtplay time.getTe xt()), tacomment.getText(), txtartist.getText(),Intege r.parseInt (txtnumber oftracks.g etText())) ;
}else{
}
}
public CD getaddcd(){
addcd addcd1 = new addcd();
addcd1.setVisible(true);
return this.tempcd;
}
}
best regards,
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.addActionListene
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,lblco
private JTextField txttitle,txtplaytime,txtar
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
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(lblnumberoft
container.add(txtnumberoft
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).equal
flag = true;
}
return this.flag;
}
public void getcd(){
if(this.inputvalid() == true){
this.tempcd = new CD(txttitle.getText(),Inte
}else{
}
}
public CD getaddcd(){
addcd addcd1 = new addcd();
addcd1.setVisible(true);
return this.tempcd;
}
}
best regards,
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
You want to dialog to be modal on that JFrame, right?
ASKER
YES
So, do the above and pass the Jframe parent to the constructor of JDialog
ASKER
addButton.addActionListene r(new ActionListener() {
public void actionPerformed( ActionEvent event ){
if(mode ==0){
cdda.addcd(addcd().getaddc d(this));
}
}
}
);
D:\LibrarySystem\src\libra rysystem\L ibrarySyst em.java:19 1: cannot resolve symbol
symbol: method addcd ()
cdda.addcd(addcd().getaddc d(this));
1 error
public void actionPerformed( ActionEvent event ){
if(mode ==0){
cdda.addcd(addcd().getaddc
}
}
}
);
D:\LibrarySystem\src\libra
symbol: method addcd ()
cdda.addcd(addcd().getaddc
1 error
ASKER
/*
* addcd.java
*
* Created on 2005年3月28 6085;, 上午 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,lblco mment,lbla rtist,lbln umberoftra cks;
private JTextField txttitle,txtplaytime,txtar tist,txtnu mberoftrac ks;
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(lblnumberoft racks);
container.add(txtnumberoft racks);
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).equal s("")){
flag = true;
}
return this.flag;
}
public void getcd(){
if(this.inputvalid() == true){
this.tempcd = new CD(txttitle.getText(),Inte ger.parseI nt(txtplay time.getTe xt()), tacomment.getText(), txtartist.getText(),Intege r.parseInt (txtnumber oftracks.g etText())) ;
}else{
}
}
public CD getaddcd(JFrame frame){
addcd addcd1 = new addcd(frame);
addcd1.setVisible(true);
return this.tempcd;
}
}
* addcd.java
*
* Created on 2005年3月28
*/
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,lblco
private JTextField txttitle,txtplaytime,txtar
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
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(lblnumberoft
container.add(txtnumberoft
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).equal
flag = true;
}
return this.flag;
}
public void getcd(){
if(this.inputvalid() == true){
this.tempcd = new CD(txttitle.getText(),Inte
}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().getaddc d(this));
}
to
if(mode ==0){
cdda.addcd(addcd.getaddcd( this));
}
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().getaddc
}
to
if(mode ==0){
cdda.addcd(addcd.getaddcd(
}
ASKER
i think i should give you more information....wait
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;
}
}
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.showMessageD
//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.showMessageDia
}
catch ( FileNotFoundException e ) {
JOptionPane.showMessageDia
}
catch ( IOException e ) {
JOptionPane.showMessageDia
}
}
public void savedb(){
try {
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(new File("CDDB.txt")));
out.writeObject(this.cdvec
out.close();
}
catch ( FileNotFoundException e ) {
JOptionPane.showMessageDia
}
catch ( IOException e ) {
JOptionPane.showMessageDia
}
}
public String addcd(CD cd){
String result="";
int temp=0, i=0;
this.cdvector.addElement(c
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()
for(i = 0;i<this.cdvector.size();i
tempcd = (CD)this.cdvector.elementA
if((tempcd.getid()).equals
this.cdvector.removeElemen
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()
for(int i = 0;i<this.cdvector.size();i
tempcd = (CD)this.cdvector.elementA
if(mode == 0){
if((tempcd.getTitle().inde
buffer += tempcd.ToString() + "\n";
}
}else if(mode ==1){
if((tempcd.getArtist().ind
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()
for(int j = 0;j<this.cdvector.size();j
tempcd = (CD)this.cdvector.elementA
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.elementA
if((tempcd.getArtist()).eq
allcd += tempcd.ToString() + "\n";
}
}}
return allcd;
}
else{
return "No CD in Database";
}
}else if(mode == 0){
if(this.cdvector.isEmpty()
for(int j = 0;j<this.cdvector.size();j
tempcd = (CD)this.cdvector.elementA
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.elementA
if((tempcd.getTitle().equa
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...
addcd()....
This is why should either make a static method to getaddcd
e.g.
cdda.addcd(addcd.getaddcd(
or do
cdda.addcd(new addcd().getaddcd(this));
Also, temcd in the addcd dialog is being intialized only when you call getcd...
ASKER
/*
* addcd.java
*
* Created on 2005年3月28 6085;, 上午 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,lblco mment,lbla rtist,lbln umberoftra cks;
private JTextField txttitle,txtplaytime,txtar tist,txtnu mberoftrac ks;
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(lblnumberoft racks);
container.add(txtnumberoft racks);
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).equal s("")){
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.txt playtime.g etText()), addcd1.tacomment.getText() , addcd1.txtartist.getText() ,Integer.p arseInt(ad dcd1.txtnu mberoftrac ks.getText ()));
return addcd1.tempcd;
}
return addcd1.tempcd;
}
}
* addcd.java
*
* Created on 2005年3月28
*/
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,lblco
private JTextField txttitle,txtplaytime,txtar
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
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(lblnumberoft
container.add(txtnumberoft
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).equal
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
return addcd1.tempcd;
}
return addcd1.tempcd;
}
}
ASKER
D:\LibrarySystem\src\libra rysystem\L ibrarySyst em.java:19 1: getcd(javax.swing.JFrame) in addcd cannot be applied to (<anonymous java.awt.event.ActionListe ner>)
cdda.addcd(addcd.getcd(thi s));
1 error
BUILD FAILED (total time: 0 seconds)
cdda.addcd(addcd.getcd(thi
1 error
BUILD FAILED (total time: 0 seconds)
ASKER
or can you give me a workable sample?
Right, assuming LibrarySystem is your JFrame...
cdda.addcd(addcd.getcd(Lib rarySystem .this));
cdda.addcd(addcd.getcd(Lib
ASKER
it is work, but the record was added a intialized CD...
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.txt playtime.g etText()), addcd1.tacomment.getText() , addcd1.txtartist.getText() ,Integer.p arseInt(ad dcd1.txtnu mberoftrac ks.getText ()));
return addcd1.tempcd;
}
return addcd1.tempcd;
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
return addcd1.tempcd;
}
return addcd1.tempcd;
ASKER
oic, you solved my problem, thanks
public CD getaddcd(){
addcd addcd1 = new addcd();
with
public CD getaddcd(JFrame frame){
addcd addcd1 = new addcd(frame);