JProgressBar setValue is not working, tried with SwingUtilities as well - java

I have implemented a JProgressBar in a JTable.
I used renderer for the ProgressBar NOT EDITOR.
Now I tried to implement a ProgressBar set value but due to EDT its not working so I used SwingUtilties but it did not work as well.
EXPECTED BEHAVIOUR - The JProgressBar must be setting value to 80 ,
currently it is showing only 0%
public class SkillSetTableProgressBarRenderer extends JProgressBar implements
TableCellRenderer {
public SkillSetTableProgressBarRenderer() {
super(0, 100);
super.setPreferredSize(new Dimension(100, 80));
}
#Override
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int row, int column) {
final JProgressBar bar = (JProgressBar) value;
if (bar.getString().equals("JAVA") || bar.getString().equals("SWING"))
super.setString("Mellow");
else
super.setString("GOOD");
setOpaque(true);
table.setOpaque(true);
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
System.err.println("MAIN ANDER");
setValue(80);
bar.setValue(80);
}
});
super.setStringPainted(true);
super.setIndeterminate(true);
super.setPreferredSize(new Dimension(140, 16));
if (isSelected) {
super.setBackground(table.getSelectionBackground());
super.setForeground(table.getSelectionForeground());
// this.setBackground(table.getSelectionBackground());
} else {
super.setForeground(table.getForeground());
super.setBackground(Color.WHITE);
}
return this;
}
}

