A.G.
A.G.

Reputation: 153

redraw jlabel in loop defined jpanels and decleration of jpanels in loop

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

Upvotes: 0

Views: 147

Answers (1)

MadProgrammer
MadProgrammer

Reputation: 347204

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

}

Upvotes: 1

Related Questions