JComboBox and actionPerformed Problems
Hello all
I am making a GUI that on top has JComboBox. This combo box controls all the TextFields and JLabels (pictures) inside the GUI. They are all dependent on what is selected in the JComboBox.
My actionPerformed () Method is following
publicvoid actionPerformed(ActionEvent e){
String petName;
petName = get_yahoo_TFs(companies.getSelectedItem());
if (e.getSource() == companies)
updateLabel(petName);
System.out.println("petName"+petName);
//get_compet (petName);
}
The get_yahoo_TFs method is like the following.
public String get_yahoo_TFs (Object tf)
{
String name_arg="";
name_arg = tf.toString();
QueryString query =new QueryString ("type","");
query.add("s", name_arg);
query.add("r","");
query.add("","Search");
finance =new EnvokeURL (u+query);
System.out.println(finance.getURL());
finance.makeStream();
String name_spec="";
name_spec = finance.getName(index_name);
System.out.println("Last Trade:\t"+finance.finder(last_trade, 32, end));
System.out.println("Came here");
yahooTFs[0].setText(finance.finder(last_trade, 32, end));
yahooTFs[1].setText(finance.finder(market_cap, 37, end));
yahooTFs[2].setText(finance.finder(trade_time, 44, end));
yahooTFs[3].setText(finance.finder(pe, 47, end));
yahooTFs[4].setText(finance.finder(change, 26, end));
yahooTFs[5].setText(finance.finder(eps, 58, end));
for (int i=0; i < yahooTFs.length; i++){
yahooTFs[i].setEditable(false);
}
return name_spec;
}
UpdateLabel() is following
protectedvoid updateLabel(String name){
// Toolkit t = java.awt.Toolkit.getDefaultToolkit();
ImageIcon icon =null;
try
{
java.net.URL u =new java.net.URL ("http://chart.finance.yahoo.com/c/1y/g/"+name);
icon =new ImageIcon (u);
System.out.println(name);
}
catch (MalformedURLException e){System.out.println(e);}
chart.setIcon(icon);
chart.setToolTipText("A drawing of a " + name.toLowerCase());
if (icon !=null){
chart.setText(null);
}else{
chart.setText("Image not found");
}
}
Now Getting to the problem:
The problem is that When the GUI starts....the JLabel (Picture) is there to begin with. But when i click something else in the JComboBox the picture doesnt show anymore. Furthermore, I found out that if I Take off the get_yahoo_TFs() method from the actionPerformed() method then everything works fine. But the get_yahoo_TFs () method is essential since it controls all the TextFields. Please suggest a way around this weird problem
Please ..any sugggestions? I am stuck on this for a while nowI know the control is given back to the program after get_yahoo_TFs() returns a String. Because I print out that String. Then y wont the Image display?
[url http://java.sun.com/docs/books/tutorial/uiswing/components/combobox.html]How to Use Comobo Boxes[/url] has a working example.
Have you checked that the URL that's created is correct? What happens if you print the URL out and copy it into a browser - do you get an image loaded?
Have you confirmed that the get_yahoo_tfs method isn't throwing an exception? If removing it fixes your problem then perhaps you need to be spending some time debugging that? Wrap it up in a try/catch and have a look at any stack trace that you get from it!
> Have you confirmed that the get_yahoo_tfs method
> isn't throwing an exception? If removing it fixes
> your problem then perhaps you need to be spending
> some time debugging that? Wrap it up in a try/catch
> and have a look at any stack trace that you get from
> it!
Yes i have confirmed it, because get_yahoo_tfs() method works fine and fills in all the TextFields. I dont see any exceptions thrown.
And yes the URL is working fine. I copied and pasted it in the browser and i do see a chart. Like i said before....if i comment out the get_yahoo_tfs() method from actionPerformed(), then the Image is displayed just fine!. IF i call updateLabel, before get_yahoo_tfs() then it also works fine....but the picture is dependent on the String that get_yahoo_tfs() returns.
This is so weird that I dont even have a clue any more.
If u notice...in my updateLabel() method i have the following lines
chart.setToolTipText("A drawing of a " + name.toLowerCase());
if (icon != null) {
chart.setText(null);
} else {
chart.setText("Image not found");
}
So now when the gui runs...i hoover my mouse over the panel which is suppose to have the image and it says "A drawing of a goog" (goog) is the item selected from the ComboBox. If posting my whole code...would help...im willing to do that too, since it is kind of long...ill post it on some site so any one can look at it.
> If posting my whole code...would help...im willing to do that too
No we don't want to see your entire program.
Post a demo that demonstrates the incorrect behaviour. Chances are that while you are creating this simplified demo program you will find your mistake. If not, then you have some code that you can post and we can see the exact behaviour. So all you program needs is a combo box with an ActionListern added and a JLabel so you can update the icon. Sounds like about 20 lines of code.
ok here is a demo...not 20 lines but still
import java.awt.*;
import java.awt.event.*;
import java.net.MalformedURLException;
import javax.swing.*;
public class ComboBoxDemo extends JFrame
implements ActionListener {
JLabel picture;
JTextField tf;
JComboBox petList;
public ComboBoxDemo() {
String[] petStrings = { "goog", "yhoo", "msft", "aapl", "amd" };
Container c = this.getContentPane();
c.setLayout(new BorderLayout());
petList = new JComboBox(petStrings);
petList.setSelectedIndex(4);
c.add(petList, BorderLayout.NORTH);
petList.addActionListener(this);
picture = new JLabel();
picture.setFont(picture.getFont().deriveFont(Font.ITALIC));
picture.setHorizontalAlignment(JLabel.CENTER);
updateLabel(petStrings[petList.getSelectedIndex()]);
picture.setBorder(BorderFactory.createEmptyBorder(10,0,0,0));
JScrollPane sc = new JScrollPane(picture);
c.add(sc, BorderLayout.CENTER);
tf = new JTextField();
picture.setPreferredSize(new Dimension(277, 222));
//add(petList, BorderLayout.PAGE_START);
//add(sc, BorderLayout.CENTER);
// add(sc, BorderLayout.NORTH);
// add(tf, BorderLayout.SOUTH);
// setBorder(BorderFactory.createEmptyBorder(20,20,20,20));
tf.addActionListener(this);
c.add(tf, BorderLayout.SOUTH);
this.setVisible(true);
this.setSize(400,400);
System.out.println("Came");
}
/** Listens to the combo box. */
public void actionPerformed(ActionEvent e) {
String petName;
petName = get_yahoo_TFs(petList.getSelectedItem());
tf.setText(petName);
updateLabel(petName);
}
public String get_yahoo_TFs (Object tf)
{
String name_arg="";
name_arg = tf.toString();
QueryString query = new QueryString ("type","");
query.add("s", name_arg);
query.add("r","");
query.add("","Search");
EnvokeURL finance;
String u = "http://finance.yahoo.com/search?";
finance = new EnvokeURL (u+query);
System.out.println(finance.getURL());
String index_name = "http://finance.yahoo.com/q/pr?s=";
finance.makeStream();
String name_spec="";
name_spec = EnvokeURL.getName(index_name);
return name_spec;
}
protected void updateLabel(String name) {
ImageIcon icon = null;
try
{
java.net.URL u = new java.net.URL ("http://chart.finance.yahoo.com/c/1y/g/"+name);
icon = new ImageIcon (u);
System.out.println(name);
}
catch (MalformedURLException e){System.out.println(e);}
picture.setIcon(icon);
picture.setToolTipText("A drawing of a " + name.toLowerCase());
if (icon != null) {
picture.setText(null);
} else {
picture.setText("Image not found");
}
}
public static void main(String[] args) {
ComboBoxDemo d = new ComboBoxDemo();
}
}
It has the same problem. Initially there is a combobox at top then a Image in middle and finally a TF. But when i select something from the combobox...the image disappears. Image disappears but the TF is still there and it does get populated
I dont think issue would be with the EnvokeURL class but just in case..it can be accessed here. http://www.kanundrum.net/bhaarat/EnvokeURL.java
If I understand the problem correctly you are having a problem reading an image from the web when you click on an item in the combo box.
The reason you can't solve you problem is because you can't simply the code to see whats happening.
> I dont think issue would be with the EnvokeURL
Then remove that code from the demo and see what happens. That class is 200 lines of code and I see no reason why it is required to read an image fro the web. It is definetly not required for a simple demo program.
Here is a simple example of a program that reads an image from the web (that I found on this forum) and displays it in a label.
import java.awt.*;
import java.awt.geom.*;
import java.awt.image.*;
import java.io.*;
import java.net.*;
import javax.imageio.*;
import javax.swing.*;
public class LabelImage2 {
public static void main(String[] args) throws IOException {
BufferedImage gosling = ImageIO.read(new URL("http://today.java.net/jag/bio/JagHeadshot-small.jpg"));
BufferedImage duke = ImageIO.read(new URL("http://today.java.net/jag/old/green/duke/T4.gif"));
Graphics2D g = gosling.createGraphics();
double dx = (gosling.getWidth()-duke.getWidth())/2.0;
double dy = gosling.getHeight()-duke.getHeight();
AffineTransform xform = AffineTransform.getTranslateInstance(dx, dy);
g.drawRenderedImage(duke, xform);
g.dispose();
JFrame f = new JFrame("LabelImage");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().add(new JLabel(new ImageIcon(gosling)));
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}
}
All you need to do is add a combo box and an ActionListener to make the code more dynamic.
I added it thinking that it would be a better demo, because my program uses that class.
But you were right
ok so now i removed the EnvokeURL class and changed actionPerformed() method to the following in the demo.
public void actionPerformed(ActionEvent e) {
String petName;
petName = petList.getSelectedItem().toString();
tf.setText(petName);
updateLabel(petName);
}
The method get_yahoo_Tfs() was used because it creates a object of EnvokeURL class and finds out the companies correct name.
Nehow....now the demo works fine. So the problem is in get_yahoo_Tfs()
...I know its no longer a Swing related problem now. But i still dont know whats causing the problem.
Here is the get_yahoo_TFs() function
public String get_yahoo_TFs (Object tf)
{
String name_arg="";
name_arg = tf.toString();
QueryString query = new QueryString ("type","");
query.add("s", name_arg);
query.add("r","");
query.add("","Search");
finance = new EnvokeURL (u+query);
System.out.println(finance.getURL());
finance.makeStream();
String name_spec="goog";
name_spec = finance.getName(index_name);
return name_spec;
Instead of posting the whole big EnvokeURL class...here are the methods from it that are relative to the get_yahoo_tfs() mehod.
makeStream(), getURL(), and getName();
public void makeStream()
{
BufferedReader the_HTML1 = null;
try
{
InputStream in = new BufferedInputStream (u.openStream());
Reader r = new InputStreamReader(in);
the_HTML1 = new BufferedReader(r);
}
catch (IOException e)
{
System.err.println(e);
}
the_HTML = the_HTML1;
String s1;
s = new Vector();
int i = 0;
try{
while ((s1=the_HTML.readLine())!=null)
{
s.addElement(s1);
i++;
}
}
catch (IOException e)
{
System.out.println(e);
}
}
getURL()
public String getURL()
{
return u.toString();
}
getName()
public static String getName (String index_name)
{
String nameS="";
int n;
for (Enumeration i = s.elements(); i.hasMoreElements();)
{
nameS = (String)i.nextElement();
n = nameS.indexOf(index_name);
if ((n != -1) )
{
nameS = nameS.substring((n+=32), n+4);
break;
}
}
return cleanString(nameS);
}
I really appreciate your time. Please if you see anything that might be causing the problem..let me know