Looks like you're trying to use a ProgressBar as a CellRenderer. One thing you should know, is that CellRenders are only called when a cell is being drawn, and setting values at other moments has no effect: this is because Swing uses a flyweight pattern for renderers, and thus reuses the renderer.
To get the effect you want, you should
notify the table that the cell is to be updated, for instance by updating its underlying model (that will fire the necessary events), and
set all values you need before the getTableCellRendererComponent return, so, remove the invokeLater (the getter is called on the EDT, so you don't need to worry about threading there).

1) even is possible, don't create JComponents inside TableCellRenderer, nor re_create Object, Renderer is only for formatting cell contents
2) use SwingWorker for moving with progress in JProgressBar (if you have real important reasons, then use Runnable#Tread)
example about Runnable#Tread
import java.awt.Component;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JProgressBar;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellRenderer;
public class TableWithProgressBars {
public static class ProgressRenderer extends JProgressBar implements TableCellRenderer {
private static final long serialVersionUID = 1L;
public ProgressRenderer(int min, int max) {
super(min, max);
this.setStringPainted(true);
}
#Override
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int row, int column) {
this.setValue((Integer) value);
return this;
}
}
private static final int maximum = 100;
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
new TableWithProgressBars().createGUI();
}
});
}
public void createGUI() {
final JFrame frame = new JFrame("Progressing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Integer[] oneRow = {0, 0, 0, 0};
String[] headers = {"One", "Two", "Three", "Four"};
Integer[][] data = {oneRow, oneRow, oneRow, oneRow, oneRow,};
final DefaultTableModel model = new DefaultTableModel(data, headers);
final JTable table = new JTable(model);
table.setDefaultRenderer(Object.class, new ProgressRenderer(0, maximum));
table.setPreferredScrollableViewportSize(table.getPreferredSize());
frame.add(new JScrollPane(table));
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
new Thread(new Runnable() {
#Override
public void run() {
Object waiter = new Object();
synchronized (waiter) {
int rows = model.getRowCount();
int columns = model.getColumnCount();
Random random = new Random(System.currentTimeMillis());
boolean done = false;
while (!done) {
int row = random.nextInt(rows);
int column = random.nextInt(columns);
Integer value = (Integer) model.getValueAt(row, column);
value++;
if (value <= maximum) {
model.setValueAt(value, row, column);
try {
waiter.wait(15);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
done = true;
for (row = 0; row < rows; row++) {
for (column = 0; column < columns; column++) {
if (!model.getValueAt(row, column).equals(maximum)) {
done = false;
break;
}
}
if (!done) {
break;
}
}
}
frame.setTitle("All work done");
}
}
}).start();
}
}

Take a look at the Swing table tutorial: renderers and editors. The renderer uses the component to make a sort of 'stamp'. Changing the component afterwards has no effect since the component is not actually contained in your UI. That is one of the reasons you can return the same component over and over again, which would not be possible if the component was actually contained in the table. This is all explained in the linked tutorial.
To solve your problem, just remove the SwingUtilities.invokeLater method call, and return a JProgressBar where the correct progress is set.
And if I see this correctly, you have a TableModel containing a JProgressBar. I hope there is a good reason for this, since this is a UI component which should normally not be included on the model side. This is based on your
final JProgressBar bar = (JProgressBar) value;
call in the renderer. I would strongly suggest to take a look at your TableModel to see whether this can be avoided

Related

Bug in popping up JComboBox and JPopup

I am having a bug I'm having trouble tracking down and was wondering if anyone had seen anything like this. I am building my code in Java 8. I run/test this code on both Macintosh & Windows. This bug only happens on a machine running Windows OS. (Haven't tried linux yet) I am testing on Windows 8.
I have a JComboBox that is in the same window as a JTable and a JTree which I have modified to add checkboxes. The JComboBox initially works perfectly. Popups up and works correctly. But once I interact with the JTable or JTree it no longer functions. It does not popup anymore.
When this pane is further added to a JTabbedPane, I can get it functioning again when tab out of this pane & back into the one with the JComboBox. In an attempt to go around this bug (and understand it better) I tried using a JPopupMenu instead of the JComboBox. The bug happens with the JPopup as well and it's easier to tell where it fails. When I debug I find that I do enter the mousePressed event and everything looks good in the mousePressed event. But the JPopupMenu.show() function doesn't get executed correctly.
#Override
public void mousePressed(MouseEvent e) {
topDirPopup.show(e.getComponent(), e.getX(), e.getY());
}
I could show more code but I'm not sure what code is interacting with the popup.
Any ideas? Any work arounds?
Isolated it down to it's going wrong in the checkboxes in the JTable (or the JTree) but I'm using standard Swing Table so it's easier to isolate. Very basic code.. doesn't read anything from the file system. Doesn't do any modifications to swing objects. Still see the bug.
I stripped most everything that is useless out of my code.
Main Test Class.
import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
public class Test extends JPanel {
Test() {
super(new BorderLayout());
FileSelectionPane fpane = new FileSelectionPane();
JLabel label = new JLabel("File Selection Pane Test");
this.add(fpane, BorderLayout.CENTER);
this.add(label, BorderLayout.NORTH);
}
private static void createAndShowGUI() {
//Create and set up the window.
JFrame frame = new JFrame("File Selection Test Tool");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Add content to the window.
frame.add(new Test());
//Display the window.
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
//Schedule a job for the event dispatch thread:
//creating and showing this application's GUI.
SwingUtilities.invokeLater(new Runnable() {
public void run() {
//Turn off metal's use of bold fonts
UIManager.put("swing.boldMetal", Boolean.FALSE);
createAndShowGUI();
}
});
}
}
My FileTool Pane. Yes I have a BorderLayout within a BorderLayout. It's silly in this example but my actual code is more complex and I have a Tabbed Pane in between these two.
import java.awt.BorderLayout;
import java.awt.Panel;
import javax.swing.JComboBox;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
public class FileSelectionPane extends Panel {
/**
*
*/
private static final long serialVersionUID = 1784004323821479260L;
/**
*
*/
JTable fileTable;
JComboBox<String> topDirComboBox;
FileSelectionPane() {
super(new BorderLayout());
createTopDirectoryComboBox();
FileSelectionTableModel tModel = new FileSelectionTableModel();
fileTable = new JTable(tModel);
JScrollPane jScrollPane2 = new JScrollPane(fileTable);
this.add(jScrollPane2, BorderLayout.CENTER);
this.add(topDirComboBox, BorderLayout.NORTH);
}
void createTopDirectoryComboBox() {
topDirComboBox = new JComboBox<String>();
topDirComboBox.addItem("C:\\Test\\Fake");
topDirComboBox.addItem("C:\\Test\\More");
topDirComboBox.addItem("C:\\Test\\View");
}
public class FileSelectionTableModel extends AbstractTableModel {
private static final long serialVersionUID = 1L;
static final int numCols = 4;
private String[] columnNames = {"..", "Name", "Type", "Size", "Creation Date", "Modification Date"};
private final static int checkCol = 0;
private final static int nameCol = 1;
private final static int typeCol = 2;
private final static int sizeCol = 3;
public int getColumnCount() { return numCols; }
public int getRowCount() { return 5; }
public String getColumnName(int col) {
return columnNames[col];
}
// Only the check item is editable.
public boolean isCellEditable(int row, int col)
{
return (col == checkCol) ? true : false;
}
Boolean tBool[] = new Boolean[10];
public void setValueAt(Object value, int row, int col) {
if (col == checkCol)
tBool[row] = (Boolean) value;
}
public Object getValueAt(int row, int col) {
Object rtnObject = null;
switch (col) {
case checkCol:
rtnObject = (Object) tBool[row];
break;
case nameCol:
rtnObject = (Object) "Testing";
break;
case sizeCol:
rtnObject = (Object) "5";
break;
}
return rtnObject;
}
/* Set up the types of each column */
public Class<?> getColumnClass(int col) {
// getValueAt(0, c).getClass();
Class<?> rtnValue = null;
switch (col)
{
case checkCol:
rtnValue = Boolean.class;
break;
case nameCol:
rtnValue = String.class;
break;
case sizeCol:
rtnValue = String.class;
break;
case typeCol:
rtnValue = String.class;
break;
}
return rtnValue;
}
}
}

JTable cell text color changing

I´m rather new to Java and programming itself, so excuse me for the question. What I´m trying to do is the following:
I´m making a bookkeeping program. On the column where the income/outcome is displayed, I want it so when the user enters a negative number (eg. -1.150€),
the number turns red( or any color really, but red is what most bookkeeping programs use). only that specific cell on that column only. I have not started with a code yet so I cannot input one here. I also do not need it to be right-aligned since I have already done that.
PS. Sorry if this post/question already exists, I searched but I found nothing that could help me much.
A small example with double values in a single column. This version uses JTable.setDefaultRenderer for Double.class.
You can also set colors
From an override of JTable.prepareRenderer
From a renderer set individually for columns by calling TableColumn.setCellRenderer; TableColumn instances can be retrieved from the TableColumnModel
import java.awt.*;
import javax.swing.*;
import javax.swing.table.DefaultTableCellRenderer;
#SuppressWarnings("serial")
public class TableWithColors {
protected static JTable createTable() {
Object[][] rows = new Object[][] {{1.23d},{-20.5d},{5.87d},{2.23d},{-7.8d},{-8.99d},{9d},{16.25d},{4.23d},{-26.22d},{-14.14d}};
Object[] cols = new Object[]{"Balance"};
JTable t = new JTable(rows,cols) {
#Override
public Class<?> getColumnClass(int column) {
if(convertColumnIndexToModel(column)==0) return Double.class;
return super.getColumnClass(column);
}
};
t.setDefaultRenderer(Double.class, new DefaultTableCellRenderer(){
#Override
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.setForeground(((Double) value)>0 ? Color.BLUE : Color.RED);
return c;
}
});
return t;
}
private static JFrame createFrame() {
JFrame f = new JFrame("Table with colors");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setLayout(new BorderLayout());
f.add(new JScrollPane(createTable()),BorderLayout.CENTER);
f.setSize(new Dimension(60,255));
return f;
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
createFrame().setVisible(true);
}
});
}
}
Goes like:

