Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / June 2006

Tip: Looking for answers? Try searching our database.

Fuction Question

Thread view: 
Barkster - 07 Jun 2006 20:16 GMT
I'm try to use a function to set some text and set the icons of various
labels and I thought I could send the labelnames to the function and
use it to set them but I'm having issues. It doesn't work all the time.
It randomly sets the text of various labels or sometimes none at all.
Can't figure it out. Here is what I have, I've very new to java so I'm
obviously doing something wrong.

public void jButton6_actionPerformed(ActionEvent e) {
       GetThumb(this.jLabel6, this.jTextField6);
}

public void GetThumb(JLabel labelNo, JTextField fieldNo) {
     labelNo.setIcon(thumbnail);
     fieldNo.setText(name);
}
Oliver Wong - 07 Jun 2006 20:51 GMT
> I'm try to use a function to set some text and set the icons of various
> labels and I thought I could send the labelnames to the function and
[quoted text clipped - 11 lines]
>      fieldNo.setText(name);
> }

   It's not clear what the code is supposed to do. For example, you have a
method named "GetThumb" which doesn't seem to get anything. Actually, it
seems to be doing the opposite if getting: namely, it is setting.

   You should take a step back, move away from the code, and come up with a
coherent plan and/or design for whatever it is your application is supposed
to do, perhaps using pencil and paper.

   Aside from that, there isn't much help that can be provided to you,
because you have posted enough code. For example, the variable "thumbnail"
isn't declared or assigned anywhere in the code you posted. See
http://mindprod.com/jgloss/sscce.html

   - Oliver
Barkster - 07 Jun 2006 21:39 GMT
Thanks, that was just a tiny portion of what is really going on, if you
insist. I was hoping it would be simplier with the problem stripped
out.

  public void jButton1_actionPerformed(ActionEvent e) {
       GetThumb(this.jLabel1, this.jTextField1, 1);
   }

   public void GetThumb(JLabel labelNo, JTextField fieldNo, int k) {
       JFileChooser choose = new JFileChooser();
       choose.addChoosableFileFilter(new ImageFilter());
       choose.setAcceptAllFileFilterUsed(false);
       //choose.setFileView(new ImageFileView());

       choose.setAccessory(new ImagePreview(choose));
       choose.setVisible(true);
       int returnVal = choose.showDialog(this, "Select a Image");
       if (choose.getSelectedFile() != null) {
           if (returnVal == JFileChooser.APPROVE_OPTION) {

//fieldNo.setText(choose.getSelectedFile().getAbsolutePath());
               String name =
choose.getSelectedFile().getAbsolutePath();
               ///added by bbb
               int t = 100; //thumbnail size
               ImageIcon thumbnail = null;
               ImageIcon tmpIcon = new ImageIcon(name);
               if (tmpIcon.getIconWidth() > t) {
                   //get image ratio
                   int imageWidth = tmpIcon.getIconWidth();
                   int imageHeight = tmpIcon.getIconHeight();

                   //scaling
                   double scaleX = t / (double) imageHeight;
                   double scaleY = t/ (double) imageWidth;
                   double scale = Math.min(scaleX, scaleY);

                   // Determine size of new image.
                   int w = (int) (scale * imageWidth);
                   int h = (int) (scale * imageHeight);

                   //needs scaling
                   if ((imageWidth > t)||(imageHeight >t)) {

//                        try {
//                            java.net.InetAddress localMachine =
java.net.InetAddress.getLocalHost();
//
fieldNo.setText(String.valueOf(localMachine));
//                        } catch (java.net.UnknownHostException uhe) {
// [beware typo in code sample - dmw]
//                                    // handle exception
//                        }
                       fieldNo.setText(name);
                       imgsrc[k] =
choose.getSelectedFile().getAbsolutePath();
                       imgname[k] =
choose.getSelectedFile().getName();
                       thumbnail = new ImageIcon(tmpIcon.getImage().
                                                 getScaledInstance(w,
h,
                           Image.SCALE_DEFAULT));

                   } else {
                   //fieldNo.setText(String.valueOf(imageRatio));
                   imgsrc[k] =
choose.getSelectedFile().getAbsolutePath();
                   imgname[k] = choose.getSelectedFile().getName();
                   fieldNo.setText(name);
                       thumbnail = new ImageIcon(tmpIcon.getImage().
                                                 getScaledInstance(w,
h,
                           Image.SCALE_DEFAULT));

                   }

               } else {
                   thumbnail = tmpIcon;
               }

JOptionPane.showMessageDialog(this,"FieldNo:"+fieldNo.getName()+"ImageNo:"+labelNo.getName(),
                                       "",
                                       JOptionPane.ERROR_MESSAGE);

               labelNo.setIcon(thumbnail);
           }
       }
   }
