News:

Pretty crazy that we're closer to 2030, than we are 2005. Where did the time go!

Main Menu

[Java] JTextPane linewrap issues

Started by chuck, September 28, 2006, 05:17:48 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

chuck

When i added a JTextPane to my bots GUI, it worked till i got messages with a length larger then the window. All of a sudden, the JTextPane balloons like this:

... When the text on starcraft looks like:


And my code for appending text:

    public void addColorText(String text, Color color) {
        try {
            MutableAttributeSet attr = new SimpleAttributeSet();
            StyledDocument doc = outputArea.getStyledDocument();
           
            StyleConstants.setForeground(attr, color);
            int offset = doc.getLength();
            doc.insertString(offset, text, attr);
           
        } catch (BadLocationException ex) {
            ex.printStackTrace();
        }
       
        Element root = outputArea.getDocument().getDefaultRootElement();
       
        while (root.getElementCount() > 500) {
            Element firstLine = root.getElement(0);
           
            try {
                outputArea.getDocument().remove(0, firstLine.getEndOffset());
            } catch(BadLocationException ble) {
                System.out.println(ble);
            }
        }
       
        outputArea.setCaretPosition(outputArea.getDocument().getLength());
    }


I just assume something wrong with my code, but i know its not the last parts (Commented them out to test it... no luck), so i think it might be in the insertString part.
Chucks Blog
JavaOp2 Plugins

Quote
Error, keyboard not connected. Press F1 to continue.

Chavo

I doubt it is a problem with the way you are adding text.  More likely is your initialization of the JTextPane contains the problem.

chuck

#2
I use the netbeans GUI builder... so heres snippits from the code

private javax.swing.JTextPane outputArea;
*snip*
outputArea = new javax.swing.JTextPane();
*snip*
outputArea.setBackground(new java.awt.Color(0, 0, 0));
        outputArea.setEditable(false);
        outputArea.setFont(new java.awt.Font("Lucida Grande", 0, 11));
        outputArea.setForeground(new java.awt.Color(255, 255, 0));
        outputArea.setCaretColor(new java.awt.Color(0, 255, 0));
        jScrollPane2.setViewportView(outputArea);


EDIT:
jScrollPane2's code

private javax.swing.JScrollPane jScrollPane2;
*snip*
jScrollPane2 = new javax.swing.JScrollPane();
*snip*
jScrollPane2.setBackground(new java.awt.Color(0, 0, 0));
        jScrollPane2.setForeground(new java.awt.Color(255, 255, 0));
        jScrollPane2.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
        jScrollPane2.setHorizontalScrollBar(null);
*snip*


And the way their added:

        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
                    .add(jScrollPane2)
                    .add(input, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 430, Short.MAX_VALUE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(jTabbedPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 215, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(sendBttn, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 215, Short.MAX_VALUE)))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jScrollPane2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 355, Short.MAX_VALUE)
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, jTabbedPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 355, Short.MAX_VALUE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(input, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 22, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(sendBttn, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 21, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
        );
Chucks Blog
JavaOp2 Plugins

Quote
Error, keyboard not connected. Press F1 to continue.