JTable, JTextArea or JEditorPane to highlight lines of code?

Update:

I 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 pass it to my JTable object:

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

This works correctly, and now the table rows are colored red:

Psuedocode panel

The only thing I need to know now is to limit the coloring to one line and one cell.

After further research, I need the setCellRender() method so that I can set up custom rendering in a specific cell, but this method does not exist.


Question:

I want to create a visual component for phased execution of pseudocode.

For this, I created JTable, and now I'm looking for ways to select each row (or cell, since there is only one column) to display which row is running.

I have included the layout below in the final GUI. As you can see in the Pseudocode panel, I highlighted the last line.

Please ignore the arrows that they are not strictly related to the issue.

Wireframe

I started implementing the layout in Netbeans Matisse (this is 1 of 3 algorithms). However, I do not know how to select one line of code line 1 in the JTable component.

Would it be easier to use a different type of component?

Later, I will also need to repaint individual cells, as shown in the Table JPanel layout. How can this be implemented?

Partial implementation

+6
source share
2 answers

1) use JTextPane to support stylized text, there you have three options

  • use HighLighter

  • use Html formatted text (Java6 in the current form, supporting <= Html3.2 and with reduced css support compared to Html5 )

  • combine both parameters.

  • write your own Kit editor or HtmlEditorKit (thanks @stryba)

2) for JTable, pass the desired value to prepareRenderer() instead of implementing getTableCellRendererComponent()

3) if the value for JSlider can be changed (from another JComponents) then find BoundedRangeModel

+3
source

A little code for your help on how to achieve the allocation of a specific text literal with the 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 result:

JTEXTPANE TEXT HIGHLIGHTING

+2
source

Source: https://habr.com/ru/post/910115/


All Articles