Article View: borland.public.jbuilder.java.language
Article #7227Re: Combine methods
From: Joi Ellis [TeamB]
Date: Wed, 04 Jul 2001 04:05
Date: Wed, 04 Jul 2001 04:05
130 lines
5306 bytes
5306 bytes
KK <kk@hotmail.com> wrote:
> Sorry Joi. Yes, I made a mistake! The codes should be:
> public void showDlgSimLib() {
> dlg_ec_simlib dlg = new dlg_ec_simlib(this);
> Dimension dlgSize = dlg.getPreferredSize();
> Dimension frmSize = getSize();
> Point loc = getLocation();
> dlg.setLocation((frmSize.width - dlgSize.width) / 2 + loc.x,
> (frmSize.height - dlgSize.height) / 2 + loc.y);
> dlg.show();
> }
> public void showDlgSynLib() {
> dlg_ec_synlib dlg = new dlg_ec_synlib(this);
> Dimension dlgSize = dlg.getPreferredSize();
> Dimension frmSize = getSize();
> Point loc = getLocation();
> dlg.setLocation((frmSize.width - dlgSize.width) / 2 + loc.x,
> (frmSize.height - dlgSize.height) / 2 + loc.y);
> dlg.show();
> }
> I want to combine them into one so that I can manage them easier. I find a
> problem that since the dialog classes (dlg_ec_simlib and dlg_ec_synlib) are
> different so that I cannot use the dialog object as the parameter of the
> method.
Do both classes extend JDialog? If so, try passing JDialog.
public void showDlgSimLib() {
dlg_ec_simlib dlg = new dlg_ec_simlib(this);
showDlg( dlg );
}
public void showDlgSynLib() {
dlg_ec_synlib dlg = new dlg_ec_synlib(this);
showDlg( dlg );
}
public void showDlg(JDialog dlg) {
Dimension dlgSize = dlg.getPreferredSize();
Dimension frmSize = getSize();
Point loc = getLocation();
dlg.setLocation((frmSize.width - dlgSize.width) / 2 + loc.x,
(frmSize.height - dlgSize.height) / 2 + loc.y);
dlg.show();
}
If the two classes are completely lacking in shared DNA, use a
marker interface, assuming you have source for both classes.
Define an interface, call it "Foo" for lack of something more descriptive.
Foo doesn't need to define any methods, it just has to exist.
Now edit both classes to say they implement Foo. Now pass them
as type Foo.
public void showDlgSimLib() {
dlg_ec_simlib dlg = new dlg_ec_simlib(this);
showDlg( dlg );
}
public void showDlgSynLib() {
dlg_ec_synlib dlg = new dlg_ec_synlib(this);
showDlg( dlg );
}
public void showDlg(Foo dlg) {
Dimension dlgSize = dlg.getPreferredSize();
Dimension frmSize = getSize();
Point loc = getLocation();
dlg.setLocation((frmSize.width - dlgSize.width) / 2 + loc.x,
(frmSize.height - dlgSize.height) / 2 + loc.y);
dlg.show();
}
> Ken
> "Joi Ellis [TeamB]" <joi@aravox.com> wrote in message
> news:3b414f60_2@dnews...
>> KK <kk@hotmail.com> wrote:
>> > Dear all:
>>
>> > Is there any solution to combine the following methods into one? Since
> they
>> > look similar, I would like to combine them and make the program
> simplier.
>>
>> > Ken
>>
>> > +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
>>
>> > public void showDlgSimLib() {
>> > dlg_ec_simlib dlg = new dlg_ec_simlib(this);
>> > Dimension dlgSize = dlg.getPreferredSize();
>> > Dimension frmSize = getSize();
>> > Point loc = getLocation();
>> > dlg.setLocation((frmSize.width - dlgSize.width) / 2 + loc.x,
>> > (frmSize.height - dlgSize.height) / 2 + loc.y);
>> > dlg.show();
>> > }
>>
>> > public void showDlgSimLib() {
>> > dlg_ec_simlib dlg = new dlg_ec_simlib(this);
>> > Dimension dlgSize = dlg.getPreferredSize();
>> > Dimension frmSize = getSize();
>> > Point loc = getLocation();
>> > dlg.setLocation((frmSize.width - dlgSize.width) / 2 + loc.x,
>> > (frmSize.height - dlgSize.height) / 2 + loc.y);
>> > dlg.show();
>> > }
>>
>> Uhh. These two fragments are identical. Did you intend to paste in
>> a different fragment?
>>
>>
>> --
>> Joi Ellis (TeamB) http://www.teamb.com/
>> No direct email, please.
>> For best results, post on news server newsgroups.borland.com!
>> Newsgroup Info: http://www.borland.com/newsgroups/guide.html
--
Joi Ellis (TeamB) http://www.teamb.com/
No direct email, please.
For best results, post on news server newsgroups.borland.com!
Newsgroup Info: http://www.borland.com/newsgroups/guide.html
Technical Details
Message-ID:
<3b4295f7_1@dnews>Path:
arc1-ca.pugleaf.net!archiver1.google.com!newsfeed.google.com!sn-xit-02!sn-xit-04!supernews.com!feed.textport.net!news.stealth.net!news.maxwell.syr.edu!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!dnews!not-for-mailReferences:
<3b414874_1@dnews> <3b414f60_2@dnews> <3b4288c8_1@dnews>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: f74b3,b25916793f54693d
X-Google-Attributes: gidf74b3,public
X-Google-ArrivalTime: 2001-07-03 21:08:37 PST
Message-ID: <3b4295f7_1@dnews>
From: "Joi Ellis [TeamB]" <joi@aravox.com>
Subject: Re: Combine methods
Newsgroups: borland.public.jbuilder.java.language
References: <3b414874_1@dnews> <3b414f60_2@dnews> <3b4288c8_1@dnews>
User-Agent: tin/1.4.2-20000205 ("Possession") (UNIX) (Linux/2.2.16-3 (i686))
NNTP-Posting-Host: 209.46.41.66
Date: 3 Jul 2001 21:05:11 -0700
X-Trace: dnews 994219511 209.46.41.66 (3 Jul 2001 21:05:11 -0700)
Lines: 130
Path: arc1-ca.pugleaf.net!archiver1.google.com!newsfeed.google.com!sn-xit-02!sn-xit-04!supernews.com!feed.textport.net!news.stealth.net!news.maxwell.syr.edu!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!dnews!not-for-mail