简体   繁体   English

JTable,JTextArea或JEditorPane突出显示代码行?

[英]JTable, JTextArea or JEditorPane to highlight lines of code?

Update: 更新:

I've found a partial solution in this answer here , by adding the following code: 我发现在这个答案部分解决这里 ,通过添加以下代码:

class CustomRenderer extends DefaultTableCellRenderer 
{
    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column)
    {
        Component c = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
        c.setBackground(new java.awt.Color(255, 72, 72));
        return c;
    }
}

And then passing it to my JTable object: 然后将它传递给我的JTable对象:

jTable2.setDefaultRenderer(String.class, new CustomRenderer());

This works correctly and now the table rows are coloured red: 这工作正常,现在表行为红色:

Psuedocode面板

The only thing I need to know now is how to restrict the colouring to a single row and a single cell. 我现在唯一需要知道的是如何将着色限制为单行和单个单元格。

After further research I need a setCellRender() method so that I can set the custom render on a particular cell, but this method doesn't exist. 经过进一步研究后,我需要一个setCellRender()方法,以便我可以在特定单元格上设置自定义渲染,但此方法不存在。


Question: 题:

I want to create a visual component of step-by-step pseudocode execution. 我想创建一个逐步伪代码执行的可视组件。

To do this I have created a JTable and now I am looking for ways to highlight each row (or cell since there is only one column) to display which line is being executed. 为此,我创建了一个JTable,现在我正在寻找突出显示每一行(或单元格,因为只有一列)来显示正在执行哪一行的方法。

I've included a mockup below on the final GUI. 我在下面的最终GUI中包含了一个模型。 As you can see in the Pseudocode panel I've highlighted the final row. 正如您在Pseudocode面板中看到的那样,我突出显示了最后一行。

Please ignore the arrows they are not strictly related to the question. 请忽略它们与问题没有严格关联的箭头。

线框

I've started to implement the mockup in Netbeans Matisse (this is 1 of 3 algorithms). 我已经开始在Netbeans Matisse中实现这个模型(这是3种算法中的1种)。 However I don't know how to highlight the single line code line 1 in the JTable component. 但是我不知道如何突出JTable组件中的单行code line 1

Would it be easier to use a different type of component? 使用不同类型的组件会更容易吗?

Later I'll also need to be able to re-color individual cells as show in the mockup's Table JPanel. 稍后我还需要能够重新着色单个单元格,如模型的表格JPanel中所示。 How can this be implemented? 如何实施?

部分实施

1) use JTextPane for supporting styled text, there you have three choices 1)使用JTextPane来支持样式文本,你有三个选择

  • use HighLighter 使用HighLighter

  • use Html formatted text (Java6 in current form supporting <= Html3.2 and with reduced supporting of css, sure in compare with Html5 ) 使用Html格式的文本(当前形式的Java6支持<= Html3.2并且减少了对css的支持,肯定与Html5

  • combine both am options together 将两种选择结合在一起

  • write own EditorKit or HtmlEditorKit (thanks to @stryba) 自己编写EditorKit或HtmlEditorKit(感谢@stryba)

2) for JTable pass desired value to the prepareRenderer() rather than implement getTableCellRendererComponent() 2) JTable将所需值传递给prepareRenderer()而不是实现getTableCellRendererComponent()

3) if value for JSlider is modifiable (from another JComponents) then look for BoundedRangeModel 3)如果JSlider的值是可修改的(来自另一个JComponents),那么查找BoundedRangeModel

A small code for your help, how to achieve highlighting specific text literal with desired background on JTextPane : 一个小代码供您使用,如何在JTextPane上突出显示具有所需背景的特定文本文字:

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import javax.swing.border.*;

import javax.swing.text.AttributeSet;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyleContext;

public class TextPaneTest extends JFrame
{
    private JPanel topPanel;
    private JTextPane tPane;

    public TextPaneTest()
    {
        topPanel = new JPanel();        

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);            

        EmptyBorder eb = new EmptyBorder(new Insets(10, 10, 10, 10));

        tPane = new JTextPane();                
        tPane.setBorder(eb);
        //tPane.setBorder(BorderFactory.createLineBorder(Color.DARK_GRAY));
        tPane.setMargin(new Insets(5, 5, 5, 5));

        topPanel.add(tPane);

        appendToPane(tPane, "My Name is Too Good.\n", Color.RED, Color.YELLOW);
        appendToPane(tPane, "I wish I could be ONE of THE BEST on ", Color.BLUE, Color.WHITE);
        appendToPane(tPane, "Stack", Color.PINK, Color.WHITE);
        appendToPane(tPane, "Over", Color.YELLOW, Color.RED.brighter());
        appendToPane(tPane, "flow", Color.BLACK, Color.GREEN.darker());

        getContentPane().add(topPanel);

        pack();
        setVisible(true);   
    }

    private void appendToPane(JTextPane tp, String msg, Color c, Color bColor)
    {
        StyleContext sc = StyleContext.getDefaultStyleContext();
        AttributeSet aset = sc.addAttribute(SimpleAttributeSet.EMPTY, StyleConstants.Foreground, c);

        aset = sc.addAttribute(aset, StyleConstants.Background, bColor);
        //  aset = sc.addAttribute(SimpleAttributeSet.EMPTY, StyleConstants.Foreground, c);

        aset = sc.addAttribute(aset, StyleConstants.FontFamily, "Lucida Console");
        aset = sc.addAttribute(aset, StyleConstants.Alignment, StyleConstants.ALIGN_JUSTIFIED);

        int len = tp.getDocument().getLength();
        tp.setCaretPosition(len);
        tp.setCharacterAttributes(aset, false);
        tp.replaceSelection(msg);
    }

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

Here is the output : 这是输出:

JTEXTPANE文本突出显示

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

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