Fuction Question

B

Barkster

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

Oliver Wong

Barkster said:
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
 
B

Barkster

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

Oliver Wong

[post re-ordered]

Barkster said:
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
 
B

Barkster

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 said:
[post re-ordered]

Barkster said:
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
 
D

Dimitri Maziuk

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
 
B

Barkster

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

Barkster

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

Oliver Wong

Barkster said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top