> > I'm try to use a function to set some text and set the icons of various
> > labels and I thought I could send the labelnames to the function and
[quoted text clipped - 26 lines]
>
>     - Oliver
Oliver Wong - 07 Jun 2006 22:04 GMT
[post re-ordered]

>>     Aside from that, there isn't much help that can be provided to you,
>> because you have posted enough code. For example, the variable
[quoted text clipped - 5 lines]
> insist. I was hoping it would be simplier with the problem stripped
> out.

[code snipped]

   The code you provided still doesn't compile. You've got methods just
floating around, instead of within a class.

   Also, don't use line comments (the ones that start with //) in code you
post to newsgroup, because the word wrapping will cause confusion as to
which lines are commented out and which aren't.

   Finally, don't forget to actually trim down your code to the minimum
which actually demonstrates your problem. I don't think the line that starts
with "JOptionPane.showMessageDialog" has anything to do with your labels
getting random values in them, for example. Re-read
http://mindprod.com/jgloss/sscce.html

   - Oliver
Barkster - 07 Jun 2006 22:31 GMT
I'm not posting my whole project, the questions was pretty clear I
thought, Oh well, I'm looking at my process not if it compiles.  I need
to know if I am passing labels to the function correctly or not.  I
don't have any other problems except that, I don't think my other code
is the issue but you never know.

> [post re-ordered]
>
[quoted text clipped - 24 lines]
>
>     - Oliver
Dimitri Maziuk - 08 Jun 2006 03:09 GMT
Barkster sez:
> I'm try to use a function to set some text and set the icons of various
> labels and I thought I could send the labelnames to the function and
[quoted text clipped - 11 lines]
>       fieldNo.setText(name);
> }

Print this.class.getName() in actionPerformed and see if it's
what you think it is.

Dima
Signature

Tlaloc: What was Elrond's second name?
Gruber: Hubbard                           -- <ahbou=3C69EB63.A7C431F4@last.com>

Barkster - 08 Jun 2006 03:27 GMT
Thanks, I'll look into that.

> Barkster sez:
> > I'm try to use a function to set some text and set the icons of various
[quoted text clipped - 20 lines]
> Tlaloc: What was Elrond's second name?
> Gruber: Hubbard                           -- <ahbou=3C69EB63.A7C431F4@last.com>
Barkster - 08 Jun 2006 04:05 GMT
Found the issue, wasn't in my code, I'm using jbuilder and the gui
shows the correct arrangment of buttons but at runtime they are getting
moved around.

> Thanks, I'll look into that.
>
[quoted text clipped - 22 lines]
> > Tlaloc: What was Elrond's second name?
> > Gruber: Hubbard                           -- <ahbou=3C69EB63.A7C431F4@last.com>
Barkster - 08 Jun 2006 04:15 GMT
Well wasn't moving but when I modified something in the gui it changed
all the event handlers to:         jButton3.addActionListener(new
SwingLabel_jButton6_actionAdapter(this));

> Found the issue, wasn't in my code, I'm using jbuilder and the gui
> shows the correct arrangment of buttons but at runtime they are getting
[quoted text clipped - 26 lines]
> > > Tlaloc: What was Elrond's second name?
> > > Gruber: Hubbard                           -- <ahbou=3C69EB63.A7C431F4@last.com>
Oliver Wong - 08 Jun 2006 15:01 GMT
> Found the issue, wasn't in my code, I'm using jbuilder and the gui
> shows the correct arrangment of buttons but at runtime they are getting
> moved around.

   Right. The problem wasn't in the code you posted. That's why you should
have posted an SSCCE.

   - Oliver
Barkster - 08 Jun 2006 15:20 GMT
:-)

> > Found the issue, wasn't in my code, I'm using jbuilder and the gui
> > shows the correct arrangment of buttons but at runtime they are getting
[quoted text clipped - 4 lines]
>
>     - Oliver


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.