突出显示JTextArea中的一个特定行/行

时间:2012-04-17 12:56:46

标签: java swing highlight jtextarea

我试图突出显示JTextArea中的一个特定行,但我不知道要去做什么。我需要获取特定的行然后突出显示它。我已经阅读了其他帖子,但我仍然不明白如何将它们结合在一起来解决我的问题...帮助将非常感激。

3 个答案:

答案 0 :(得分:11)

尝试使用此代码示例,并询问是否有不明确的内容:

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

public class TextHighlight
{
    private JTextArea tarea;
    private JComboBox cbox;
    private JTextField lineField;
    private String[] colourNames = {"RED", "ORANGE", "CYAN"};

    private Highlighter.HighlightPainter painter;

    private void createAndDisplayGUI()
    {
        final JFrame frame = new JFrame("Text HIGHLIGHT");
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

        JPanel contentPane = new JPanel();
        contentPane.setBorder(BorderFactory.createTitledBorder(
                BorderFactory.createEmptyBorder(5, 5, 5, 5), "Highlighter JTextArea"));

        tarea = new JTextArea(10, 10);
        JScrollPane scrollPane = new JScrollPane(tarea);
        contentPane.add(scrollPane);

        JButton button = new JButton("HIGHLIGHT TEXT");
        button.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent ae)
            {
                int selection = JOptionPane.showConfirmDialog(
                        frame, getOptionPanel(), "Highlighting Options : ", JOptionPane.OK_CANCEL_OPTION
                                                , JOptionPane.PLAIN_MESSAGE);
                if (selection == JOptionPane.OK_OPTION)                             
                {
                    System.out.println("OK Selected");
                    int lineNumber = Integer.parseInt(lineField.getText().trim());
                    try
                    {
                        int startIndex = tarea.getLineStartOffset(lineNumber);
                        int endIndex = tarea.getLineEndOffset(lineNumber);
                        String colour = (String) cbox.getSelectedItem();

                        if (colour == colourNames[0])
                        {
                            System.out.println("RED Colour");
                            painter = new DefaultHighlighter.DefaultHighlightPainter(Color.RED);
                            tarea.getHighlighter().addHighlight(startIndex, endIndex, painter);
                        }
                        else if (colour == colourNames[1])
                        {
                            System.out.println("ORANGE Colour");
                            painter = new DefaultHighlighter.DefaultHighlightPainter(Color.ORANGE);
                            tarea.getHighlighter().addHighlight(startIndex, endIndex, painter);
                        }
                        else if (colour == colourNames[2])
                        {
                            System.out.println("CYAN Colour");
                            painter = new DefaultHighlighter.DefaultHighlightPainter(Color.CYAN);
                            tarea.getHighlighter().addHighlight(startIndex, endIndex, painter);
                        }
                    }
                    catch(BadLocationException ble)
                    {
                        ble.printStackTrace();
                    }
                }
                else if (selection == JOptionPane.CANCEL_OPTION)
                {
                    System.out.println("CANCEL Selected");
                }
                else if (selection == JOptionPane.CLOSED_OPTION)
                {
                    System.out.println("JOptionPane closed deliberately.");
                }
            }
        });

        frame.add(contentPane, BorderLayout.CENTER);
        frame.add(button, BorderLayout.PAGE_END);

        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    private JPanel getOptionPanel()
    {
        JPanel panel = new JPanel();
        panel.setLayout(new GridLayout(0, 2, 5, 5));

        JLabel lineNumberLabel = new JLabel("Enter Line Number : ");
        lineField = new JTextField(10);

        JLabel colourLabel = new JLabel("Select One Colour : ");
        cbox = new JComboBox(colourNames);

        panel.add(lineNumberLabel);
        panel.add(lineField);
        panel.add(colourLabel);
        panel.add(cbox);

        return panel;
    }

    public static void main(String... args)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            public void run()
            {
                new TextHighlight().createAndDisplayGUI();
            }
        });
    }
}

以下是它的输出:

HIGHLIGHTER EXAMPLE

答案 1 :(得分:3)

如果您无法选择TextArea到TextField,则按钮单击会导致JTextArea失去焦点,因此不会显示其选择。 按钮点击事件使用btnImport.transferFocusBackward();来解决问题。

答案 2 :(得分:0)

这样做: 这是Java的文本区域摆动

JTextArea area = new JTextArea(); 
int startIndex = area.getLineStartOffset(2);
int endIndex = area.getLineEndOffset(2);
painter = new DefaultHighlighter.DefaultHighlightPainter(Color.RED);
area.getHighlighter().addHighlight(startIndex, endIndex, painter);