The Author Online Book Forums are Moving

The Author Online Book Forums will soon redirect to Manning's liveBook and liveVideo. All book forum content will migrate to liveBook's discussion forum and all video forum content will migrate to liveVideo. Log in to liveBook or liveVideo with your Manning credentials to join the discussion!

Thank you for your engagement in the AoF over the years! We look forward to offering you a more enhanced forum experience.

This topic is READ ONLY
import-bot (20211) [Avatar] Offline
#1
[Originally posted by hly]

Hi,Sir:

I have a question about using EditKit and trying to get text
from JTextPane. I found some problem with interface KeyListener.
When I move "kit.setN(2); textPane.setEditorKit(kit);" out from
keyReleased(), everything is fine. But in my situation, I have to
use KeyListener. I put my simple test code below. Thank you very
much.

liying


import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.text.*;

public class TTss extends JFrame {
private JTextPane textPane = new JTextPane();
private JScrollPane scrollPane;
private CustomView cus;
private CustomEditorKit kit=new CustomEditorKit();

class CustomView extends WrappedPlainView {
int e;
public void setI(int w){
e=w;
}
public int getI(){
return e;
}
public CustomView(Element elem) {
super(elem);
}
public void paint(Graphics g, Shape a) {
super.paint(g,a);
}
};

class CustomEditorKit extends StyledEditorKit implements ViewFactory {
int e;
public void setN(int w){
e=w;
}
public ViewFactory getViewFactory() {
return this;
}
public View create(Element elem) {
cus=new CustomView(elem);
cus.setI(e);
return cus;
}
};

public TTss() {
Container contentPane = getContentPane();
scrollPane=new JScrollPane(textPane);
contentPane.add(new JScrollPane(textPane), BorderLayout.CENTER);

textPane.addKeyListener(new KeyListener(){
public void keyPressed(KeyEvent e){}
public void keyTyped(KeyEvent e){}
public void keyReleased(KeyEvent e){
kit.setN(2);
textPane.setEditorKit(kit);
}
});
}

public static void main(String args[]) {
final JFrame f = new TTss();
f.setTitle("Simple Test");
f.setBounds(100,50,600,400);
f.setVisible(true);
f.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
f.addWindowListener(new WindowAdapter() {
public void windowClosed(WindowEvent e) {
System.exit(0);
}
});
}
}


import-bot (20211) [Avatar] Offline
#2
Re: Why I cannot get text from textPane?
[Originally posted by matt]

I am not sure I know what you are trying to do... can you
please explain your custom view and editorkit, and the
purpose of them? ..also, what are your setN() and setI()
methods for?

I would suggest using more meaningful method and variable
names..
import-bot (20211) [Avatar] Offline
#3
Re: Why I cannot get text from textPane?
[Originally posted by hly]

Hi,Sir:

I am glad to get your reply so soon.I modified the code and attached below.
What I am trying to do is in a text pane I want make some special mark for the
text,just like MS Word's jaggy line. So I try to use a customed editor kit to
provides grahpical representation. But right now my problem is I even cannot
type text in text pane. When the key released, the cursor move back to the
beginning and the text dispeared. I know maybe the reason is I use the
keyReleased method. But I am just wondering if I have to use that, how I can
type text in the text pane.

Thank you very much.
liying



import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.text.*;

public class TTss extends JFrame {
private JTextPane textPane = new JTextPane();
private JScrollPane scrollPane;
private CustomView cus;
private CustomEditorKit kit=new CustomEditorKit();

class CustomView extends WrappedPlainView {
public CustomView(Element elem) {
super(elem);
}
public void paint(Graphics g, Shape a) {
super.paint(g,a);
//here I want paint some special mark for text
g.setColor(Color.red);
g.drawRect(10,10,40,40);
}
};

class CustomEditorKit extends StyledEditorKit implements ViewFactory {
public ViewFactory getViewFactory() {
return this;
}
public View create(Element elem) {
cus=new CustomView(elem);
return cus;
}
};

public TTss() {
Container contentPane = getContentPane();
scrollPane=new JScrollPane(textPane);
contentPane.add(new JScrollPane(textPane), BorderLayout.CENTER);

textPane.addKeyListener(new KeyListener(){
public void keyPressed(KeyEvent e){}
public void keyTyped(KeyEvent e){}
public void keyReleased(KeyEvent e){
textPane.setEditorKit(kit);
}
});
}

public static void main(String args[]) {
final JFrame f = new TTss();
f.setTitle("Simple Test");
f.setBounds(100,50,600,400);
f.setVisible(true);
f.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
f.addWindowListener(new WindowAdapter() {
public void windowClosed(WindowEvent e) {
System.exit(0);
}
});
}
}
import-bot (20211) [Avatar] Offline
#4
Re: Why I cannot get text from textPane?
[Originally posted by matt]

Liying,
I've experimented with your example, and I think I understand
what you are trying to do. I modified your example to render a
blue box around every line of text...

I am guessing that you actually want to do something like
render a box around every letter or word... I haven't spent enough
time to figure out exactly how to do this yet, but this will
definitely be in the next edition of our Swing book (I think
it might be just a matter of overriding the correct method from
one of the extended view classes). To do the blue outlines
in your example, I overrode the paintChild() method defined
in BoxView.

(BTW, there is no need for the KeyListener you were using....)

==================================================

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.text.*;

public class TTss extends JFrame {
private JTextPane textPane = new JTextPane();
private JScrollPane scrollPane;
private CustomView cus;
private CustomEditorKit kit=new CustomEditorKit();

class CustomView extends WrappedPlainView {
private Element element;

public CustomView(Element elem) {
super(elem);
element = elem;
}

// Draw blue rectangle around each child view
public void paintChild(Graphics g, Rectangle alloc, int index) {
super.paintChild(g,alloc,index);
g.setColor(Color.blue);
g.drawRect((int) alloc.getX(),
(int) alloc.getY(),
(int) alloc.getWidth(),
(int) alloc.getHeight());
}
};

class CustomEditorKit extends StyledEditorKit implements ViewFactory {
public ViewFactory getViewFactory() {
return this;
}

public View create(Element elem) {
cus=new CustomView(elem);
return cus;
}
};

public TTss() {
Container contentPane = getContentPane();
scrollPane=new JScrollPane(textPane);
contentPane.add(new JScrollPane(textPane), BorderLayout.CENTER);
textPane.setEditorKit(kit);
}

public static void main(String args[]) {
final JFrame f = new TTss();
f.setTitle("Simple Test");
f.setBounds(100,50,600,400);
f.setVisible(true);
f.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
f.addWindowListener(new WindowAdapter() {
public void windowClosed(WindowEvent e) {
System.exit(0);
}
});
}
}
import-bot (20211) [Avatar] Offline
#5
Thank you very much.It is very helpful.
[Originally posted by hly]