user3531731
user3531731

Reputation: 49

JTextPane wrap and unwrap long words with out space using JButton

I am trying to implement the wordwrap/unwrap by using Button to JTextPane. I had tried that, but it is not working correctly. Problem here is:

Here is my 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 - do not modify                     
    // End of variables declaration                   
}

Upvotes: 2

Views: 367

Answers (1)

StanislavL
StanislavL

Reputation: 57381

http://java-sl.com/wrap.html

See the link above. In short your view's minimum span should be equal preferred span then no wrap is used.

To use space based wrap you should define break weights properly.

UPDATE http://java-sl.com/tip_letter_wrap_java7.html the link shows difference in wrapping for Java 7 and how it could be fixed.

Upvotes: 3

Related Questions