Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within Swing / AWT / SWT
Search Coderanch
Advance search
Google search
Register / Login
Post Reply
Bookmark Topic
Watch Topic
New Topic
programming forums
Java
Mobile
Certification
Databases
Caching
Books
Engineering
Micro Controllers
OS
Languages
Paradigms
IDEs
Build Tools
Frameworks
Application Servers
Open Source
This Site
Careers
Other
Pie Elite
all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Campbell Ritchie
Jeanne Boyarsky
Ron McLeod
Paul Clapham
Liutauras Vilda
Sheriffs:
paul wheaton
Rob Spoor
Devaka Cooray
Saloon Keepers:
Stephan van Hulst
Tim Holloway
Carey Brown
Frits Walraven
Tim Moores
Bartenders:
Mikalai Zaikin
Forum:
Swing / AWT / SWT
How to flash a row in JTable?
Caly LeeAnn
Ranch Hand
Posts: 55
posted 17 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
I want to flash a particular row in JTable. And I don't want to use the following method because a row gets flashed if it is selected.
table.addRowSelectionInterval(row, row);
table.setSelectionBackground(Color.RED);
Thread.sleep(500);
table.removeRowSelectionInterval(row, row);
table.setSelectionBackground(Color.WHITE);
Thread.sleep(500);
I try to use TableCellRenderer but get quite confused.
Any advice would be appreciated.
Thanks.
Caly
Craig Wood
Ranch Hand
Posts: 1535
posted 17 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.Timer; import javax.swing.event.*; import javax.swing.table.*; public class RowFlash implements ActionListener, ChangeListener { FlashRenderer renderer; public void actionPerformed(ActionEvent e) { renderer.flashRow(); } public void stateChanged(ChangeEvent e) { JSlider slider = (JSlider)e.getSource(); int value = slider.getValue(); renderer.setFlashRowSelection(value); } private JTable getTable() { int rows = 32, cols = 3; String[] colNames = { "column 1", "column 2", "column 3" }; Object[][] data = new Object[rows][cols]; for(int row = 0; row < rows; row++) for(int col = 0; col < cols; col++) data[row][col] = "item " + (row*cols + col + 1); DefaultTableModel model = new DefaultTableModel(data, colNames); JTable table = new JTable(model); renderer = new FlashRenderer(table); TableColumnModel colModel = table.getColumnModel(); for(int j = 0; j < table.getColumnCount(); j++) { TableColumn col = colModel.getColumn(j); col.setCellRenderer(renderer); } return table; } private JPanel getControls(JTable table) { int min = 0; int max = table.getRowCount()-1; int value = min; JSlider slider = new JSlider(JSlider.HORIZONTAL, min, max, value); slider.addChangeListener(this); slider.setSnapToTicks(true); slider.setPaintTicks(true); slider.setMinorTickSpacing(1); slider.setMajorTickSpacing(5); slider.setPaintLabels(true); JButton flash = new JButton("flash"); flash.addActionListener(this); JPanel panel = new JPanel(new GridBagLayout()); GridBagConstraints gbc = new GridBagConstraints(); gbc.insets = new Insets(2,2,2,2); gbc.weightx = 1.0; gbc.fill = GridBagConstraints.HORIZONTAL; panel.add(slider, gbc); gbc.weightx = 0.0; gbc.fill = GridBagConstraints.NONE; panel.add(flash, gbc); return panel; } public static void main(String[] args) { RowFlash test = new RowFlash(); JTable table = test.getTable(); JFrame f = new JFrame(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.getContentPane().add(new JScrollPane(table)); f.getContentPane().add(test.getControls(table), "South"); f.setSize(400,400); f.setLocation(200,200); f.setVisible(true); } } class FlashRenderer extends DefaultTableCellRenderer implements ActionListener { JTable table; int flashRowSelection; Color selectionColor; Timer flashTimer; public FlashRenderer(JTable table) { this.table = table; flashRowSelection = -1; selectionColor = Color.red; flashTimer = new Timer(500, this); flashTimer.setRepeats(false); } public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) { super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column); if(row == flashRowSelection && flashTimer.isRunning()) setBackground(selectionColor); else if(isSelected) { super.setForeground(table.getSelectionForeground()); super.setBackground(table.getSelectionBackground()); } else { super.setForeground(table.getForeground()); super.setBackground(table.getBackground()); } return this; } public void actionPerformed(ActionEvent e) { flashTimer.stop(); table.repaint(); } public void setFlashRowSelection(int frs) { flashRowSelection = frs; } public void flashRow() { if(!flashTimer.isRunning()) flashTimer.start(); Rectangle r = table.getCellRect(flashRowSelection, 0, true); table.scrollRectToVisible(r); table.repaint(); } }
Caly LeeAnn
Ranch Hand
Posts: 55
posted 17 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Thanks
alot
, Craig! It's a really good example.
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
JTable
JTable ........
JTable
Flashing a row in JTable ...
JTable(!!!)
More...