Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: passing value and setting it to jlabel text

  1. #1
    Junior Member
    Join Date
    Sep 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default passing value and setting it to jlabel text

    This site is really helping a lot of people! I have a simple program that I can't understand why I am getting the NullPointerException each time I try to pass a value from one form to another and setting it up as a text in a label. If I pass the value and do the pritln, I can actually see the result but if I place it in the jlabel as text, that's the time I get the error. Please Help! Thanks! Here is the code to pass the value to the other form.

    [CODE = java]public class Micolocoi extends JApplet {
    private String lblName;

    public JLabel lblTime, lblRem1, lblRem2, lblRem3, lblRem4, lblRem5;
    lblRem1 = new JLabel("0", SwingConstants.CENTER);
    lblRem2 = new JLabel("0", SwingConstants.CENTER);
    lblRem3 = new JLabel("0", SwingConstants.CENTER);
    lblRem4 = new JLabel("0", SwingConstants.CENTER);
    lblRem5 = new JLabel("0", SwingConstants.CENTER);

    public void setRem1(String input) { lblName = input; System.out.println(input); } //TO GET THE VALUE FROM THE OTHER FORM
    public String getRem1() { return lblName; }
    public void setLblRem1(){ lblRem1.setText(getRem1()); } //TO SET UP THE VALUE AS THE TEXT OF THE LABEL
    }
    public static void main(String args[]){

    Micolocoi micolocoi = new Micolocoi();
    JFrame frame = new JFrame("MICOLOCOI");
    Container container = frame.getContentPane();
    container.add(micolocoi);
    frame.setSize(600, 200);
    frame.setResizable(false);
    frame.setLocationRelativeTo(null);


    micolocoi.init();
    micolocoi.start();
    micolocoi.getDateTime();

    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);

    }
    }


    public class Choice extends JFrame {

    btnPrepaid.addActionListener(new ActionListener() {

    public void actionPerformed(ActionEvent e) {
    selectionAction(e);
    }
    });

    private void selectionAction(ActionEvent evt){
    System.out.println(choice);
    int choice = getChoice();
    boolean bol = true;
    switch (choice) {

    case 1:
    while(bol){
    String str = JOptionPane.showInputDialog(null, "Input minutes ", "ALERT", 1);
    if(str != null && Integer.parseInt(str) != 0){
    JOptionPane.showMessageDialog(null, "Time will run for " +str +" minutes", "ALERT", 1);
    mic.setRem1(str); //THIS IS WHAT I AM TRYING TO PASS TO THE OTHER FORM AND SET IT AS THE TEXT FOR THE LABEL SO I CAN ACTUALLY SUBTRACT 1 EACH MINUTE
    mic.setLblRem1();
    setVisible(false);
    bol = false;
    } else {
    JOptionPane.showMessageDialog(null, "No input given", "ALERT", 1);
    group.clearSelection();
    }
    }
    break;

    case 2:
    while(bol){
    String str = JOptionPane.showInputDialog(null, "Input minutes ", "ALERT", 1);

    if(str != null && Integer.parseInt(str) != 0){
    JOptionPane.showMessageDialog(null, "Time will run for " +str +" minutes", "ALERT", 1);
    System.out.println("dfsadsf");
    setVisible(false);
    bol = false;
    break;
    } else {
    JOptionPane.showMessageDialog(null, "No input given", "ALERT", 1);
    group.clearSelection();
    }
    }
    break;
    }.... [/CODE]


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: passing value and setting it to jlabel text

    Please Edit your post and wrap your code with
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.

    Also posted at Passing value and setting it to jlabel text - Dev Shed
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Loop through JLabel and change JLabel
    By JoeBrown in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 11th, 2012, 12:52 PM
  2. Adding JLabel on other Jlabel
    By mike416 in forum AWT / Java Swing
    Replies: 3
    Last Post: March 29th, 2012, 11:50 AM
  3. JLabel
    By Poseidon in forum AWT / Java Swing
    Replies: 2
    Last Post: February 18th, 2012, 04:39 PM
  4. adding text to JLabel during actionPerformed
    By that_guy in forum AWT / Java Swing
    Replies: 1
    Last Post: February 12th, 2012, 04:08 PM
  5. How can I avoid JTextArea from grabbing focus upon setting text
    By Orit in forum Java Theory & Questions
    Replies: 4
    Last Post: October 4th, 2010, 05:17 AM