How to set color for text in java.
-
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 -
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 -
Your test in the sub loop probably never yields true. Have you stepped through your code with the debugger?
-
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 ?
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 stringa
. 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.