How to change the value of a JLabel in runtime?

Posted by user365465 on Stack Overflow See other posts from Stack Overflow or by user365465
Published on 2010-06-13T01:04:22Z Indexed on 2010/06/13 1:12 UTC
Read the original article Hit count: 737

Filed under:
|

I want to change the image a JLabel is viewing during runtime, but I keep getting NullPointerExceptions or nothing happens when I press the magic button that's supposed to do things. Is it even possible in Java?

Here is my code in its entirety:

    import java.text.*;
    import javax.swing.text.*;
    import java.util.*;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.event.*;

    public class Shell implements ActionListener, MenuKeyListener
    {
        JFrame frame;
        JWindow window;
        JButton PSubmit;
        JPanel pane1, pane2;
        JRadioButton R1, R2, R3;
            ButtonGroup PGroup;
        JTabbedPane layout;

        String result;
        String border = "Border.png";
        String DF = "Frame.png";
        String list [];
        Driver driver;

        public Shell()
        {
            driver = new Driver();
            list = new String [6];
        }

        public void setFrame()
        {
            frame = new JFrame("Pokemon Program 3 by Systems Ready");
            frame.setSize(600, 600);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setVisible(true);
            frame.getContentPane().setLayout(new BorderLayout());
        }
        public void frameLayout()
        {
            layout = new JTabbedPane();
            JPanel pane1 = new JPanel();
            JPanel pane2 = new JPanel();
            JLabel label = new JLabel("Please choose the restrictions:");
            JLabel imgLabel1 = new JLabel(new ImageIcon(border));
            JLabel notiLabel1 = new JLabel("The Pokemon chosen with these restrictions are:       ");
            JLabel notiLabel2 = new JLabel("'No Restrictions': No restrictions for the kind of Pokemon chosen based on species or items.");
            JLabel notiLabel3 = new JLabel("'Battle Revolution': All Pokemon must have unique items.");
            JLabel notiLabel4 = new JLabel("'Battle Tower': All Pokemon must have unique items, Uber and Event Legendaries banned.");
            JLabel label2 = new JLabel("Please choose possible Pokemon:");
            pane1.add(label);
            pearlButtons();
            pane1.add(R1);
            pane1.add(R2);
            pane1.add(R3);
            pane1.add(PSubmit);
            pane1.add(notiLabel2);
            pane1.add(notiLabel3);
            pane1.add(notiLabel4);
            pane1.add(imgLabel1);
            pane1.add(notiLabel1);
            JLabel pokeLabel1 = new JLabel(new ImageIcon(DF));
            JLabel pokeLabel2 = new JLabel(new ImageIcon(DF));
            JLabel pokeLabel3 = new JLabel(new ImageIcon(DF));
            JLabel pokeLabel4 = new JLabel(new ImageIcon(DF));
            JLabel pokeLabel5 = new JLabel(new ImageIcon(DF));
            JLabel pokeLabel6 = new JLabel(new ImageIcon(DF));
            pane1.add(pokeLabel1);
            pane1.add(pokeLabel2);
            pane1.add(pokeLabel3);
            pane1.add(pokeLabel4);
            pane1.add(pokeLabel5);
            pane1.add(pokeLabel6);
            pane2.add(label2);
            layout.add("Pearl Version", pane1);
            layout.add("SoulSilver Version", pane2);
            frame.add(layout);
        }
        public void pearlButtons()
        {
            PGroup = new ButtonGroup();
            R1 = new JRadioButton("No Restrictions", true);
            R1.setActionCommand("N");
            R1.setVisible(true);
            R2 = new JRadioButton("Battle Revolution");
            R2.setActionCommand("BR");
            R2.setVisible(true);
            R3 = new JRadioButton("Battle Tower");
            R3.setActionCommand("B");
            R3.setVisible(true);
            PGroup.add(R1);
            PGroup.add(R2);
            PGroup.add(R3);
            PSubmit = new JButton("Submit");
            PSubmit.setActionCommand("pstart");
            PSubmit.setVisible(true);
            PSubmit.addActionListener(this);
        }
        public void pearlProcessing()
        {
                    //The "list" array has a bunch of string names that get .png affixed to them (and I named the image files as such when I name them)
            String file1 = list[0] + ".png";
            String file2 = list[1] + ".png";
            String file3 = list[2] + ".png";
            String file4 = list[3] + ".png";
            String file5 = list[4] + ".png";
            String file6 = list[5] + ".png";
    /*-------------------------------------------------------------------------------//
                    This is where the method's supposed to go to change the image...
                    I've tried pokeLabel = new JLabel(new ImageIcon(file1));, but that yields a NullPointerException.
//-----------------------------------------------------------------------------------*/
        }
        public static void main(String[] args)
        {
            Shell test = new Shell();
            test.setFrame();
            test.frameLayout();
            test.frame.setVisible(true); 
        }
        public void actionPerformed(ActionEvent e)
        {
            if ("pstart".equals(e.getActionCommand()))
            {
                result = PGroup.getSelection().getActionCommand();
                if (result.equals("N"))
                {
                    list = driver.Prandom();
                    pearlProcessing();
                }
                else
                    System.out.println("Not done yet! ;)");
            }
        } 

        public void menuKeyPressed(MenuKeyEvent e)  
        {
            System.out.println("pressed");
        }
        public void menuKeyReleased(MenuKeyEvent e)
        {
            System.out.println("menuKeyReleased");
        }
        public void menuKeyTyped(MenuKeyEvent e)
        {
            System.out.println("menuKeyTyped");
        }

    }

© Stack Overflow or respective owner

Related posts about java

Related posts about swing