setCaretPosition method doesn't work after using setPage method

[nobr]I'm trying to setCaretPosition in JEditorPane but doesn't work!

This seem's to happen after i use setPage method.

Here is the code :

import java.awt.*;

import java.awt.event.*;

import java.io.BufferedReader;

import java.io.File;

import java.io.FileReader;

import java.io.FileWriter;

import java.io.IOException;

import java.io.PrintWriter;

import java.util.Vector;

import javax.swing.*;

import javax.swing.event.HyperlinkEvent;

import javax.swing.event.HyperlinkListener;

import javax.swing.text.BadLocationException;

import javax.swing.text.Style;

import javax.swing.text.StyleConstants;

import javax.swing.text.StyleContext;

import javax.swing.text.StyledDocument;

import javax.swing.text.html.HTMLEditorKit;

/**

* Summary description for InterfataCuPoze

*

*/

publicclass InterfataCuPozeextends JFrameimplements HyperlinkListener

{

privatestaticint caret=0;

private String newline="\n";

private String textScris="";

// Variables declaration

private JTextArea inputArea;

private JScrollPane scroll;

privatefinal JEditorPane outputArea=new JEditorPane();

private JScrollPane jScrollPane4;

private JPanel contentPane;

private JTextPane textPane;

//--

private JButton buzz;

private JPanel butoane;

//--

private JButton send;

private JPanel panel_send;

private JEditorPane jep;

private String cale="file:///C:\\chat\\test.html";

public InterfataCuPoze()

{

super();

initializeComponent();

setPage(cale);

// here i want to set Caret Position so that the scrool bar to be at the bottom

this.repaint();

this.setVisible(true);

}

publicvoid setPage(String cale_noua)

{

cale=cale_noua;

try

{

outputArea.setPage(cale);

}catch (IOException e1)

{

try{

new File("test.html").createNewFile();

}catch (IOException ex){

ex.printStackTrace();

}

}

outputArea.repaint();

}

privatevoid initializeComponent()

{

inputArea =new JTextArea();

scroll =new JScrollPane();

//outputArea = new JEditorPane();

jScrollPane4 =new JScrollPane();

contentPane = (JPanel)this.getContentPane();

buzz =new JButton();

butoane =new JPanel();

send =new JButton();

panel_send =new JPanel();

scroll.setViewportView(inputArea);

outputArea.setEditable(false);

jScrollPane4.setViewportView(outputArea);

contentPane.setLayout(null);

addComponent(contentPane, scroll, 5,244,353,82);

addComponent(contentPane, jScrollPane4, 5,3,457,207);

addComponent(contentPane, butoane, 5,213,456,29);

addComponent(contentPane, panel_send, 363,243,98,83);

//buzz.setIcon(new ImageIcon("icons\\alert.gif"));

buzz.setText("Buzz");

buzz.addActionListener(new ActionListener(){

publicvoid actionPerformed(ActionEvent e)

{

buzz_actionPerformed(e);

}

});

butoane.setLayout(null);

butoane.setBackground(new Color(102, 102, 102));

addComponent(butoane, buzz, 9,2,42,25);

send.setText("Send");

send.addActionListener(new ActionListener(){

publicvoid actionPerformed(ActionEvent e)

{

send_actionPerformed(e);

}

});

panel_send.setLayout(null);

addComponent(panel_send, send, 7,26,81,37);

this.setTitle("InterfataCuPoze - extends JFrame");

this.setLocation(new Point(5, 0));

this.setSize(new Dimension(478, 360));

}

/** Add Component Without a Layout Manager (Absolute Positioning) */

privatevoid addComponent(Container container,Component c,int x,int y,int width,int height)

{

c.setBounds(x,y,width,height);

container.add(c);

}

privatevoid buzz_actionPerformed(ActionEvent e)

{

System.out.println("\nbuzz_actionPerformed(ActionEvent e) called.");

// TODO: Add any handling code here

}

privatevoid send_actionPerformed(ActionEvent e)

{

textScris="<font color=red><b>kynamar</b></font> : "+inputArea.getText().trim()+"\n"+"<br>";

try{

outputArea.getDocument().insertString(outputArea.getDocument().getLength(),textScris,null);

caret=outputArea.getDocument().getText(0,outputArea.getDocument().getLength()).length();

outputArea.setCaretPosition(caret);

}catch (BadLocationException ex){

ex.printStackTrace();

}

//} catch (BadLocationException ex) {

//ex.printStackTrace();

// }

try{

PrintWriter out=new PrintWriter(new FileWriter("test.html",true));

out.println(textScris);

out.close();

inputArea.setText("");

}catch (Exception ex){

ex.printStackTrace();

}

outputArea.setContentType("html");

setPage(cale);

try{

System.out.println(outputArea.getDocument().getText(0,outputArea.getDocument().getLength()).length());

}catch (BadLocationException ex){

ex.printStackTrace();

}

}

publicstaticvoid main(String[] args)

{

JFrame.setDefaultLookAndFeelDecorated(true);

JDialog.setDefaultLookAndFeelDecorated(true);

try

{

UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");

}

catch (Exception ex)

{

System.out.println("Failed loading L&F: ");

System.out.println(ex);

}

SwingUtilities.invokeLater(new Runnable(){

publicvoid run(){

new InterfataCuPoze();

}

});

}

//= End of Testing =

publicvoid hyperlinkUpdate(HyperlinkEvent e){

}

}

[/nobr]

[11638 byte] By [kynamara] at [2007-11-26 23:47:36]
# 1
Try wrapping the setCaretPosition(...) method in a SwingUtilities.invokeLater(...)> caret=outputArea.getDocument().getText(0,outputArea.getDocument().getLength()).length();Should be:caret = outputArea.getDocument().getLength();
camickra at 2007-7-11 15:22:22 > top of Java-index,Desktop,Core GUI APIs...
# 2
This method doesn't work!Could please give me another solution?
kynamara at 2007-7-11 15:22:22 > top of Java-index,Desktop,Core GUI APIs...
# 3
> This method doesn't work!Yes it does. Post your SSCCE showing how you implemented the suggestion given.
camickra at 2007-7-11 15:22:22 > top of Java-index,Desktop,Core GUI APIs...
# 4
Here's the deal-o yoit seems like you are using the JEditorPane to display a webpage... that's what you're using setPage for, correct?JEditorPane only renders an HTML page IFF you call editorpane.setEditable(false);I'm 99.44% sure that that's your issue
tjacobs01a at 2007-7-11 15:22:22 > top of Java-index,Desktop,Core GUI APIs...
# 5

After this 2 lines :

outputArea.setContentType("html");

setPage(cale);

i add the follow :

SwingUtilities.invokeLater(new Runnable(){

public void run(){

caret = outputArea.getDocument().getLength();

outputArea.setCaretPosition(caret);

}

});

kynamara at 2007-7-11 15:22:22 > top of Java-index,Desktop,Core GUI APIs...