How to get Alternative row colours using a gridControl component - java

I'm currently working on redoing the GUI of a project which uses the component GridControl which is an extension of gridView, found here:
http://techpubs.borland.com/books/jbuilder/jbuilder2/jbuilder/reference/borland.jbcl.control.GridControl.html
I'm not allowed to change the components used so I have to find a way to do this with gridControl, however I'm relatively new at programming, so don't really know what I'm doing. I've been asked to get the alternative rows of the table to be grey. I can't find a specific function of the component which caters for this like JTabel seems to have so I was hoping that someone would be able to help. The only one which seems related is .setBackground which seems to only affect the full table.
Thanks very much in advance.

Check out THIS link. It has a mention of this function:
public Color getBackgroundColor(..):callback method automatically invoked by the grid to retrieve the background color for each grid's cell. A programmer can override this method to specify distinct colors for grid's cells.
If the method is not overridden, then rows are colored alternatively with two different colors: one color is defined through ClientSettings.GRID_CELL_BACKGROUND attribute and the other color is slightly different from the first one, defined through getDeltaColor() method.
now, the defaule colour values for ClientSettings.GRID_CELL_BACKGROUND is rgb (238,238,238) which is grey, and for deltaColor is rgb (235,235,235) also grey.
so, the cells should all be grey, unless there's been an override for the function public Color getBackgroundColor(..), something like this:
public Color getBackgroundColor(int row,String attributedName,Object value) {
return new Color(255,255,255); /*for all cells to be White*/
}
In order to make alternate rows grey (assuming the other color white) find the override of the above method and change its body to get your required colours, for eg:
public Color getBackgroundColor(int row,String attributedName,Object value) {
if (row%2==0)
return new Color(255,255,255); /*sets White background for even rows*/
else
return new Color(238,238,238); /*sets Grey background for odd rows*/
}
I'm relatively new at programming, so don't really know what I'm doing hmm.. there are a lot of help/examples available online. you need to learn to search intelligently and follow it up with some background theory to learn on your own.. and you'll survive (for now).

Related

Making a qustom EnumChatFormatting Color

Is there a way to make a custom color with EnumChatFormatting, with rgb or hsb? Because i got a gui where i can change the menu color with red(0-255), blue, green also. Now i got the problem to insert that values to EnumChatFormatting anyone got a solution?
It depends. So if you're making a forge mod, it's possible to set the color of your custom GUIs to whatever you want. The ChatFormatting enum is already used in minecrafts code, so you can't change that easily.

Swing: keeping reference to Color in components

How do I programmaticaly update the color of N JComponents (background and foreground), by changing a Color variable c ?
Example:
I have 10 JButton and 5 JCheckbox.
I have the variable:
Color mainColor = Color.RED;
I set all backgrounds and foregrounds to mainColor.
Now if I want all of them to be blue, mainColor = Color.BLUE will not work.
Normally I would do something like mainColor.setRgb(0,0,255), but for whatever reason, Color does not allow this.
So what can I do? Other workarounds are appreciated as well.
The components are spread out accross a lot of panels and frames and some appear dynamically so I'd rather not keep a list.
Also not all of them require both foreground and background to be painted.
You have to reset the color on all the JComponents. For simplicity, you could create a helper method that does it for you.
void changeMainColor(Color c){
this.mainColor = c;
for (JComponents jc : this.myComponentsList){
jc.setForeground(c);
jc.setBackground(c);
}
}
EDIT
If you do not want to keep a list of components manually, use the Observer pattern:
For even greater abstraction, use Publisher/Subscriber pattern. Everytime you change the main color you sill "publish" the fact. All the components that are registered as "subscribers" to that event, will then be able to use the information provided (the color) in the event to do what they want (change background etc..)
I would suggest reading about them in the gang of four book (Design Patterns: Elements of Reusable Object-Oriented Software), but some quick references are below:
https://sourcemaking.com/design_patterns/observer and
https://abdulapopoola.com/2013/03/12/design-patterns-pub-sub-explained/
Ugly style : subclass java.awt.Color, delegating to a real Color instance with a mutator for the delegate.

Libgdx display text/score as actor on stage

