突出显示JTextPane中的第一行

时间:2013-01-09 07:46:21

标签: java swing file jtextpane

我的文本文件包含以下内容:

public class MyC{
public void MyMethod()
{
    System.out.println("My method has been accessed");
    System.out.println("hi");
}
}

我想在public class MyC{中加载文本文件时为第一行JTextPane加下划线。有人可以指导我吗?我试着看http://docs.oracle.com/javase/7/docs/api/javax/swing/text/Highlighter.html,但它正在使用职位。我无法弄清楚如何去做。

这是我到目前为止所做的:

  public class ReadDemo {

    // read the file into the pane

    static void readin(String fn, JTextComponent pane) {
        try {
            FileReader fr = new FileReader(fn);
            pane.read(fr, null);
            fr.close();
        }
        catch (IOException e) {
            System.err.println(e);
        }
    }

    public static void main(String args[]) {
        final JFrame frame = new JFrame("Testing");
        frame.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });

        // set up the text pane, either a JTextArea or JTextPane

        final JTextComponent textpane = new JTextArea();

        //final JTextComponent textpane = new JTextPane();

        // set up a scroll pane for the text pane

        final JScrollPane pane = new JScrollPane(textpane);
        pane.setPreferredSize(new Dimension(600, 600));


        // set up the file chooser

        String cwd = System.getProperty("user.dir");
        final JFileChooser jfc = new JFileChooser(cwd);
        final JLabel elapsed = new JLabel("Elapsed time: ");

        JButton filebutton = new JButton("Choose File");
        filebutton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if (jfc.showOpenDialog(frame) !=
                    JFileChooser.APPROVE_OPTION)
                        return;
                File f = jfc.getSelectedFile();

                // record the current time and read the file

                final long s_time = System.currentTimeMillis();
                frame.setCursor(Cursor.getPredefinedCursor(
                    Cursor.WAIT_CURSOR));
                readin(f.toString(), textpane);

                // wait for read to complete and update time

                SwingUtilities.invokeLater(new Runnable() {
                    public void run() {
                        frame.setCursor(Cursor.
                            getPredefinedCursor(
                            Cursor.DEFAULT_CURSOR));
                        long t = System.currentTimeMillis() -
                               s_time;
                        elapsed.setText("Elapsed time: " + t);
                    }
                });
            }
        });

        JPanel buttonpanel = new JPanel();
        buttonpanel.add(filebutton);
        buttonpanel.add(elapsed);

        JPanel panel = new JPanel();
        panel.setLayout(new BorderLayout());
        panel.add("North", buttonpanel);
        panel.add("East", pane);


        frame.getContentPane().add(panel);
        frame.pack();
        frame.setVisible(true);
    }
}

2 个答案:

答案 0 :(得分:3)

你可以这样试试:

 static void readin(String fn, JTextComponent pane) {
    try {
        FileReader fr = new FileReader(fn);
        pane.read(fr, null);
        fr.close();
        Highlighter hilit = new DefaultHighlighter();
        Highlighter.HighlightPainter painter = new DefaultHighlighter.DefaultHighlightPainter(Color.yellow);
        pane.setHighlighter(hilit);
        hilit.addHighlight(0, pane.getText().indexOf("\n"), painter);
    }
    catch (IOException e) {
        System.err.println(e);
    } catch (BadLocationException e) {
        e.printStackTrace();
    }
}

重点是你可以获得第一行结束的索引并在添加高亮显示时使用它。

对于任何行/ s,您可以尝试:

 static void readin(String fn, JTextComponent pane) {
    try {
        FileReader fr = new FileReader(fn);
        pane.read(fr, null);
        fr.close();
        Highlighter hilit = new DefaultHighlighter();
        Highlighter.HighlightPainter painter = new DefaultHighlighter.DefaultHighlightPainter(Color.yellow);
        pane.setHighlighter(hilit);
        hilit.addHighlight(getLineEndIdx(pane.getText(), 4), getLineEndIdx(pane.getText(), 8 ), painter);
    }
    catch (IOException e) {
        System.err.println(e);
    } catch (BadLocationException e) {
        e.printStackTrace();
    }
}

private static int getLineEndIdx(String text, int lineNo) {
    int lineEndIdx = 0;
    for(int i = 1; i <= lineNo && lineEndIdx + 1 < text.length(); i++)
    {
        lineEndIdx = text.indexOf('\n', lineEndIdx + 1) ;
    }
    return lineEndIdx;
}

你还可以进一步改善它......

答案 1 :(得分:3)

使用javax.swing.text.Utilities.getRowStart()/getRowEnd()获取Highlighter

的偏移量