Author Topic: [Java] JTextPane linewrap issues  (Read 3641 times)

0 Members and 1 Guest are viewing this topic.

Offline chuck

  • Full Member
  • ***
  • Posts: 335
  • Canadian Biathlete
    • View Profile
    • Chucks Blog
[Java] JTextPane linewrap issues
« on: September 28, 2006, 05:17:48 pm »
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:
Code: [Select]
    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.

Offline Chavo

  • x86
  • Hero Member
  • *****
  • Posts: 2219
  • no u
    • View Profile
    • Chavoland
Re: [Java] JTextPane linewrap issues
« Reply #1 on: September 28, 2006, 05:22:03 pm »
I doubt it is a problem with the way you are adding text.  More likely is your initialization of the JTextPane contains the problem.

Offline chuck

  • Full Member
  • ***
  • Posts: 335
  • Canadian Biathlete
    • View Profile
    • Chucks Blog
Re: [Java] JTextPane linewrap issues
« Reply #2 on: September 28, 2006, 05:25:26 pm »
I use the netbeans GUI builder... so heres snippits from the code
Code: [Select]
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
Code: [Select]
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:
Code: [Select]
        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)))
        );
« Last Edit: September 28, 2006, 05:28:15 pm by chuck »
Chucks Blog
JavaOp2 Plugins

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