I would like to display multiple texts/scores on my stage. This tutorial suggested SpriteBatch, however, the issue is that these texts won’t fade out (not actor). Certain posts suggested use table, but the issue is that I can’t set texts' position on stage. Is there any other way to display texts as an actor on stage without using table?
Thanks in advance.
Zi
You can create a subclass of actor and draw text as you like in it's overridden draw method.
Just don't forget to set alpha before drawing using setColor method. The value of alpha will be available as a parameter for draw.
You then can fade it the way you want.
Good luck.

drawing layers using java graphics API

I'm doing a simulator project that tests several A* based algorithms and show how they work and their results.
The algorithms are all multi-agent and run on a grid map environment.
I used a JPanel for the grid which contains a two dimensional array of Cells where each Cell is a custom class that extends the Component class and use the paint method to draw the stuff i need inside each cell.
For the drawing inside the cell I use method such as Graphics.fillRect or Graphics.drawImage to fill each cell with a certain color or icon).
I'm using a special Icon for the start position and goal position of every agent on the grid.
My problem is that I want to be able to draw more than one item on the same cell.
For example I want to be able to show the path of one of the agents by painting the cells along the path in a special color and the path might go through a start position of a different agent, so I want to be able to fill the cell with the color and have an icon drawn on top.
In another example I want to be able to mix two colors using alpha blending.
If I use graphics.fillRect() with one color that has alpha and then use it again with a different color with alpha value it won't work since the last fillRect() will override the first call.
Is there a way I can achieve what I need using the same Cell Component I created or should I implement it differently?
Perhaps there is a better solution to this problem?
I would really appreciate any advice on this matter.
If you draw a rectangle with 50% alpha and then draw another one, the second one will override it instead of blending with it.
It depends on the mode. This convenient utility shows the result of blending different colors using the modes defined in AlphaComposite. The available source code may offer some insights for your project.
Addendum:
the stuff I was trying to composite was on the same Component.
The example cited does exactly this, as does this example. If AlphaComposite does not meet your requirements, you can always vary hue, saturation and/or value; this example composes a color table based on saturation.

Dynamically changing the tooltip of a draw2d figure

We are currently using Eclipse Draw2D/GEF for an information page describing a process in our application. This basically consists of a matrix of large squares, each containing a matrix of smaller squares. We originally had all the squares as GEF objects, but because of the large volume of them being shown, we found that this did not scale very well and the view took a very long time to open. We then changed it so that only the large squares are Figures and we then draw the smaller squares using the graphic in paintFigure.
The problem that we are running into is that we still want the tooltip to change depending on which small square you are hovering over. I tried to do this by adding a mouseMotionListener and setting the tooltip, through setTooltip, depending on where the mouse currently is. The problem is that once the tooltip is displayed, it does not change any more when setTooltip is called.
Does any one know of an alternative way of doing this? Is there a way of getting the viewpart's PopupHelper and using that? Any help would be appreciated.
Thanks
Hmnn.. interesting problem. Since you paint your own Grid within the Figure, I would think that you have two options.
Try posting SWT events to fool Eclipse. I'd try a focus lost followed by a focused gained, to trigger tooltip machinery, at which point you could get the coordinates and display the appropriate contents.
Don't use the Figure#getTooltip strategy at all. Just show your own composite.
To dynamically change the tooltip, you can hold an instance of the tooltip Figure in your parent Figure. In the constructor of the parent Figure, create a new tooltip Figure (e.g. a Label) and use setToolTip() method to set the tooltip Figure to parent Figure.
When data model is changed, the updated tooltip text/icon can be set to the tooltip Figure. Then you just call setToolTip(tooltipFigure) method again.
You can have a method like:
protected Label toolTipLabel;
protected void updateToolTip(String text, Image icon){
toolTipLabel.setText(text);
toolTipLabel.setIcon(icon);
setToolTip(toolTipLabel);
}
The updateToolTip() method can be invoked in parent Figure's conturctor to initialize the tooltip. And this method can be invoked each time after the data model is changed.
I encountered the same problem in my code and solved it with that method. In my code, I invoked the updateToolTip() in the parentFigure.paintFigure() method.

Categories

Resources