0

So for fun I've been working on developing this simple stock-chart GUI, which grabs charts from YahooFinance and displays them into a tabbed-Jpanel. I've been able to get the tabs to populate with user-defined stocks and all. However, I've developed some buttons that allow one to query different chart aspects (boll bands, moving averages, etc.) and would like the be able to "redraw" the panel with this update chart.

Problem: I'm not sure how to access the individual panels that I create via the method below. I need to be able to select a panel (say panel1 created when i=1 below) and have it update in an ActionListener. I'm really just wondering how Java defines these panels in the loop so I can access them later and redraw the label! Cheers.

 public static void urlstock(String options,final String[] s, final JFrame f,final      
 JTabbedPane tpane) throws IOException{

for(int i=0;i<s.length;i++){

String path = "http://chart.finance.yahoo.com/z?s="+s[i]+options;

    URL url = new URL(path);

    BufferedImage image = ImageIO.read(url);

    JLabel label = new JLabel(new ImageIcon(image));

    JPanel panel=new JPanel();

    tpane.addTab(s[i],null, panel);

    panel.add(label);

}

So I've tried this which cues on a button press, but it doesn't work because it doesn't recognize panel as variable for a reason that is beyond my understanding:

   public void actionPerformed(ActionEvent e)
        { //Execute when button is pressed
    System.out.println("MAButton");
    tpane.getComponentAt(1);
    tpane.remove(panel);
    //Container.remove();

    String path = "http://chart.finance.yahoo.com/z?s=GOOG&t=7m&z=l&q=l&a=ss,sfs";

    URL url = new URL(path);

    BufferedImage image = ImageIO.read(url);

    JLabel label = new JLabel(new ImageIcon(image));
JPanel panel=new JPanel();

tpane.setComponentAt(1,panel);
panel.add(label);
    }
A.G.
  • 153
  • 1
  • 4
  • 12

1 Answers1

1

UPDATED with Example

public class TestTabPane {

    public static void main(String[] args) {
        new TestTabPane();
    }

    public TestTabPane() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                final JTabbedPane tabPane = new JTabbedPane();

                JPanel panel = new JPanel();
                JButton updateButton = new JButton("Update me");
                panel.add(updateButton);
                updateButton.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        int indexOfTab = tabPane.indexOfTab("Testing");
                        JPanel panel = (JPanel) tabPane.getComponentAt(indexOfTab);
                        panel.removeAll();
                        panel.add(new JLabel("I've begin updated"));
                        panel.repaint();
                    }
                });

                tabPane.addTab("Testing", panel);

                JFrame frame = new JFrame();
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(tabPane);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

}
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • Sweet. I'll take a look at it. I tried to do a work around by deleting and then readding tabs, but I think "redrawing" is more elegant. I've been looking through the methods of the tabbedpane class, and I was wondering if there was a way to set the tab that is viewed upon running. Something like `tpane.gototab(int i=0);`. Is this possible? – A.G. Nov 20 '12 at 03:10
  • [`JTabbedPane#setSelectedComponent(Component)`](http://docs.oracle.com/javase/7/docs/api/javax/swing/JTabbedPane.html#setSelectedComponent(java.awt.Component)) or [`JTabbedPane#setSelectedIndex(int)`](http://docs.oracle.com/javase/7/docs/api/javax/swing/JTabbedPane.html#setSelectedIndex(int)) – MadProgrammer Nov 20 '12 at 03:16
  • Hmm..doesn't work. `Container.removeAll()` gives me a non-static in static context error.Here's what I have: `public void actionPerformed(ActionEvent e) { //Execute when button is pressed System.out.println("MAButton"); tpane.getComponentAt(1); tpane.remove(panel); //Container.remove(); }` – A.G. Nov 20 '12 at 05:15
  • Sorry, my bad, that was suppose to be a method reference. `tpane.getComponentAt(1).removeAll()` should work, but if I was you, I be maintaining a reference to the component returned by `JTabbedPane#getComponentAt(int)` as you will want to add a new label to it – MadProgrammer Nov 20 '12 at 05:19
  • Hmm...ok again, I'll take a look at it. For my understanding, what type of Object is `getCompenentAt()` returning? Am I correct in assuming it's returning the JPanel on the Jtab? Just trying to understand beyond the syntax. Anyways, thanks for the prompt response. I'll let you know how it goes. – A.G. Nov 20 '12 at 05:31
  • Alright, so I tried `tpane.getComponentAt(1).removeAll();` and it gave me a "cannot find symbol: method removeAll()". Says it's in the `java.awt.Component` library; and I believe I loaded that with `java.awt.*`. Any thoughts? – A.G. Nov 20 '12 at 05:35
  • Yeah, try casting the `Component` returned from `getComponentAt` to either a `java.awt.Container` or `javax.swing.JPanel` – MadProgrammer Nov 20 '12 at 05:41
  • OK, sorry to be incessant. But I tried `tpane.remove(tpane.getComponentAt(1));` and it removes the tab and panel together. Looking to just remove image (or is this not possible?). – A.G. Nov 20 '12 at 05:41
  • `getCompenentAt()` is returning a `java.awt.Component` – MadProgrammer Nov 20 '12 at 05:42
  • Boom! Worked! Casting operation did the trick! (Losing my mind slowly...) In any case, is the casting operation safe?At least I'm able to remove the picture. Step 2 is adding it back...since the component is a container, should it be simple as x.add(new picture) where x is a container? – A.G. Nov 20 '12 at 05:49
  • You've misunderstood what I'm saying (sorry). You want to remove the contents of the panel that is acting as component for the tab, not the tab itself, check the updated example – MadProgrammer Nov 20 '12 at 05:57
  • Casting is never really safe. You could use an `instanceof` check to see that the component is a `Container` and therefore has `add` and `remove` methods – MadProgrammer Nov 20 '12 at 06:01
  • Thanks. Will do. Was able to update the image and all, but it's going to take me a bit longer to comb through the code to make sure there aren't any bugs I'm overlooking. Thanks though! – A.G. Nov 20 '12 at 06:15