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 3 of 3

Thread: Unbale to setText to a label multiple times in same program

  1. #1
    Junior Member
    Join Date
    Jul 2014
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Unbale to setText to a label multiple times in same program

    Hi ,

    I am developing a java swing application. In which i want to set a different text to a label in same program ( in single JFrame). I dont want to use another JFrame instead i want to use setText method of java and set different text to a label at different intervals of time according to my need.

    I am using java 1.7 sdk and pupy linux as OS.
    Below i am giving u my source code.

    What i am doing is in constructor of class i am setting an image to JFrame and setting text "Welcom...". And when user clicks on this JFrame a method is called which will clear the text "Welcome.." and sets new text to another label "Enter...." and from there another method is called and it clears label "Enter..." and sets a text "Plzz wait..". and yet there are more methods, i havnt provided here.

    But what is happening is it shows welcome text and then directly Plzz wait..
    Its not showing text Enter... because control is finished after last method gets executed.

    My concern is i want to show all the setText (my messages) in a sequence. Without using another JFrame. There is no any user input other than user will click on first welcome page.

    So can someone help me to do so.



    public class BackgroundFrame extends javax.swing.JFrame {

    JLabel setTitle1;
    JLabel setTitle2;
    JLabel setTitle3;
    JLabel setTitle4;

    JLabel midTitle1;
    JLabel midTitle2;
    JLabel midTitle3;

    JLabel lblPleaseWait;
    JLabel lblTurnPage;

    static String scanResponse;
    static String printerStatus;
    String accountNo;

    public BackgroundFrame() throws IOException
    {
    initComponents();

    setTitle1 = new JLabel(); // Welcome To
    setTitle1.setBounds(250,220,950,150);
    setTitle1.setForeground(new Color(6,42,120));
    setTitle1.setFont(new Font("Caladia", Font.BOLD, 60 ));
    setTitle1.setOpaque(false);
    labelBackground.add(setTitle1);

    midTitle1 = new JLabel(); // Please Enter Your Passbook
    midTitle1.setBounds(50,270,950,150);
    midTitle1.setForeground(new Color(6,42,120));
    midTitle1.setFont(new Font("Caladia", Font.BOLD, 60 ));
    midTitle1.setOpaque(false);
    labelBackground.add(midTitle1);

    setTitle2 = new JLabel(); // Passbook Printing
    setTitle2.setBounds(150,320,950,150);
    setTitle2.setForeground(new Color(6,42,120));
    setTitle2.setFont(new Font("Caladia", Font.BOLD, 60 ));
    setTitle2.setOpaque(false);
    labelBackground.add(setTitle2);

    midTitle2 = new JLabel(); // From Last Updated
    midTitle2.setBounds(150,370,950,150);
    midTitle2.setForeground(new Color(6,42,120));
    midTitle2.setFont(new Font("Caladia", Font.BOLD, 60 ));
    midTitle2.setOpaque(false);
    labelBackground.add(midTitle2);

    setTitle3 = new JLabel(); // Kiosk
    setTitle3.setBounds(350,420,950,150);
    setTitle3.setForeground(new Color(6,42,120));
    setTitle3.setFont(new Font("Caladia", Font.BOLD, 60 ));
    setTitle3.setOpaque(false);
    labelBackground.add(setTitle3);

    midTitle3 = new JLabel(); // Page
    midTitle3.setBounds(350,470,950,150);
    midTitle3.setForeground(new Color(6,42,120));
    midTitle3.setFont(new Font("Caladia", Font.BOLD, 60 ));
    midTitle3.setOpaque(false);
    labelBackground.add(midTitle3);

    lblPleaseWait = new JLabel(); // Please Wait
    lblPleaseWait.setBounds(150,520,950,150);
    lblPleaseWait.setForeground(new Color(6,42,120));
    lblPleaseWait.setFont(new Font("Caladia", Font.BOLD, 60 ));
    lblPleaseWait.setOpaque(false);
    labelBackground.add(lblPleaseWait);

    scanResponse = pl.init_scan();
    scanResponse = "success";

    if(scanResponse.equalsIgnoreCase("success"))
    {
    setTitle1.setText("Welcome To");
    setTitle2.setText("Passbook Printing");
    setTitle3.setText("Kiosk");
    }
    else
    {
    setTitle2.setText("Kiosk Is Out Of Service");
    }
    }

    private void labelBackgroundMouseClicked(java.awt.event.MouseEv ent evt)
    {
    getPrinterInfo();
    }

    public void clearSetTitle()
    {
    setTitle1.setText("");
    setTitle2.setText("");
    setTitle3.setText("");
    }

    public void clearMidTitle()
    {
    midTitle1.setText("");
    midTitle2.setText("");
    midTitle3.setText("");
    }

    public void getPrinterInfo()
    {
    printerStatus = pl.get_status();
    printerStatus = "Online";

    if(printerStatus.equalsIgnoreCase("Online"))
    {
    System.out.println("inside getPrinterInfo");

    clearSetTitle();

    midTitle1.setText("Please Enter Your Passbook");
    midTitle2.setText("From Last Updated");
    midTitle3.setText("Page");

    try
    {
    Thread.sleep(2000);
    }
    catch (InterruptedException ex)
    {
    Logger.getLogger(BackgroundFrame.class.getName()). log(Level.SEVERE, null, ex);
    }

    getPassbookInfo();
    }
    else
    {
    setTitle2.setText("Kiosk Is Out Of Service");
    }

    }

    public void getPassbookInfo()
    {
    clearMidTitle();

    accountNo = pl.acquire_image();
    accountNo = "12345";

    lblPleaseWait.setText("Please Wait . . . .");

    // authenticationRequest();

    }

    public static void main(String args[])
    {
    java.awt.EventQueue.invokeLater(new Runnable() {
    public void run()
    {
    try
    {
    new BackgroundFrame().setVisible(true);
    }
    catch (IOException ex)
    {
    Logger.getLogger(BackgroundFrame.class.getName()). log(Level.SEVERE, null, ex);
    }
    }
    });
    }

    }


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,387
    My Mood
    Hungover
    Thanks
    144
    Thanked 628 Times in 538 Posts

    Default Re: Unbale to setText to a label multiple times in same program

    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Jul 2014
    Location
    Canada
    Posts
    25
    My Mood
    Bored
    Thanks
    0
    Thanked 5 Times in 4 Posts

    Default Re: Unbale to setText to a label multiple times in same program

    For future reference, you should put your code in [CODE][\CODE] tags.

    setTitle1 = new JLabel(); // Welcome To
    setTitle1.setBounds(250,220,950,150);
    setTitle1.setForeground(new Color(6,42,120));
    setTitle1.setFont(new Font("Caladia", Font.BOLD, 60 ));
    setTitle1.setOpaque(false);
    labelBackground.add(setTitle1);
    You should put this code into a function in order to reduce code duplication. Create a Collection of JLabels (maybe a Map with String identifiers) and throw them all in there.

    Ideally, you should use a Layout manager rather than defining the bounds of each element in your frame individually.

    This is bad form:
    Logger.getLogger(BackgroundFrame.class.getName()). log(Level.SEVERE, null, ex);

    Make a global Logger object instead:
    public class MyClass {
     
      Logger logger = LoggerFactory.getLogger(MyClass.class.getName());
     
      public static void main(String[] args) {
        logger.log( ); //etc.
      }
     
    }

    As for your question, can you post just the code that has to do with the changes you're referring to? What method is called when the Welcome JFrame is clicked?

    Why is your constructor throwing an IOException? Why do you have an initComponents() method when you're initializing your components in the constructor? Why do you have
    scanResponse = pl.init_scan();
    scanResponse = "success";
    instead of passing "success" back from pl.init_scan(); on success?

    All in all, the code you have provided doesn't really seem to demonstrate the functionality you described in your question. If you could provide more pertinent code and information, it would be easier to help you solve the problem you're having.

Similar Threads

  1. [SOLVED] How to make a program run multiple times.
    By kunimaro15689 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: May 23rd, 2014, 12:35 PM
  2. Getting program to ask user input three times
    By Java_noob333 in forum Object Oriented Programming
    Replies: 5
    Last Post: February 24th, 2013, 08:03 AM
  3. [SOLVED] Don't understand why it loops through multiple times...
    By Discoveringmypath in forum Loops & Control Statements
    Replies: 4
    Last Post: January 26th, 2013, 08:00 PM
  4. Using the same scanner multiple times in code help
    By theostorm in forum What's Wrong With My Code?
    Replies: 3
    Last Post: September 16th, 2012, 11:30 PM
  5. Counting How Many Times Program Was Excecuted
    By Override in forum Loops & Control Statements
    Replies: 2
    Last Post: October 30th, 2010, 05:11 PM

Tags for this Thread