The Artima Developer Community
Sponsored Link

Legacy Java Answers Forum
November 2001

Advertisement

Advertisement

This page contains an archived post to the Java Answers Forum made prior to February 25, 2002. If you wish to participate in discussions, please visit the new Artima Forums.

Message:

Scrolling

Posted by Jay on November 19, 2001 at 8:03 AM

Terry,

The immediate solution that I could think of is the one posted below. But frankly, I dont like it (the highlighted portion). Theres got to be a better way than this. I worked quite a bit trying to better it but just cant seem to think out of the box. Can you or anyone else help me here improve this? Also, how do you fire actions (the nested Action classes) in EditorKit?


import javax.swing.*;
import javax.swing.text.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.event.*;
import java.net.URL;
import java.io.*;

public class JavaEditor extends JFrame
{
JEditorPane editorPane;
JLabel status;


public JavaEditor(String fileName)
{
super( "JavaEditor" );
JPanel contentPane = new JPanel();
editorPane = createEditorPane(fileName);
status = new JLabel( "You are at: 1, 1" );

JScrollPane editorScrollPane = new JScrollPane(editorPane);
editorScrollPane.setVerticalScrollBarPolicy( JScrollPane.VERTICAL_SCROLLBAR_ALWAYS );
editorScrollPane.setPreferredSize( new Dimension(250, 145) );
editorScrollPane.setMinimumSize( new Dimension(10, 10) );

contentPane.setLayout( new BorderLayout() );
contentPane.add( editorScrollPane, BorderLayout.CENTER );
contentPane.add( status, BorderLayout.SOUTH );
setContentPane( contentPane );

addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});

setSize(200, 200);
setVisible(true);
}

private JEditorPane createEditorPane(String fileName)
{
final JEditorPane editorPane = new JEditorPane();
editorPane.setEditorKitForContentType( "text/plain", new DefaultEditorKit() );
editorPane.setEditable(true);

try
{
editorPane.read(new FileInputStream(fileName), "Text File");
}
catch (Exception e)
{
System.err.println(e);
}

editorPane.addCaretListener(new CaretListener()
{
public void caretUpdate(CaretEvent e)
{
int lineNumber = 1;
int columnNumber = 0;
int currentPosition = editorPane.getCaretPosition();
try
{
String strText = editorPane.getDocument().getText(0, currentPosition);
char arrText[] = strText.toCharArray();
for(int i=0; i {
if(arrText[i] == '\n') lineNumber++;

}
columnNumber = currentPosition - strText.lastIndexOf('\n');
}
catch(BadLocationException ble)
{
System.err.println(ble);
}
status.setText( "You are at: " + lineNumber + ", " + columnNumber );
}
});

return editorPane;
}

public static void main(String[] args)
{
if(args.length != 1)
{
System.out.println("Usage: java JavaEditor fileName");
System.exit(1);
}
new JavaEditor(args[0]);
}
}

-Jay



Replies:

Sponsored Links



Google
  Web Artima.com   
Copyright © 1996-2009 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us