setbackground is not working for cell - java

I am trying to set the background color for a particular section in the row.
Cell myCell = new PCell(tablename);
myCell.setBorder(Rectangle.NO_BORDER);
myCell.setVerticalAlignment(Element.ALIGN_TOP);
if(mycondition) {
myCell.setFixedHeight(2f);
myCell.setBackgroundColor(colorFourteenDay);
}
mytable.addCell(myCell);
but the setBackgroundColor is applying color for the whole row, so can you help me how to set the color for a particular section of row.
myCell.setBorder(Rectangle.NO_BORDER);...... and after using this code section also the border is coming

Related

iText 7 borderless table (no border)

This code below does not work.
Table table = new Table(2);
table.setBorder(Border.NO_BORDER);
I am new to iText 7 and all I wanted is to have my table borderless.
Like how to do it?
The table itself is by default not responsible for borders in iText7, the cells are. You need to set every cell to be borderless if you want a borderless table (or set the outer cells to have no border on the edge if you still want inside borders).
Cell cell = new Cell();
cell.add("contents go here");
cell.setBorder(Border.NO_BORDER);
table.addCell(cell);
You could write a method which runs though all children of a Table and sets NO_BORDER.
private static void RemoveBorder(Table table)
{
for (IElement iElement : table.getChildren()) {
((Cell)iElement).setBorder(Border.NO_BORDER);
}
}
This gives you the advantage that you can still use
table.add("whatever");
table.add("whatever");
RemoveBorder(table);
instead of changing it on all cells manual.

How to get colour, font of java table row/cell/text with Jemmy?

Need to check colour/font style of java table (text, background) in swing application cause style of row should depend on a column value.
It is possible to get colour of font and background of selected (cell/row):
mainTable.selectCell(0, 0);
String bgcol = mainTable.getSelectionBackground().toString(); // => javax.swing.plaf.ColorUIResource[r=51,g=153,b=255]
String fgcol = mainTable.getSelectionForeground().toString(); // => javax.swing.plaf.ColorUIResource[r=255,g=255,b=255]
But selected cell/row has its own style of selection, so this check becomes quite useless.
What is the way to accomplish style checking of any cell/row (not just selected) with Jemmy library?
A renderer is used to paint each cell in the table. You should be able to access the component used to render the cell with code something like:
TableCellRenderer renderer = table.getCellRenderer(row, column);
Component c = table.prepareRenderer(renderer, row, column);
System.out.println(c.getBackground());

Description tooltips for Vaadin Grid header cells

I'd like to define descriptions for Grid header cells, similarly to how AbstractComponent.setDescription(String description) works (i.e. tooltip shown on mouse hover). As the Grid doesn't support this in itself, I tried adding a Label component into the header cell, and then use setDescription() on the label. I can get the info tooltip working like this, but the downside is that clicking on the label component doesn't trigger sorting. If I want to sort the column, I need to click the header cell on the really narrow area that's left between the right edge of the label component and the column border, where the sorting indicator will be shown. If you look at the screenshot below, the highlighted area is the label component, and in order to trigger sorting, the user needs to click on the space on the right side of the component.
Is there a better way to apply descriptions to header cells than the one I described? And if not, is there a way to make the sorting work properly when the header cell contains a Component?
Based on the answer from kukis, I managed to come up with a simpler solution that doesn't require any JavaScript. Instead of adding a Label component into the header cell, I'm adding a div element manually with StaticCell.setHtml(), and setting the title attribute on it:
#Override
protected void init(VaadinRequest request) {
Grid grid = new Grid();
grid.addColumn("to");
grid.addColumn("the");
grid.addColumn("moon");
Grid.HeaderRow headerRow = grid.getDefaultHeaderRow();
headerRow.getCell("to").setHtml("<div title='Hello world'>to</div>");
headerRow.getCell("the").setHtml("<div title='Hello world 2'>the</div>");
headerRow.getCell("moon").setHtml("<div title='Hello world 3'>moon</div>");
grid.addRow("1","2","3");
grid.addRow("d","v","w");
grid.addRow("g","s","h");
setContent(new VerticalLayout(grid));
}
Feature added to Vaadin 8.4.0
Feature added to Grid in Vaadin 8.4.0.
Ticket:
https://github.com/vaadin/framework/pull/10489
Release notes:
https://vaadin.com/download/release/8.4/8.4.0/release-notes.html
Grid headers and footers now support tooltips.
Well, since Grid doesn't support it by itself you can always use JavaScript to achieve desired behaviour. SSCCE:
private final String SCRIPT;
{
StringBuilder b = new StringBuilder();
b.append("var grid = document.getElementById('mygrid');\n");
b.append("var child = grid.getElementsByClassName('v-grid-tablewrapper')[0];\n");
b.append("child = child.firstChild.firstChild.firstChild;\n");
b.append("child.childNodes[0].title='Hello world';\n");
b.append("child.childNodes[1].title='Hello world 2';\n");
b.append("child.childNodes[2].title='Hello world 3';\n");
SCRIPT = b.toString();
}
#Override
protected void init(VaadinRequest request) {
final VerticalLayout layout = new VerticalLayout();
Grid grid = new Grid();
grid.addColumn("to");
grid.addColumn("the");
grid.addColumn("moon");
grid.addRow("1","2","3");
grid.addRow("d","v","w");
grid.addRow("g","s","h");
grid.setId("mygrid");
setContent(layout);
layout.addComponent(grid);
JavaScript.getCurrent().execute(SCRIPT);
}
Another possibility would be to develop your own Grid in GWT based on the Grid provided by Vaadin Team but it is a way higher cost approach.
Another solution would be to, as you have tried, put label in a column and propagate the label-clicked-event to the Grid.
I use my own utillity finction:
public static Grid setHeaderCellDescription(Grid grid, int rowIndex, String property, String description) {
Grid.HeaderCell cell;
String cellHeader = "<span title=\"%s\">%s</span>";
cell = grid.getHeaderRow(rowIndex).getCell(property);
cell.setHtml(String.format(cellHeader, description, cell.getText()));
return grid;
}
You may add some additional checks if need (existing of cell and row number).
Or other variant - instead setHtml use cetComponent.
Grid.HeaderCell cell = grid.getHeaderRow(rowIndex).getCell(property);
Label newLabel = new Label(cell.getText());
newLabel.setDescription(description);
cell.setComponent(newLabel);
Update for Vaadin 23: you can use your own Component as a column header with this method: com.vaadin.flow.component.grid.Grid.Column#setHeader(com.vaadin.flow.component.Component).
So you can use e.g. a Span with a title:
Span headerComponent = new Span();
headerComponent.setText("Your header text");
headerComponent.getElement().setProperty("title", "Your tooltip text");
column.setHeader(headerComponent);

How to keep custom colors in SWT cells?

I made a class that extends from OwnerDrawLabelProvider to customize the cell's content. Then I override the method paint, so only a column will be green.
paint(Event event, Object element) {
TableItem item = (TableItem) event.item;
Color color = SWTResourceManager.getColor(0, 204, 0);
item.setBackground(7, color);
}
but when the row is selected or mouse hovers over, the custom color is not showed.
Maybe it's important to say that cell is green only when it meets some validations, so it is important for my user. The problem is when they only get one result in table and then they select the only row and think that the result is "normal".

Checking if a row appears on screen before force scrolling to it?

I am using a Swing JTable, and i want to force scroll to a specific row inside it. That is simple using scrollRowToVisible(...), but i want first to check this row is not already visible on the screen before scrolling to it, as if it is already visible there is no need to force scroll.
How can i do that ?
The link below is to an article that determines if a cell is visible. You could use that - if the cell is visible, then the row is visible. (But of course, possibly not the entire row, if horizontal scrolling is also present.)
However, I think this will fail when the cell is wider than the viewport. To handle this case, you change the test to check if the top/bottom of the cell bounds is within the vertical extent of the viewport, but ignore the left/right part of the cell. It is simplest to set the left and width of the rectangle to 0. I've also changed the method to take just the row index (no need for column index) and it returns true if the table is not in a viewport, which seems to align better with your use-case.
public boolean isRowVisible(JTable table, int rowIndex)
{
if (!(table.getParent() instanceof JViewport)) {
return true;
}
JViewport viewport = (JViewport)table.getParent();
// This rectangle is relative to the table where the
// northwest corner of cell (0,0) is always (0,0)
Rectangle rect = table.getCellRect(rowIndex, 1, true);
// The location of the viewport relative to the table
Point pt = viewport.getViewPosition();
// Translate the cell location so that it is relative
// to the view, assuming the northwest corner of the
// view is (0,0)
rect.setLocation(rect.x-pt.x, rect.y-pt.y);
rect.setLeft(0);
rect.setWidth(1);
// Check if view completely contains the row
return new Rectangle(viewport.getExtentSize()).contains(rect);
}
Determining if a cell is visible in JTable

Categories