简体   繁体   English

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

[英]Highlight one specific row/line in JTextArea

I am trying to highlight just one specific row in JTextArea , but I have no idea as to going about it. 我正在尝试仅突出显示JTextArea特定行,但是我对此一无所知。 I need to get the specific row and then highlight it. 我需要获取特定行,然后突出显示它。 I've read the other posts, but I still do not understand how to bring it together to solve my problem...help would be much appreciated. 我已经阅读了其他文章,但是我仍然不明白如何将其组合起来以解决我的问题...帮助将不胜感激。

Try your hands on this code example, and do ask if something is not clear : 尝试使用此代码示例,并询问是否不清楚:

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();
            }
        });
    }
}

Here is the output of it : 这是它的输出:

突出示例

If you are unable to select TextArea to TextField reason is button click causes the JTextArea to lose focus and thus not show its selection. 如果无法选择TextArea到TextField,则单击按钮会导致JTextArea失去焦点,因此不会显示其选择。 on button click event use btnImport.transferFocusBackward(); 在按钮单击事件上使用btnImport.transferFocusBackward(); to resolve issue. 解决问题。

do like that : this is the text area swing of java 这样做:这是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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM