java - text displaying in a JTextArea -


i'm working on window supposed represent chat box of dialogue between user , program. want user messages end on right side of conversation zone , messages generated computer end on left side. conversation zone have jtextarea scrollbar chat frame

you can use jtextpane , set "paragraph" attributes add text text pane control right/left alignment.

here simple example shows how "center" text inserted. concept same right/left alignment.

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();             }         }); */     } } 

Comments

Popular posts from this blog

How to understand 2 main() functions after using uftrace to profile the C++ program? -

c# - Update a combobox from a presenter (MVP) -

How to put a lock and transaction on table using spring 4 or above using jdbcTemplate and annotations like @Transactional? -