java - JTextPane wrap and unwrap long words with out space using JButton -
i trying implement wordwrap/unwrap using button jtextpane
. had tried that, not working correctly. problem here is:
- i typed text spaces working,
- without spaces it's not working.
here code:
public class testvisual extends javax.swing.jframe { private boolean wrapped; private jbutton togglebutton = null; private jtextpane jtp = null; private jpanel nowrappanel = null; private jscrollpane scrollpane = null; public testvisual() { super(); init(); } public void init() { this.setsize(300, 200); this.setlayout(new borderlayout()); wrapped = false; jtp = new jtextpane(); nowrappanel = new jpanel( new borderlayout() ); nowrappanel.add( jtp ); scrollpane = new jscrollpane( nowrappanel); togglebutton = new jbutton("wrap"); togglebutton.addactionlistener(new java.awt.event.actionlistener() { public void actionperformed(java.awt.event.actionevent e) { if (wrapped == true){ scrollpane.setviewportview(nowrappanel); nowrappanel.add(jtp); wrapped = false; }else { scrollpane.setviewportview(jtp); togglebutton.settext("unwrap"); wrapped = true; } } }); this.add(scrollpane, borderlayout.center); this.add(togglebutton, borderlayout.north); } // <editor-fold defaultstate="collapsed" desc="generated code"> private void initcomponents() { setdefaultcloseoperation(javax.swing.windowconstants.exit_on_close); javax.swing.grouplayout layout = new javax.swing.grouplayout(getcontentpane()); getcontentpane().setlayout(layout); layout.sethorizontalgroup( layout.createparallelgroup(javax.swing.grouplayout.alignment.leading) .addgap(0, 400, short.max_value) ); layout.setverticalgroup( layout.createparallelgroup(javax.swing.grouplayout.alignment.leading) .addgap(0, 300, short.max_value) ); pack(); }// </editor-fold> public static void main(string args[]) { java.awt.eventqueue.invokelater(new runnable() { public void run() { new testvisual().setvisible(true); } }); } // variables declaration - not modify // end of variables declaration }
see link above. in short view's minimum span should equal preferred span no wrap used.
to use space based wrap should define break weights properly.
update http://java-sl.com/tip_letter_wrap_java7.html link shows difference in wrapping java 7 , how fixed.
Comments
Post a Comment