Fuction Question

Discussion in 'Java' started by Barkster, Jun 7, 2006.

  1. Barkster

    Barkster Guest

    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);
    }
     
    Barkster, Jun 7, 2006
    #1
    1. Advertising

  2. Barkster

    Oliver Wong Guest

    "Barkster" <> wrote in message
    news:...
    > 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);
    > }
    >


    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
     
    Oliver Wong, Jun 7, 2006
    #2
    1. Advertising

  3. Barkster

    Barkster Guest

    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);
    }
    }
    }
    Oliver Wong wrote:
    > "Barkster" <> wrote in message
    > news:...
    > > 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);
    > > }
    > >

    >
    > 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, Jun 7, 2006
    #3
  4. Barkster

    Oliver Wong Guest

    [post re-ordered]

    "Barkster" <> wrote in message
    news:...
    > Oliver Wong wrote:
    >> 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
    >>

    >
    > 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.
    >

    [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
     
    Oliver Wong, Jun 7, 2006
    #4
  5. Barkster

    Barkster Guest

    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.

    Oliver Wong wrote:
    > [post re-ordered]
    >
    > "Barkster" <> wrote in message
    > news:...
    > > Oliver Wong wrote:
    > >> 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
    > >>

    > >
    > > 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.
    > >

    > [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, Jun 7, 2006
    #5
  6. 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
    > 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);
    > }


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

    Dima
    --
    Tlaloc: What was Elrond's second name?
    Gruber: Hubbard -- <ahbou=>
     
    Dimitri Maziuk, Jun 8, 2006
    #6
  7. Barkster

    Barkster Guest

    Thanks, I'll look into that.

    Dimitri Maziuk wrote:
    > 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
    > > 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);
    > > }

    >
    > Print this.class.getName() in actionPerformed and see if it's
    > what you think it is.
    >
    > Dima
    > --
    > Tlaloc: What was Elrond's second name?
    > Gruber: Hubbard -- <ahbou=>
     
    Barkster, Jun 8, 2006
    #7
  8. Barkster

    Barkster Guest

    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.

    Barkster wrote:
    > Thanks, I'll look into that.
    >
    > Dimitri Maziuk wrote:
    > > 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
    > > > 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);
    > > > }

    > >
    > > Print this.class.getName() in actionPerformed and see if it's
    > > what you think it is.
    > >
    > > Dima
    > > --
    > > Tlaloc: What was Elrond's second name?
    > > Gruber: Hubbard -- <ahbou=>
     
    Barkster, Jun 8, 2006
    #8
  9. Barkster

    Barkster Guest

    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));


    Barkster wrote:
    > 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.
    >
    > Barkster wrote:
    > > Thanks, I'll look into that.
    > >
    > > Dimitri Maziuk wrote:
    > > > 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
    > > > > 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);
    > > > > }
    > > >
    > > > Print this.class.getName() in actionPerformed and see if it's
    > > > what you think it is.
    > > >
    > > > Dima
    > > > --
    > > > Tlaloc: What was Elrond's second name?
    > > > Gruber: Hubbard -- <ahbou=>
     
    Barkster, Jun 8, 2006
    #9
  10. Barkster

    Oliver Wong Guest

    "Barkster" <> wrote in message
    news:...
    > 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
     
    Oliver Wong, Jun 8, 2006
    #10
  11. Barkster

    Barkster Guest

    :)

    Oliver Wong wrote:
    > "Barkster" <> wrote in message
    > news:...
    > > 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, Jun 8, 2006
    #11
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. mike
    Replies:
    3
    Views:
    373
    Chris Botha
    Nov 16, 2005
  2. somez72
    Replies:
    4
    Views:
    4,367
    Roedy Green
    May 3, 2004
  3. MiniDisc_2k2
    Replies:
    2
    Views:
    393
    MiniDisc_2k2
    Jul 3, 2003
  4. orion30
    Replies:
    2
    Views:
    316
    Alf P. Steinbach
    Jul 14, 2003
  5. PCHOME
    Replies:
    5
    Views:
    450
    Darius
    Apr 13, 2005
Loading...

Share This Page