Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Java
  4. How to set color for text in java.

How to set color for text in java.

Scheduled Pinned Locked Moved Java
javatutorial
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    mybm1
    wrote on last edited by
    #1

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {

       filepath ="/home/GUI/Language\_Independent/Output/Frame\_1.txt";
       file =new File(filepath);
       fileContents="";
    
    try
         {
             br = new BufferedReader(new FileReader(filepath));
       
         }
         catch(Exception E)
         {
           jLabel1.setText(E.toString());
           
         }
    
    try
         {
             while((fileContents= br.readLine())!=null)
             {
                 System.out.println("fileContents");
                 jLabel1.setText(fileContents);
                 
                 
                 String str = "a";
          
            while(br.readLine()== str) {
               
             jLabel1.setForeground(Color.red);
            }
             }
             br.close();
         }
         catch(Exception E1)
         {
             jLabel1.setText(E1.toString());
         }    
        
    
        
        
    // TODO add your handling code here:
    }         
    

    Frame_1 consist of generate text eg:abcdefgh..
    for this generated file i want to set color for specific char eg a=red so on.
    jLabel1.setForeground(Color.red);
    is not working at all.Please someone suggest and thnks in advanced

    L 1 Reply Last reply
    0
    • M mybm1

      private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {

         filepath ="/home/GUI/Language\_Independent/Output/Frame\_1.txt";
         file =new File(filepath);
         fileContents="";
      
      try
           {
               br = new BufferedReader(new FileReader(filepath));
         
           }
           catch(Exception E)
           {
             jLabel1.setText(E.toString());
             
           }
      
      try
           {
               while((fileContents= br.readLine())!=null)
               {
                   System.out.println("fileContents");
                   jLabel1.setText(fileContents);
                   
                   
                   String str = "a";
            
              while(br.readLine()== str) {
                 
               jLabel1.setForeground(Color.red);
              }
               }
               br.close();
           }
           catch(Exception E1)
           {
               jLabel1.setText(E1.toString());
           }    
          
      
          
          
      // TODO add your handling code here:
      }         
      

      Frame_1 consist of generate text eg:abcdefgh..
      for this generated file i want to set color for specific char eg a=red so on.
      jLabel1.setForeground(Color.red);
      is not working at all.Please someone suggest and thnks in advanced

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      Your test in the sub loop probably never yields true. Have you stepped through your code with the debugger?

      M 1 Reply Last reply
      0
      • L Lost User

        Your test in the sub loop probably never yields true. Have you stepped through your code with the debugger?

        M Offline
        M Offline
        mybm1
        wrote on last edited by
        #3

        ya i also observed it is not going inside the loop condition is not satisfied but i can solved it out is my logic wrong ?

        L 1 Reply Last reply
        0
        • M mybm1

          ya i also observed it is not going inside the loop condition is not satisfied but i can solved it out is my logic wrong ?

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          Your logic does not make much sense. You read a line from the file, then you are trying to use a while loop to read another line of text and compare it with the string a. You should read a line and then test if it contains the character you are looking for. However, note that you cannot set different characters to different colours. You can only use a single colour for the entire label.

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups