Options instead of JTable - java

I want to color each rows of table,
by using particular values of database
spcification of mine database is some what like
**
id name color
1 pavan red
2 xyz white
**
i can give a color to complete table using
table.setBackground(new color(158,145,134);
please provide me some solution or hint to approch towards answer,
Thanks in advance.

This isn't hard at all with a JTable! In fact, it's incredibly easy!
See my answer here: Highlight a cell in JTable via custom table model
Reproduced for ease:
...Subclass JTable and override JTable.preparedRenderer(TableCellRenderer renderer, int row, int column). If the row and column numbers are the same, you can change the background color of the Component returned as the display (usually a JLabel);
Here's an example that highlights the row the mouse is over:
#Override
public Component prepareRenderer(final TableCellRenderer renderer, final int row, final int column) {
final Component c = super.prepareRenderer(renderer, row, column);
if (row == this.itsRow) {
c.setBackground(Color.RED);
}
return c;
}
where this.itsRow is an int field updated by a MouseMotionListener:
this.addMouseMotionListener(new MouseMotionListener() {
public void mouseMoved(MouseEvent e) {
SubclassedJTable.this.itsRow = SubclassedJTable.this.rowAtPoint(e.getPoint());
SubclassedJTable.this.repaint();
}
public void mouseDragged(MouseEvent e) {/***/}
});

Related

Set Background Color of a clicked table cell

I'm new to Java and I want to change the background color of a Specific cell, the one I clicked on, of a JTable.
I know that I have to use a MouseListener which I already did, also, the mousePressed. But at this point I am pretty lost.
EDIT: Forgot to add that the table is disabled, so you can't select a cell.
Can anyone help me? Thanks!
You must create a custom TableCellRenderer and pass it to the table
like this
public class ColorRenderer extends DefaultTableCellRenderer {
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int col) {
// get the DefaultCellRenderer to give you the basic component
Component c = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, col);
// apply your rules
if(table.isRowSelected(row) && table.isColumnSelected(col))
c.setBackground(Color.GREEN);
else{
c.setBackground(table.getBackground());
}
return c;
}
}
in this class we check if the given cell if the selected cell (which is pretty much what happens when we click it) and paint it differently (in my case I paint it green) , else we paint with the default color or any color you like.
don't forget to set the custom renderer you just created
table.setDefaultRenderer(Object.class, new ColorRenderer());
Edit 1
you must get the row and col of the clicked cell.
create 2 int variables that will hold the position
private int clickedRow=-1,clickedCol=-1;
add a mouse listener that updates the position variables
table.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent event) {
clickedRow= table.rowAtPoint(event.getPoint());
clickedCol= table.columnAtPoint(event.getPoint());
}
});
after that you change the renderer so it paints only the clicked cell with the special color
if( clickedRow == row && clickedCol == col){
c.setBackground(Color.GREEN);
}

JTable - how do i insert a new JLabel for each row in a specific column?

im been looking around on the internet for about 5 hours now to debug on this issue ive been having. and Basically i haven't been able to find anywhere where a person tries to add a new JLabel for each row in a specific column.
functionality explanation: i get a file url, i take the postFix which is usually xlsx or
doc - this postfix i want to display in a JLabel in coalition with a Excel or Doc
Icon -.- but what my current code does is just paint the same JLabel over and over again because it only sets the CellRenderer one time and uses it on all the rows dispite the fact that im setting it in a for-loop for each iteration - all my code is correct for this functionality up until the renderer only gets called once.
so my question goes as follows - how do i add a new JLabel for each row
in a Column ? -
my code follows.
my TableCellRenderer:
public class JLabelRenderer extends DefaultTableCellRenderer {
private static final long serialVersionUID = -166379583761969293L;
// private String fileExtension;
// private JLabel label;
private LogEntry log;
private JLabel label;
public JLabelRenderer(LogEntry log) {
label = new JLabel();
System.out.println("makeing a new JLabelRenderer");
this.log = log;
}
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int row, int column) {
label = (JLabel) super.getTableCellRendererComponent(table, value,
isSelected, hasFocus, row, column);
System.out.println(log.getFileExtension());
if (log.getFileExtension().equalsIgnoreCase("xlsx")) {
label.setIcon((ImageIcon) Pictures.getXlsx());
label.setText(log.getFileExtension());
} else if (log.getFileExtension().equalsIgnoreCase("doc")) {
label.setIcon((ImageIcon) Pictures.getDoc());
label.setText(log.getFileExtension());
} else if (log.getFileExtension().equalsIgnoreCase("docx")) {
label.setIcon((ImageIcon) Pictures.getDoc());
label.setText(log.getFileExtension());
} else if (log.getFileExtension().equalsIgnoreCase("pdf")) {
label.setIcon((ImageIcon) Pictures.getPdf());
label.setText(log.getFileExtension());
}
value = label;
return label;
}
#Override
public void setHorizontalAlignment(int alignment) {
super.setHorizontalAlignment(alignment);
}
public void setLog(LogEntry log) {
this.log = log;
}
}
where i make my model:
(i have alot more code adding actionListeners to right-click functionality and what not
but thats hardly relevant)
public void makeLogModel()
{
logModel = new DefaultTableModel();
//addCellEditorListener(this);
logModel.setColumnIdentifiers(new String[]{"Lavet Dato", "Lavet Af", "Beskrivelse", "Sidst Redigeret Dato", "Sidst Redigeret Af", "Fil Type"});
setAutoCreateRowSorter(true);//allows to sort through the information.
setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
setModel(logModel);
}
Where the magic was suppose to happen(update the log table after a search on logs)
public void updateLogTable(ArrayList<LogEntry> entryList)
{
logModel.setRowCount(entryList.size());
for(int i = 0; i < logModel.getRowCount(); i++)
{
setRowHeight(i, 30);
}
int row = 0;
for(LogEntry log : entryList)
{
logModel.setValueAt(log.getCreateDate(), row, 0);
logModel.setValueAt(log.getMadeBy(), row, 1);
logModel.setValueAt(log.getDescription(), row, 2);
logModel.setValueAt(log.getLastEdited(), row, 3);
logModel.setValueAt(log.getLastEditedBy(), row, 4);
labelRenderer = new JLabelRenderer(log);
getColumn("Fil Type").setCellRenderer(labelRenderer);
logModel.setValueAt(new JLabel(), logRow, 5);
row++;
}
}
i have read the Java documentation for components and Editors- but none of the
code examples are for a JLabel - probably because CellRenderer extends JLabel..
i have allso sniffed up the info that the 'value' parameter in the getTableCellRendererComponent() method is the one that is supposed to be set dynamically somehow... any surgestions would be greatly apriciatet, feel free to ask any questions.
thanks
logModel.setValueAt(new JLabel(), logRow, 5);
don't put JLabel, any JComponent to the model, XxxTableModel is designated to nest value for Renderer or Editor only, more in Oracle tutorial How to use Tables - Creating a Table Model
Renderer or Editor visually represents real JComponents, more in Oracle tutorial How to use Tables - Concepts: Editors and Renderers
label.setIcon((ImageIcon) Pictures.getXlsx());
Icon should be placed into local variables or array, list, whatever of Icons, don't load any FileIO from Renderer, renderer can be called many times per one second, e.g. from all mouse, keys and methods inplemented in APIs
Seems your problem in DefaultTableCellRenderer implementation.
You create renderer like JLabelRenderer(LogEntry log) because of, for all cells you have one instance of LogEntry, and log.getFileExtension() in getTableCellRendererComponent() returns same result for all rows.
In case of TableCellRenderer you need to use value parametr from getTableCellRendererComponent() method to determine extension and LogEntry instance.
Examine Concepts: Editors and Renderers.
Here is simple example for you, I use color instead of icons:
import java.awt.Color;
import java.awt.Component;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableCellRenderer;
public class TestFrame extends JFrame{
public TestFrame(){
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
init();
pack();
setVisible(true);
}
private void init() {
JTable table = new JTable(new Object[][]{
{1,"doc"},
{2,"xlsx"},
{3,"abc"}
},new Object[]{"nmb","extension"});
table.getColumnModel().getColumn(1).setCellRenderer(getRenderer());
add(new JScrollPane(table));
}
private TableCellRenderer getRenderer() {
return new DefaultTableCellRenderer(){
#Override
public Component getTableCellRendererComponent(JTable table,
Object value, boolean isSelected, boolean hasFocus,
int row, int column) {
Component tableCellRendererComponent = super.getTableCellRendererComponent(table, value, isSelected, hasFocus,row, column);
if("doc".equals(value)){
tableCellRendererComponent.setBackground(Color.YELLOW);
} else if("xlsx".equals(value)){
tableCellRendererComponent.setBackground(Color.GREEN);
} else {
tableCellRendererComponent.setBackground(isSelected ? table.getSelectionBackground() : table.getBackground());
}
return tableCellRendererComponent;
}
};
}
public static void main(String... strings) {
new TestFrame();
}
}
Also don't put Component's to TableModel like here logModel.setValueAt(new JLabel(), logRow, 5); just value.