Color JTable row

I use TableCellRenderer in a function to change the color of a row.
public void change_color(JTable tableName){
tableName.setDefaultRenderer(Object.class, new TableCellRenderer(){
private DefaultTableCellRenderer DEFAULT_RENDERER = new DefaultTableCellRenderer();
#Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
Component c = DEFAULT_RENDERER.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
c.setBackground(Color.RED);
//Add below code here
return c;
}
});
}
It changes the color of entire table. To call this function i use following condition.
if(ellapsed.getMinutes() > 30)
{
change_color(table_dineIn,ellapsed.getMinutes());
}
Cant figure out the problem. I have tried other codes to but nothing helped me.
The cell renderers in JTable, JList etc. are used like a stamp (see Editors and Renderers for details).
This means that usually, the same (identical) JComponent is used for painting all the cells. This component is only filled with the appropriate contents before it is used for painting the cell. And when the background is set to RED, it will remain red until it is set to a different color.
I'm not sure what you wanted to achieve by using this DEFAULT_RENDERER instance. You could simply inherit from DefaultTableCellRenderer and return the component (which is in fact the renderer itself) direcly. However, in any case, you'll have to include some code that makes sure that the appropriate color is set for every call, roughly like
....
if (shouldBeRed(row, column)) {
c.setBackground(Color.RED);
} else {
c.setBackground(notRed);
}
return c;
(note that this can actually be hidden in the call to the super method if you extend DefaultTableCellRenderer, but the details here depend on whether you'll keep this DEFAULT_RENDERER instance or not...)
You might also be interested in this example of blinking table cells, showing how several table cells may be assigned different colors based on certain criteria.
EDIT: An example. Although I usually try to avoid answereing questions like this with examples like this, because even for the slightest modification, you'll ask another question, which in this case will probably in the line of
how to un-highlight the row
how to highlight multiple rows
how to highlight multiple rows with different colors
...
You'll find answers to all these questions in https://stackoverflow.com/a/24556135/3182664 - in the meantime, I'll mark this question as a duplicate.
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.table.DefaultTableCellRenderer;
public class TableRowColor
{
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
createAndShowGUI();
}
});
}
private static void createAndShowGUI()
{
JFrame frame = new JFrame("TableRowColor");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(new BorderLayout());
final JTable table = createTable();
JScrollPane scrollPane = new JScrollPane(table);
frame.getContentPane().add(scrollPane, BorderLayout.CENTER);
JButton changeColorButton = new JButton("Change color");
changeColorButton.addActionListener(new ActionListener()
{
#Override
public void actionPerformed(ActionEvent e)
{
changeColor(table, 1);
}
});
frame.getContentPane().add(changeColorButton, BorderLayout.SOUTH);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
private static JTable createTable()
{
String[] columnNames = { "First Name", "Last Name", "Sport", };
Object[][] data = { { "Kathy", "Smith", "Snowboarding" },
{ "John", "Doe", "Rowing" }, { "Sue", "Black", "Knitting" },
{ "Jane", "White", "Speed reading" }, { "Joe", "Brown", "Pool" } };
return new JTable(data, columnNames);
}
public static void changeColor(JTable table, final int coloredRow)
{
table.setDefaultRenderer(Object.class, new DefaultTableCellRenderer()
{
#Override
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 == coloredRow)
{
setBackground(Color.RED);
}
else
{
setBackground(null);
}
return this;
}
});
table.repaint();
}
}
The following would color 30th row of the table:
public void change_color(JTable tableName){
tableName.setDefaultRenderer(Object.class, new TableCellRenderer(){
private DefaultTableCellRenderer DEFAULT_RENDERER = new DefaultTableCellRenderer();
#Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
Component c = DEFAULT_RENDERER.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
if (row == 30) {
c.setBackground(Color.RED);
}
return c;
}
});
}
Edit:
If you want to choose table color based on data, add that data to the the table model (e.g. a Time stored in particular column), then use the model in the renderer. For example:
static final int TIME_ELLAPSED_COL = ...;
...
if (((Time) table.getModel().getValueAt(row, TIME_ELLAPSED_COL)).getMinutes() > 30) {
c.setBackground(Color.RED);
}
...

