i have jtextarea not user editable. acts console without input. want change background color next append have no idea how. have idea:
- create instance of type font , somehow set background color of font object
- call method jtextarea.setfont(the instance created) right before call next append.
- call jtextarea.append("message background color\n");
i think works have no idea how set backgroundcolor attribute font object. can give me insight please? thanks.
you can't use jtextarea. doesn't support different font colors.
instead need use jtextpane
, can play attibutes. here simple example started:
import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.text.*; public class textpaneattributes extends jpanel { public textpaneattributes() { setlayout( new borderlayout() ); jtextpane textpane = new jtextpane(); textpane.settext( "one\ntwo\nthree\nfour\nfive\nsix\nseven\neight" ); // defaulthighlighter highlighter = (defaulthighlighter)textpane.gethighlighter(); // highlighter.setdrawslayeredhighlights(false); // define character , paragraph attributes simpleattributeset keyword = new simpleattributeset(); styleconstants.setbold(keyword, true); simpleattributeset green = new simpleattributeset(); styleconstants.setforeground(green, color.green); simpleattributeset center = new simpleattributeset(); styleconstants.setalignment(center, styleconstants.align_center); simpleattributeset left = new simpleattributeset(); styleconstants.setalignment(left, styleconstants.align_left); // change attributes on existing text styleddocument doc = textpane.getstyleddocument(); doc.setcharacterattributes(0, 3, keyword, false); doc.setcharacterattributes(8, 5, green, true); doc.setparagraphattributes(20, 1 , center, false); // add text attributes try { doc.insertstring(doc.getlength(), "\nnormal text", null); doc.insertstring(doc.getlength(), "\ngreen text centered", green); doc.setparagraphattributes(doc.getlength(), 1 , center, false); doc.insertstring(doc.getlength(), "\nkeyword text", keyword); doc.setparagraphattributes(doc.getlength(), 1 , left, false); // newly typed text @ end of document inherit // "keyword" attributes unless remove attributes textpane.setcaretposition(doc.getlength()); textpane.getinputattributes().removeattributes(keyword); } catch(exception e) {} // add text pane frame jscrollpane scrollpane = new jscrollpane( textpane ); scrollpane.setpreferredsize( new dimension( 200, 250 ) ); add( scrollpane ); // create button panel jpanel buttons = new jpanel(); add(buttons, borderlayout.page_end); // add bold button jbutton bold = new jbutton( new stylededitorkit.boldaction() ); buttons.add( bold ); // add right alignment button jbutton right = new jbutton( new stylededitorkit.alignmentaction("align right", styleconstants.align_right) ); buttons.add( right ); } private static void createandshowgui() { jframe frame = new jframe("sscce"); frame.setdefaultcloseoperation(jframe.exit_on_close); frame.add(new textpaneattributes()); frame.pack(); frame.setlocationbyplatform( true ); frame.setvisible( true ); } public static void main(string[] args) { eventqueue.invokelater( () -> createandshowgui() ); /* eventqueue.invokelater(new runnable() { public void run() { createandshowgui(); } }); */ } }
i'll let read styleconstants
api other properties can control including background color of text. can set multiple properties each attribute set.
read section swing tutorial on text component features more information , working examples.
Comments
Post a Comment