JTable Custom TableCellRenderer displaying images

I have a JTable which I would like to display an image depending on the content of a cell, I understand in order to accomplish this I have to implement my own custom cell renderer, which I have already, however, as soon as the first image is drawn on the cell the programme draws the image on other cells regardless of their content. I have tried pretty much everything and have also scoured the internet for a solution, all with no avail. Here is my code:
public class GameBoard extends JTable
{
public GameBoard()
{
super(new GameBoardModel());
setFocusable(false);
setCellSelectionEnabled(true);
setRowHeight(26);
TableColumn column = null;
for (int i = 0; i < getColumnCount(); i++)
{
column = getColumnModel().getColumn(i);
column.setPreferredWidth(26);
}
setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
setDefaultRenderer(Object.class, new CellRenderer());
}
private class CellRenderer extends DefaultTableCellRenderer
{
private CellRenderer()
{
setHorizontalAlignment(JLabel.CENTER);
}
#Override
public Component getTableCellRendererComponent(JTable table,
Object value, boolean isSelected, boolean hasFocus, int row,
int column)
{
if (value.toString().equals("X"))
{
URL test = getClass().getResource(resources/icon.png");
setIcon(new ImageIcon(test));
}
else
setText(value.toString());
return this;
}
}
Forgive me if I'm doing something silly somewhere along those lines. . .
Thanks in advance,
Zig.
Don't forget to invoke:
super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
at the start of the method so you get the default settings of the renderer.
If the above doesn't fix the problem then your code may need to be something like:
if (value.toString().equals("X"))
{
URL test = getClass().getResource(resources/icon.png");
setIcon(new ImageIcon(test));
setText("");
}
else
{
setIcon(null);
setText(value.toString());
}
Also, you should never read the image in the renderer. The render gets called multiple times so you don't want to read the image every time. Read the image in the constructor of the class.

How can I change background of several cells with a click?

I have a table which every cell keep a string. Actually I used the table as a page of book and it contains text. My problem is that I want to click on a cell and all similar words' background color in the table change to one unique color. For instance, when I click on the cell which contains 'and', all 'and' in my table become highlighted. I implemented defaulttablecellrenderer and I know that when java wants to draw table recall it for every cell. I tried to use intrinsic repetition capability and set the color but it does not work in the way I expected. These are my codes:
JTable t=new Jtabale();
//Filling my table....here....
t.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e){
int column=((JTable)e.getSource()).getSelectedColumn();
int row=((JTable)e.getSource()).getSelectedRow();
JTable table=(JTable)e.getComponent();
Object myS=table.getValueAt(row, column);//value of that cell saved
CustomCellRenderer r=(CustomCellRenderer)table.getCellRenderer(row, column);
r.setCell(myS);
table.repaint();
}
});
and this is my DefaultTableCellRenderer implementation:
public class CustomCellRenderer extends DefaultTableCellRenderer {
Object myStr;
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);
if(myStr==value){
c.setBackground(Color.YELLOW);
}
else
c.setBackground(table.getBackground());
return c;
}
public void setCell(Object val){
myStr=val;
}
I do not know what's wrong with this code? This is only highlight that cell which I click. But I expected it change the background of several cells together! Even I put println inside the if but even it goes once in the if brace! I got confused. What is your idea?!

How to set the background color of target cells in a jtable?

I don't want to set the background color for all the cells in the jtable, just the ones I choose. How do i go about doing this?
you need to use a custom renderer
You might find the concept presented in Table Row Renderering easier to implement. Maybe keep a Set of Points (representing a cell you want to color). Or maybe even a Map of Points and Colors.
final JTable table = new JTable(tableModel);
table.getTableHeader().addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent mouseEvent) {
int selectedHeader = table.convertColumnIndexToModel(table
.columnAtPoint(mouseEvent.getPoint()));
table.getColumn(table.getColumnName(selectedHeader))
.setCellRenderer(new DefaultTableCellRenderer() {
public void setBackground(Color c) {
super.setBackground(Color.blue);
}
});
};
});
You'll need to create a custom CellRenderer and call it in your custom JTable's getCellRenderer(int col, int rol) method.
See http://self-reference.com/tech/swing.html for a good example.

Categories