Sorted JTable's return value of rowAtPoint(Point)

I am trying to make a Checkbox change value on click in a JTable. Here is the code I use for that in the MouseListener
public void mouseClicked(MouseEvent e) {
Point mouse = e.getPoint();
int row = table.rowAtPoint(mouse);
int col = table.columnAtPoint(mouse);
if (col == 0) tableModel.setValueAt(new Boolean(!(Boolean) tableModel.getValueAt(row, col)), row, col);
}
The problem is, that when I sort the table, this happens
Here is an SSCCE
import javax.swing.JFrame;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.table.AbstractTableModel;
#SuppressWarnings("serial")
public class SSCCE extends JFrame {
JTable table;
public SSCCE() {
setSize(300, 200);
Object[][] data = { {false, "This is false"}, {true, "This is true"}};
table = new JTable(new CustomTableModel(data));
add(table);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setVisible(true);
}
private class CustomTableModel extends AbstractTableModel {
Object[][] data;
public CustomTableModel(Object[][] data) {
this.data = data;
}
public Class<?> getColumnClass(int columnIndex) {
return data[0][columnIndex].getClass();
}
public int getColumnCount() {
return data[0].length;
}
public int getRowCount() {
return data.length;
}
public Object getValueAt(int rowIndex, int columnIndex) {
return data[rowIndex][columnIndex];
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new SSCCE();
}
});
}
}
Is there a way around this? Or a better method (not ListListener) to detect clicks on cells?
There is no need to use a MouseListener. You just need to use a proper editor for the column and the table will handle it for you.
Read the section from the Swing tutorial on How to Use Tables for more information and working examples.
Basically you need to do two things:
Add Boolean data to the TableModel
Override the getColumnClass(...) method of the TableModel to return Boolean.class for that column and the table will choose the appropriate editor.
Above is the answer for your question, but for future information the MouseEvent is relative to the table, so you want to use table methods to access the data. That is you would use table.getValueAt(...) and table.setValueAt(...). These reference the data as it is currently displayed in the view of the table. That is the view could be sorted or the column could have been moved.

