Hi all,
I have a little problem with my JEditorPane. I want to implement the posibility to put signs on a document loaded in a jeditorpane and save them. One solution is to save the position of the scrollbar, but because the font size can be changed it will not be working. So I want to simulate a cmouse click on the first row of text from viewport to put there the caret and take after that his position. The problem is that the simulation for the mouse click donesn,t work. The event is simulated, but the caret position is not changing.
I tried also using Robot class, but this implementation give me a serie a mouse events and it moves the cursor to the requested position and it's not the behaviour that I want.
Here is my code:
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JButton;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.text.BadLocationException;
public class Reader {
JEditorPane jEditorPane;
JScrollPane editorScrollPane;
private JFrame frame;
private JPanel readerPane;
private JPanel commandsPane;
/**
* @param args
*/
public static void main(String[] args) {
new Reader();
}
public Reader() {
frame = new JFrame();
createReader();
createCommands();
addPanes();
frame.setSize(1000, 850);
// frame.setExtendedState(frame.MAXIMIZED_BOTH);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
loadHtml();
}
private void addPanes() {
GridBagLayout layout = new GridBagLayout();
frame.setLayout(layout);
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.BOTH;
c.gridx = 1;
c.gridy = 0;
c.ipadx = 0;
c.ipady = 0;
c.weightx = 1;
c.weighty = 2;
frame.add(readerPane, c);
c = new GridBagConstraints();
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 1;
c.gridy = 1;
c.ipadx = 0;
c.ipady = 10;
c.weightx = 0;
c.weighty = 0;
c.gridwidth = 1;
c.gridheight = 1;
frame.add(commandsPane, c);
}
private void createCommands() {
commandsPane = new JPanel();
commandsPane.setBackground(Color.white);
JButton backButton = new JButton("PUSH");
commandsPane.add(backButton);
backButton.addMouseListener(new MouseListener() {
@Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent e) {
MouseEvent click = new MouseEvent(jEditorPane,
MouseEvent.MOUSE_CLICKED, System.currentTimeMillis(),
16, 10, 200, 1, false, 1);
MouseListener[] listeners = jEditorPane.getMouseListeners();
for (int i = 0; i < listeners.length; i++) {
listeners.mouseClicked(click);
}
}
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
});
}
private void createReader() {
jEditorPane = new JEditorPane();
jEditorPane.setEditable(false);
jEditorPane.setSelectionColor(Color.green);
jEditorPane.addMouseListener(new MouseListener(){
@Override
public void mouseClicked(MouseEvent e) {
System.out.println("clicked");
System.out.println("caret pos = "
+ jEditorPane.getCaretPosition());
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
});
editorScrollPane = new JScrollPane(jEditorPane);
editorScrollPane.setBorder(null);
editorScrollPane.getVerticalScrollBar().setPreferredSize(
new Dimension(0, 0));
readerPane = new JPanel();
readerPane.setBackground(Color.black);
readerPane.setLayout(new GridBagLayout());
JPanel spacePane = new JPanel();
spacePane.setBackground(Color.white);
GridBagConstraints c = new GridBagConstraints();
c = new GridBagConstraints();
c.fill = GridBagConstraints.NONE;
c.ipadx = 500;
c.ipady = 650;
c.weighty = 1;
c.weightx = 1;
c.gridx = 1;
c.gridy = 1;
c.gridwidth = 1;
c.gridheight = 1;
readerPane.add(spacePane, c);
spacePane.setLayout(new GridBagLayout());
c = new GridBagConstraints();
c.fill = GridBagConstraints.BOTH;
c.weighty = 1;
c.weightx = 1;
c.gridx = 0;
c.gridy = 0;
c.gridwidth = 1;
c.gridheight = 1;
c.insets = new Insets(20, 20, 20, 20);
spacePane.add(editorScrollPane, c);
}
private void loadHtml() {
System.out.println("load html file");
jEditorPane.setContentType("text/html");
jEditorPane.setText("<html><body>" +
"<p>some text here for testing some text here for testing some text here for testing some text here for testing " +
"some text here for testing some text here for testing some text here for testing some text here for testing " +
"some text here for testing some text here for testing some text here for testing some text here for testing " +
"some text here for testing some text here for testing some text here for testing some text here for testing " +
"some text here for testing some text here for testing some text here for testing some text here for testing </p>" +
"</body></html>");
jEditorPane.revalidate();
}
}
If tou run this code you can observe that when the "PUSH" button is pressed the jeditorpane receives a mouseclick event, but the caret position is not changing.
Strange is that when you click with the mouse over the editor pane the caret position is changed and I don't understand what is wrong.
Can you help me please?