6

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

Community
  • 1
  • 1
Ashley
  • 2,256
  • 1
  • 33
  • 62
  • 1
    _I can set the custom render on a particular cell_ - no that's not what you need :-) Instead, you need a custom renderer which decides about the visuals of its rendering component based on the parameters passed into the getXXCellRendererComponent, see f.i. http://docs.oracle.com/javase/tutorial/uiswing/components/table.html – kleopatra Mar 07 '12 at 14:26
  • I see. So I set the custom renderer to all cells and then check which cell needs highlighting within the custom renderer method? I still don't know how to check what cell I am in within the custom renderer method. – Ashley Mar 07 '12 at 16:17
  • 1
    I don't understand what you don't understand ;-) The parameters of the method tell you all you need to know. You did read the tutorial I linked to above, didn't you? – kleopatra Mar 07 '12 at 16:22
  • If I do a condition statement in my `getTableCellRendererComponent` method - `if(column == 0 && row == 1) c.setBackground(new java.awt.Color(255, 72, 72));` it still paints all cells red. – Ashley Mar 07 '12 at 16:48

2 Answers2

3

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

mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • Just to add one more possible to point 1) you can also create your own `EditorKit` with its own `View`, which is a more work but usually outperforms the other options – stryba Mar 06 '12 at 21:53
  • @stryba thank you for notice, hmmm for all EditorKit or HtmlEditorKit rellated questions is there our (JTextComponent's King @StanislavL), – mKorbel Mar 06 '12 at 22:01
  • -1 for 2) (time to start a quest against that spreading not quite correct advice :-) that's _not_ the general way to achieve visual cell decorations in application code, instead custom renderers are. The basic rule is: never-ever extend a JSomething for application needs, they are meant to be used as-is. That said: extending core components with additional functionality (in this context f.i. add api to allow custom striping) is fine: this allows to re-use the extended component without _further_ extension in all your applications, they can configure the custom component as appropriate. – kleopatra Mar 07 '12 at 10:07
  • This answer and the comment above have left me slightly confused. I assume that I should extend JTable in a custom class and ?override? the `prepareRenderer()` method. I should confess also that I am not very well experience with Java Swing. – Ashley Mar 07 '12 at 12:13
  • 1) maybe would be better waiting for @kleopatra's reply, 2) this comment is about `pass desired value` and be sure basically her honesty have got true, and more than valuable, in the case that you know something about Swing, 3) I have to notice this not possible to answering your question without edit your question with a [SSCCE](http://sscce.org/), 4) I take this question as interview only, – mKorbel Mar 07 '12 at 13:43
  • hmmm... as often as I read the bullets in your comment, I can't make much sense out of them, except the part about the SSCCE :-) – kleopatra Mar 07 '12 at 14:31
2

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

nIcE cOw
  • 24,468
  • 7
  • 50
  • 143
  • Thank you for your answer. Although that addresses another issue I have in the `Calculations Area` it still doesn't address the problem I am having with colouring JTable cells. – Ashley Mar 07 '12 at 12:04
  • @Ash : Ahhh... Wish I could help you on `JTable` s, but my knowledge in them is way too short. I am sorry about that. But hoping this answer might help you somewhere in your endeavour and hopefully you might find your answer soon :-) – nIcE cOw Mar 07 '12 at 12:30