CellTable with Image Column

I have celltable with 4 column (name size addedBy modifiedBy )
all the value is filled during the run time.
The table actully show the documents.
Documents can be pdf, txt , doc so
i want to add icon before the name of the document.
and i also want one more Image column before name column
My code so far.
*
private CellTable<FDocument> getDocumentTable() {
if (documentTable == null) {
documentTable = new CellTable<FDocument>();
documentTable.setSize("600px", "300px");
documentTable.addColumn(nameColumnD, "NAME");
documentTable.addColumn(sizeColumnD, "SIZE");
documentTable.addColumn(modified_by_ColumnD, "MODIFIED BY");
documentTable.addColumn(dateColumnD, "MODIFIED ON");
}
return documentTable;
}
TextColumn<FDocument> idColumnD = new TextColumn<FDocument>() {
#Override
public String getValue(FDocumentobject) {
// TODO Auto-generated method stub
return object.getId();
}
};
TextColumn<FDocument> nameColumnD = new TextColumn<FDocument>() {
#Override
public String getValue(FDocumentobject) {
return object.getName();
}
};
TextColumn<FDocument> sizeColumnD = new TextColumn<FDocument>() {
#Override
public String getValue(FDocumentobject) {
return object.getSize();
}
};
TextColumn<FDocument> modified_by_ColumnD = new TextColumn<FDocument>() {
#Override
public String getValue(FilenetDocument object) {
return object.getModifiedBy();
}
};
TextColumn<FDocument> dateColumnD = new TextColumn<FDocument>(){
#Override
public String getValue(FDocumentobject){
return object.getModifiedOn();
}
};
private void addValuesToTable(){
List<FDocument> FDC = null;
/*
* Adding data to folder Table
*/
ArrayList<FDocument> documentsArrayList = new ArrayList<FDocument>();
Iterator<String> iteratorDocument = documents.getDocuments().getDocumentCollection().keySet().iterator();
while(iteratorDocument.hasNext()){
String key = iteratorDocument.next().toString();
FDocument value = documents.getDocuments().getDocumentCollection().get(key);
documentsArrayList.add(new FDocument(value.getName(), value.getSize(),value.getModifiedBy(), value.getModifiedOn(),value.getId()));
}
FDC = documentsArrayList;
// Create a data provider.
ListDataProvider<FDocument> dataProvider1 = new ListDataProvider<FDocument>();
// Connect the table to the data provider.
dataProvider1.addDataDisplay(documentTable);
// Add the data to the data provider, which automatically pushes it to the widget.
List<FDocument> listDocument = dataProvider1.getList();
for (FDocument fDocument: FDC) {
listDocument.add(fDocument1);
}
This the way i have made my table and the value.
Plz any one tell me the procedure
to add icon before the name of the document.
and i also want one more Image column before name column
Use DefaultTableCellRenderer to set custom renderer against the column with the desired icon. As shown below, the renderer creates a new label to mask all the cells within the selected column.
private class CellRenderer extends DefaultTableCellRenderer
{
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column)
{
JLabel label = new JLabel((String)value);
label.setOpaque(true);
Icon icon = new ImageIcon("icon.png");
label.setIcon(icon);
return label;
}
}
And then apply it to your table as shown below:
table.getColumnModel().getColumn(0).setCellRenderer(new CellRenderer());
Edited:
If you're using GWT then see here for an example code for Grid Cell format.
For creating image column, You need to define a custom renderer that sets the icon on the label
import java.awt.Component;
import javax.swing.ImageIcon;
import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;
public class MyRenderer extends DefaultTableCellRenderer {
/*
* #see TableCellRenderer#getTableCellRendererComponent(JTable, Object, boolean, boolean, int, int)
*/
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus,
int row, int column) {
ImageIcon icon = new ImageIcon(getClass().getResource("images/moon.gif"));
setText((String)value);
setIcon(icon);
return this;
}
}
And then, use the renderer in a table as follows,
import java.awt.GridLayout;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableModel;
public class Sample {
public static void main(String[] args) {
JFrame f = new JFrame("Sample");
f.getContentPane().setLayout(new GridLayout(1,0,0,0));
TableModel model = new AbstractTableModel() {
public Object getValueAt(int rowIndex, int columnIndex) {
return rowIndex + "," + columnIndex;
}
public int getColumnCount() {
return 3;
}
public int getRowCount() {
return 10;
}
};
JTable table = new JTable(model);
table.getColumnModel().getColumn(1).setCellRenderer(new MyRenderer());
JScrollPane pane = new JScrollPane(table);
f.getContentPane().add(pane);
f.pack();
f.setVisible(true);
}
}
Both of these will probably need a custom TableCellRenderer...
Refer to the API docs here... http://docs.oracle.com/javase/1.4.2/docs/api/javax/swing/table/DefaultTableCellRenderer.html
You basically want to overwrite the getTableCellRendererComponent() method, and add in any additional code for rendering the image.
For example, to add an icon before the document, you would do the following...
public void MyTableCellRenderer extends DefaultTableCellRenderer {
public MyTableCellRenderer(){
super();
}
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column){
Component renderer = super.getTableCellRendererComponent(table,value,isSelected,hasFocus,row,column);
if (row == 0 && renderer instanceof JLabel){ // where row == 0 is the row you want to add the icon to
((JLabel)renderer).setIcon(new ImageIcon("image.png"));
}
return renderer;
}
}
You would need to set this as the TableCellRenderer for the column or table.
You would also do a similar thing for adding an image column to the table.

Categories