JTextPane +style

I have a proble with strange line Wrap

whene i am in new line TextPane insert new line whene last style end

Eche style is in next line

How to fix this or off line wrap

Should be

adsasdasdsadasdasdasd

asdasdasd

It is

adsasdasdsad

asdasdasdasdasdasd

[344 byte] By [argola] at [2007-11-27 6:22:24]
# 1

If you need further help then you need to create a [url http://homepage1.nifty.com/algafield/sscce.html]Short, Self Contained, Compilable and Executable, Example Program[/url] (SSCCE) that demonstrates the incorrect behaviour, because I can't guess exactly what you are doing based on the information provided.

Don't forget to use the [url http://forum.java.sun.com/help.jspa?sec=formatting]Code Formatting Tags[/url] so the posted code retains its original formatting.

camickra at 2007-7-12 17:39:20 > top of Java-index,Desktop,Core GUI APIs...
# 2

import java.awt.*;

import java.awt.event.*;

import java.lang.reflect.Array;

import java.util.ArrayList;

import javax.swing.*;

import javax.swing.event.CaretEvent;

import javax.swing.event.CaretListener;

import javax.swing.text.*;

import javax.swing.text.rtf.RTFEditorKit;

public class przyklad222 extends JFrame implements ActionListener,FocusListener,ItemListener,CaretListener{

String newline ="\n",all,podziel;

StyledDocument doc,doc2,doc3;

JTextPane textPane,textPane2;

JToggleButton g,g2;

int start,end;

RTFEditorKit kit,kit2;

MutableAttributeSet in,set;

String style [] = {"regular","bold","bold_off","italic","italic_off","icon"};

int ktoryStyl = 0,ktoryStylSpr=0,startDziel=0,START=0;

ArrayList zmianaStyli = new ArrayList();

ArrayList podzielonyString = new ArrayList();

public przyklad222(){

this.setLayout(null);

kit = new RTFEditorKit();

kit2 = new RTFEditorKit();

textPane = new JTextPane(){

public void setSize(Dimension d)

{

if (d.width < getParent().getSize().width)

d.width = getParent().getSize().width;

super.setSize(d);

}

public boolean getScrollableTracksViewportWidth()

{

return false;

}

};

textPane.setEditorKit(kit);

doc = textPane.getStyledDocument();

addStylesToDocument(doc);

textPane.addFocusListener(this);

textPane.addCaretListener(this);

JScrollPane paneScrollPane = new JScrollPane(textPane);

paneScrollPane.setVerticalScrollBarPolicy(

JScrollPane.VERTICAL_SCROLLBAR_NEVER);

paneScrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);

paneScrollPane.setSize(300,40);

paneScrollPane.setLocation(10,410);

this.add(paneScrollPane);

textPane2 = new JTextPane();

doc2 = textPane2.getStyledDocument();

addStylesToDocument(doc2);

// textPane2.setEditorKit(kit2);

JScrollPane paneScrollPane2 = new JScrollPane(textPane2);

paneScrollPane2.setVerticalScrollBarPolicy(

JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);

paneScrollPane2.setSize(300,350);

paneScrollPane2.setLocation(10,10);

this.add(paneScrollPane2);

JButton b = new JButton("image");

b.setSize(70,20);

b.setLocation(150,370);

b.addActionListener(this);

this.add(b);

JButton b2 = new JButton("send");

b2.setSize(70,20);

b2.setLocation(320,410);

b2.addActionListener(this);

this.add(b2);

g = new JToggleButton("B");

g.setSize(60,30);

g.setLocation(10,370);

g.setActionCommand("bold");

g.addItemListener(this);

this.add(g);

g2 = new JToggleButton("I");

g2.setSize(60,30);

g2.setLocation(80,370);

g2.setActionCommand("italic");

g2.addItemListener(this);

this.add(g2);

}

protected void addStylesToDocument(StyledDocument doc) {

//Initialize some styles.

Style def = StyleContext.getDefaultStyleContext().

getStyle(StyleContext.DEFAULT_STYLE);

Style regular = doc.addStyle("regular", def);

StyleConstants.setFontFamily(def, "SansSerif");

Style s = doc.addStyle("italic", regular);

StyleConstants.setItalic(s, true);

s = doc.addStyle("italic_off", regular);

StyleConstants.setItalic(s, false);

s = doc.addStyle("bold", regular);

StyleConstants.setBold(s, true);

s = doc.addStyle("bold_off", regular);

StyleConstants.setBold(s, false);

s = doc.addStyle("icon", null);

StyleConstants.setAlignment(s, StyleConstants.ALIGN_CENTER);

ImageIcon pigIcon = createImageIcon("cross.GIF",

"a cute pig");

if (pigIcon != null) {

StyleConstants.setIcon(s, pigIcon);

}

}

protected static ImageIcon createImageIcon(String path,

String description) {

java.net.URL imgURL = TextSamplerDemo.class.getResource(path);

if (imgURL != null) {

return new ImageIcon(imgURL, description);

} else {

System.err.println("Couldn't find file: " + path);

return null;

}

}

public void set(AttributeSet s){

int st = textPane.getSelectionStart( );

int fn = textPane.getSelectionEnd( );

if ( !textPane.hasFocus( ) )

{

st = start;

fn = end;

}

if ( st != fn )

{

doc.setCharacterAttributes( st, fn - st, s, false );

}

else

{

in = kit.getInputAttributes( );

in.addAttributes( s );

}

textPane.grabFocus( );}

public static void main(String[] args) {

przyklad222 f = new przyklad222();

f.setSize(500,500);

f.setLocation(10,10);

f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

f.setVisible(true);

}

public void actionPerformed(ActionEvent e) {

if(e.getActionCommand().equals("image")){

ktoryStyl = 5;

ImageIcon pigIcon = createImageIcon("cross.GIF",

"a cute pig");

textPane.insertIcon(pigIcon);

set(set);

textPane.grabFocus();

start = textPane.getSelectionStart( );

end = textPane.getSelectionEnd( );

}

if(e.getActionCommand().equals("send")){

podziel = all.substring(startDziel,doc.getLength());

podzielonyString.add(podziel);

String initString [] = new String[podzielonyString.size()];

String initStyles [] = new String[zmianaStyli.size()];

for(int i=0;i<podzielonyString.size();i++){

if(i == podzielonyString.size()-1){

initString [i] = (String)podzielonyString.get(i)+"\n";

}

initString [i] = (String)podzielonyString.get(i);

initStyles[i] = (String)zmianaStyli.get(i);

System.out.println("STYL="+initStyles[i]+" String="+initString[i]);

}

System.out.println(initString.length+"=="+initStyles.length);

zmianaStyli.clear();

podzielonyString.clear();

startDziel =0;

START=0;

try {

doc.remove(0,doc.getLength());

for (int i=0; i < initString.length; i++) {

/*AttributeSet ss = doc2.getStyle(initStyles[i]) ;

in = kit2.getInputAttributes( );

in.addAttributes( ss);*/

doc2.insertString(doc2.getLength(), initString[i],

doc2.getStyle(initStyles[i]));

in.addAttributes(doc2.getStyle(initStyles[i]));

}

} catch (BadLocationException ble) {

System.err.println("Couldn't insert initial text into text pane.");

}

}

}

public void itemStateChanged(ItemEvent e) {

g = (JToggleButton)e.getSource();

g2 = (JToggleButton)e.getSource();

String a = g.getActionCommand();

String a2 = g2.getActionCommand();

if( a.equals("bold") ){

if( e.getStateChange() == 1){

System.out.println("bold_jest_on");

ktoryStyl = 1;

set = doc.getStyle("bold");

set(set);

}

else if( e.getStateChange() == 2){

System.out.println("bold_jest_off");

ktoryStyl = 2;

set = doc.getStyle("bold_off");

set(set);

}

}

if( a2.equals("italic") ){

if( e.getStateChange() == 1){

System.out.println("italc_jest_on");

set = doc.getStyle("italic");

set(set);

ktoryStyl = 3;

}

else if( e.getStateChange() == 2){

System.out.println("ilatci_jest_off");

set = doc.getStyle("italic_off");

set(set);

ktoryStyl = 4;

}

}

}

public void focusGained(FocusEvent arg0) {

if ( start >= 0 && end >= 0 )

{

if ( textPane.getCaretPosition( ) == start )

{

textPane.setCaretPosition( end );

textPane.moveCaretPosition( start );

}

else

{

textPane.select( start, end );

}

}

}

public void focusLost(FocusEvent arg0) {

start = textPane.getSelectionStart( );

end = textPane.getSelectionEnd( );

}

public void caretUpdate(CaretEvent e) {

try {

all = doc.getText(0,doc.getLength());

} catch (BadLocationException e1) {

// TODO Auto-generated catch block

e1.printStackTrace();

}

if(START == 0){

ktoryStylSpr = ktoryStyl;

zmianaStyli.add(style [ktoryStyl]);

START++;

}else{

if(ktoryStylSpr == ktoryStyl){

//System.out.println("row");

}

else{

//System.out.println();

ktoryStylSpr = ktoryStyl;

zmianaStyli.add(style [ktoryStyl]);

podziel = all.substring(startDziel,e.getDot());

startDziel = e.getDot();

podzielonyString.add(podziel);

}

}

}

}

argola at 2007-7-12 17:39:20 > top of Java-index,Desktop,Core GUI APIs...
# 3
how to have previous style after i insert icon becouse whene i inserticon it change style to normal
argola at 2007-7-12 17:39:20 > top of Java-index,Desktop,Core GUI APIs...