Question

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:

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

This works correctly and now the table rows are coloured red:

Psuedocode Panel

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.


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.

I've included a mockup below on the final GUI. As you can see in the Pseudocode panel I've highlighted the final row.

Please ignore the arrows they are not strictly related to the question.

Wireframe

I've started to implement the mockup in Netbeans Matisse (this is 1 of 3 algorithms). However I don't know how to highlight the single line code line 1 in the JTable component.

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. How can this be implemented?

Partial implementation

Was it helpful?

Solution

1) use JTextPane for supporting styled text, there you have three choices

  • use HighLighter

  • use Html formatted text (Java6 in current form supporting <= Html3.2 and with reduced supporting of css, sure in compare with Html5)

  • combine both a.m. options together

  • write own EditorKit or HtmlEditorKit (thanks to @stryba)

2) for JTable pass desired value to the prepareRenderer() rather than implement getTableCellRendererComponent()

3) if value for JSlider is modifiable (from another JComponents) then look for BoundedRangeModel

OTHER TIPS

A small code for your help, how to achieve highlighting specific text literal with desired background on 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 TEXT HIGHLIGHTING

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top