java - can JLabel be added to JTextArea? -
is there way add jlabel jtextarea? cause try add
didnt work , set visible true, allowed add jlabel inside jtextarea? through append?
here current code didnt work
jta = new jtextarea(); jta.seteditable(false); jta.setlinewrap(true); jta.setwrapstyleword(true); jta.setfont(new font("calibri", font.plain,16)); jlarray = new jlabel("radsjhkaljk sadf"); jta.add(jlarray); jta.setvisible(true); jsptextfield = new jscrollpane(jta);
can append jlabel inside jtextarea each time message added?
jtextarea
not appropriate swing
component insert jlabels
or other components within it. can use jtextpane
purpose. example , consider code given below:
import javax.swing.*; import javax.swing.text.*; import javax.swing.border.*; import java.awt.event.*; import java.awt.*; class jlabeltotextpane extends jframe implements actionlistener { jtextfield tf; jtextpane tp ; jbutton click; styleddocument doc; simpleattributeset attr; public void createandshowgui() { settitle("add jlabel jtextpane"); tf = new jtextfield(10); tp = new jtextpane(); click = new jbutton("click"); doc = tp.getstyleddocument(); attr = new simpleattributeset(); jscrollpane pane = new jscrollpane(tp); jpanel npanel = new jpanel(); npanel.add(tf);npanel.add(click); tf.addactionlistener(this); click.addactionlistener(this); container c = getcontentpane(); c.add(npanel,borderlayout.north); c.add(pane); setdefaultcloseoperation(jframe.exit_on_close); setsize(300,200);setlocationrelativeto(null);setvisible(true); } @override public void actionperformed(actionevent evt) { string text = tf.gettext(); if (text!=null && !"null".equals(text) && !"".equals(text.trim())) { jlabel label = new jlabel(text); label.setopaque(true); label.setbackground(color.gray); label.setborder(borderfactory.createlineborder(color.black,1)); tp.setcaretposition(tp.getdocument().getlength()); tp.insertcomponent(label); label.addmouselistener(new mouseadapter() { public void mouseclicked(mouseevent evt) { string text = ((jlabel)evt.getsource()).gettext(); joptionpane.showmessagedialog(jlabeltotextpane.this,"hi, text "+text,"information",joptionpane.information_message); } }); try { doc.insertstring(doc.getlength(), " ", attr ); } catch (badlocationexception ex) { ex.printstacktrace(); } } } public static void main(string[] args) { swingutilities.invokelater( new runnable() { @override public void run() { jlabeltotextpane lta = new jlabeltotextpane(); lta.createandshowgui(); } }); } }
Comments
Post a Comment