I have a table where each row represents a picture. In the column Path I store its absolute path. The string being kinda long, I would like that when I hover the mouse over the specific cell, a tooltip should pop-up next to the mouse containing the information from the cell.
Just use below code while creation of JTable object.
JTable auditTable = new JTable(){
//Implement table cell tool tips.
public String getToolTipText(MouseEvent e) {
String tip = null;
java.awt.Point p = e.getPoint();
int rowIndex = rowAtPoint(p);
int colIndex = columnAtPoint(p);
try {
tip = getValueAt(rowIndex, colIndex).toString();
} catch (RuntimeException e1) {
//catch null pointer exception if mouse is over an empty line
}
return tip;
}
};
I assume you didn't write a custom CellRenderer for the path but just use the DefaultTableCellRenderer. You should subclass the DefaultTableCellRenderer and set the tooltip in the getTableCellRendererComponent. Then set the renderer for the column.
class PathCellRenderer extends DefaultTableCellRenderer {
public Component getTableCellRendererComponent(
JTable table, Object value,
boolean isSelected, boolean hasFocus,
int row, int column) {
JLabel c = (JLabel)super.getTableCellRendererComponent( /* params from above (table, value, isSelected, hasFocus, row, column) */ );
// This...
String pathValue = <getYourPathValue>; // Could be value.toString()
c.setToolTipText(pathValue);
// ...OR this probably works in your case:
c.setToolTipText(c.getText());
return c;
}
}
...
pathColumn.setCellRenderer(new PathCellRenderer()); // If your path is of specific class (e.g. java.io.File) you could set the renderer for that type
...
Oracle JTable tutorial on tooltips
You say you store an absolute path in a cell. You are probably using a JLabel for setting absolute path string. Suppose you have a label in your cell, use html tags for expressing tooltip content:
JLabel label = new JLabel("Bla bla");
label.setToolTipText("<html><p>information about cell</p></html>");
setToolTipText() can be used for some other Swing components if you are using something other than JLabel.
Related
I have a JPanel like below
The data in the Transaction table (RIGHT) is generated from a MySQL resultset when the user selects a row in the Plot Table (LEFT). Code below:
tableModelTran.getDataVector().removeAllElements();
if (rs.isBeforeFirst()) {
while (rs.next()) {
java.util.Vector data = new java.util.Vector();
data.add(rs.getString(1));
data.add(rs.getString(2));
data.add(new java.text.SimpleDateFormat("dd/MM/yyyy").format(rs.getDate(3)));
data.add(String.valueOf(new java.text.SimpleDateFormat("MMMM yyyy").format(rs.getDate(3))).toUpperCase());
data.add(rs.getString(4));
data.add(rs.getString(5));
data.add(rs.getString(6));
data.add(rs.getBoolean(7));
tableModelTran.addRow(data);
}
performTotals();
}
I would like to change the colour of the rows to GREEN if the 'Verified' column is ticked and leave uncoloured if it is not. Also, if the user changes the value of the column, the colour should update accordingly. Any and all help is appreciated.
EDIT:
I created a class and added it to my constructor as below:
static class ColorRenderer extends javax.swing.table.DefaultTableCellRenderer {
#Override
public java.awt.Component getTableCellRendererComponent(javax.swing.JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
java.awt.Component c = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
if (value.equals(true)) {
c.setBackground(java.awt.Color.GREEN);
}
return c;
}
public PlotDetailsWindow() {
initComponents();
....
....
transactionTable.setDefaultRenderer(Boolean.class, new ColorRenderer());
....
....
}
Now whenever I click select a row from the table on the left, if the first value is false, it will not change the colour but if it is true it will change just the 'Verified' column to green and then it doesn't matter if the value is true or false it will always be green. Also, the check boxes turned to true/false now.
I would like to change the colour of the rows to GREEN if the 'Verified' column is ticked
Check out Table Row Rendering.
It shows how to override the prepareRenderer(...) method of JTable to color an entire row based on the data in the row.
The basic structure for overriding this method would be as follows:
JTable table = new JTable(...)
{
public Component prepareRenderer(
TableCellRenderer renderer, int row, int column)
{
Component c = super.prepareRenderer(renderer, row, column);
// add custom rendering here
return c;
}
};
My project involves a JTable and GUI. The user populates the JTable by adding "transactions" (represented as a row) and can then copy or delete these "transactions" at will.
I have decided that I want to add some more color to the table. In particular I would like to have every other header and column have a black background with white text so that it is easy to view.
I have been able to write the code to accomplish with the columns. I have also been able to change the color of ALL column headers. The problem that I seem to be having is changing individual column headers dynamically (like I do the columns). I have referred to the question here: JTable header background color but am very confused by it.. The solution posted is quite complicated and I am having trouble translating it to my individual code. Here is what I have so far:
tbl = new JTable();
String header[] = new String[]{
"Notification Reference Number", "Amount","Credit/Debit Indicator","Initiating Party Id",
"Initiating Party Scheme Name", "Debtor Name","Debtor Zip Code","Debtor Town",
"Debtor Country Sub-Division","Debtor Address","Debtor Agent Clearing System Id","Debtor Agent Name",
"Creditor Agent Clearing System Id",
"Creditor Agent Name",
"Return/Reject Code","Return/Reject Additional Info",
"Payment Instrument","Type of Payment","Pass-Thru Data","Clearing Account","Transaction Type",
"Return Occurence","Retired Indicator","Representment Method",
"Check-Split Indicator","Check Split Amount","Corporate Check Indicator","Source Batch Identifier"
};
dtm.setColumnIdentifiers(header);
tbl.setModel(dtm);
tbl.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
JScrollPane scroll = new JScrollPane(tbl);
scroll.getViewport().setBackground(Color.gray);
boolean flag = false;
for(int i = 0; i < header.length; i++){
tbl.getColumnModel().getColumn(i).setPreferredWidth(200);
if(flag==false){
TableColumn tm = tbl.getColumnModel().getColumn(i);
tm.setCellRenderer(new ColorColumnRenderer(Color.black,Color.white));
flag=true;
}else if(flag==true){
TableColumn tm = tbl.getColumnModel().getColumn(i);
tm.setCellRenderer(new ColorColumnRenderer(Color.white, Color.black));
flag=false;
}
}
As you can see I use the "flag" Boolean to ensure that every other column is changed. Here is the code for changing the columns:
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int row, int column) {
// TODO Auto-generated method stub
return null;
}
}
class ColorColumnRenderer extends DefaultTableCellRenderer{
Color bkgndColor, fgndColor;
public ColorColumnRenderer(Color bkgnd, Color foregnd){
super();
bkgndColor = bkgnd;
fgndColor = foregnd;
}
public Component getTableCellRendererComponent
(JTable table, Object value, boolean isSelected,
boolean hasFocus, int row, int column)
{
Component cell = super.getTableCellRendererComponent
(table, value, isSelected, hasFocus, row, column);
cell.setBackground(bkgndColor);
cell.setForeground(fgndColor);
return cell;
}
}
During experimenting I was able to set all headers with this:
tbl.getTableHeader().setOpaque(false);
tbl.getTableHeader().setBackground(Color.black);
tbl.getTableHeader().setForeground(Color.white);
I have tried numerous methods of coding a specific header but all are unsuccessful. I'm sure the solution is simple I just can't figure it out.
I should note that I apologize for not posting a full code example. This code is actually quite complex so I was hoping someone might be able to easily identify what I need to do before I have to create new JTable and try to isolate this example - Although I will do this if necessary.
i need to change my jtable appearance .
i have these data :
image image.
title String.
date String .
description String.
auteur String .
img , title , date ,description ,auteur.
my question is :
is that possible that i can show in every row these data as twitter feed appearance .
i want to show all these data in the same cell with a sample .
thanks every one .
You should use a JList (or a JTable, but with only one cell per row, a JList seems to be more appropriate) with a custom cell renderer.
Create your data class
public class MyData {
// image, title, date, description and author
}
Create your cell renderer
class MyCellRenderer extends JLabel implements ListCellRenderer<MyData> {
public Component getListCellRendererComponent(
JList<?> list, // the list
MyData value, // value to display
int index, // cell index
boolean isSelected, // is the cell selected
boolean cellHasFocus) // does the cell have focus
{
// tune your component in the way you want, for example
this.setText(value.getTitle());
// return the component to draw for this cell
return this;
}
}
Of course, your cell renderer can extends another component like JPanel.
Finally, instantiate a JList and set your custom renderer
JList<MyData> list = new JList<>();
list.setCellRenderer(new MyCellRenderer());
So i want to add a empty JLabel with a Background color to my JTable.
It's for a piechart and i want to add this so the legend matches.
code:
for (String result : queryResult) {
JLabel label = new JLabel("Hallo: "+rowCount);
label.setBackground(colors[rowCount]);
label.setOpaque(true);
String queryResultString = "";
queryResultString = result.toString();
String[] lineArray = queryResultString.split("////");
String[] pieData = new String[3];
pieData[0] = lineArray[0];
pieData[1] = lineArray[rangId - 1];
int value = Integer.parseInt(pieData[1]);
double percentage = value / total * 100;
pieData[2] = "" + percentage + "%";
pieModel.addRow(new Object[] {label, pieData[0], pieData[1], pieData[2]});
rowCount++;
}
pieTable.setDefaultRenderer(String.class, new DefaultTableCellRenderer() {
#Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column){
if(value instanceof JLabel){
//This time return only the JLabel without icon
return (JLabel)value;
}else{
return super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
}
}
});
What am i doing wrong? :o
When i look at the results in the first column of the table it says:
javax.swing.JLabel[,0,0,0......
can't see the rest but it definetly is not working as it should! :(
So i want to add a empty JLabel with a Background color to my JTable. It's for a piechart and i want to add this so the legend matches.
you can't do that Renderer by default returs Component, JComponent or JLabel
Every cell in JTable is already a JLabel. You need to customize getTableCellRendererComponent to return super.getTableCellRendererComponent with proper background color set to achieve the effect you are aiming for.
DefaultTableCellRenderer inherit from JLabel (that inherit from JComponent). So you can change the JLabel properties within getTableCellRendererComponent.
For example :
ImageIcon icon = new ImageIcon(getClass().getResource("images/moon.gif"));// prepared before
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus,
int row, int column) {
setText((String)value);
setIcon(icon);
return this;
}
setText come from the super JLabel class and setIcon from the super JComponent class.
Almost all DefaultTableCellRenderer methods override method from these classes.
I have a JTable and i want a cell (or its row) painted in red when the value entered is higher than a certain value. I'm checking that into a TableModelListener to detect TableChange, so I see no way of colouring the table at the renderer (yet I'm sure it is possible, only it is unknown for me).
I also saw this question but i don't know how to use it.
that job for prepareRendered as you can see here
Following is for single table cell you can extend it for row:
First take table column you want to pint and then add a TableCellRenderer to it as follows:
TableColumnModel columnModel = myTable.getColumnModel();
TableColumn column = columnModel.getColumn(5); // Give column index here
column.setCellRenderer(new MyTableCellRenderer());
Make MyTableCellRendere class which implements TableCellRenderer and extends JLabel(so that we can give a background color to it). It will look something like following:
public class MyTableCellRenderer extends JLabel implements TableCellRenderer {
public Component getTableCellRendererComponent(JTable table,
Object value, boolean isSelected, boolean hasFocus, int row,
int col) {
JLabel jLabel = (JLabel) value;
setBackground(jLabel.getBackground());
setForeground(UIConstants.black);
setText(jLabel.getText());
return this;
}
}
Now in method where you are listening table cell value change do something like follow:
JLabel label = new JLabel(changedValue);
// check for some condition
label.setBackground(Color.red); // set color based on some condition
myTable.setValueAt(label, 0, 5); // here 0 is rowNumber and 5 is colIndex that should be same used to get tableColumn before.