Setting Row Color of the JTable
Hi,
I wanted to set the first row and first column of the JTable .
Can anybody help me in this? I read CellRendering but didn't understood when getTableCellRendererComponent(...) method get called and how it works.
I realy appreciate if anybody explains me.
Thanks,
Padmashree
Hi,
If my understading is correct, You are trying to set the color of the first column of the first row.
"Renderer" returns the component used for drawing the cell. It applies for all the cell. Method called
"GetTableCellRendererComponent" is used to configure the renderer appropriately before drawing.
So the following parameters are provided to acheive it.
table - the JTable that is asking the renderer to draw; can be null
value - the value of the cell to be rendered.
isSelected - true if the cell is to be rendered with the selection highlighted; otherwise false
hasFocus - if true, render cell appropriately.
row - the row index of the cell being drawn.
column - the column index of the cell being drawn
So to acheive your requrement, you have to do something like this.
1 - apply the required color to first cell of the first row
2 - configure in such a way that it works when the renderer reads first row and first column.
For. e.g To set the background color as Blue,
if(row == 0) && ( column == 0){
this.setBackground(Color.Blue); }else {
this.setBackground(defaultColor);
Hope it gives a good picture.
Similar Messages
-
How to create a Listbox in UI5 with alternate row color for the Listitems.
Hi,
I need to create a Listbox for browser (not mobile). But not getting any properties for alternate row color for the list items.
If anyone worked on this pls share.
Thanks,
NigamHi Guys
Chandra's solution is sweet.
Here is another one so that it will not affect the other lists (if any) that you do not want alternate colors.
-D -
I am trying to display some rows in a JTable in a different color regardless of selection status. The row color criteria would be from data from the object in the row, i.e., given a certain status of a record, I would want to show it as red instead of black.
A few optimizations:
class StatusColorRenderer
extends DefaultTableCellRenderer
public StatusColorRenderer()
// super(); not needed, call by default
setOpaque(true);
// DefaultTableCellRenderer returns itself as a CellRenderer Component, it extends JLabel so you can call setOpaque on it
// since this method is called at every paint for every cell it should be HIGHLY optimized
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);
MyObjectClass myObject = ( MyObjectClass ) myTableModel.getMyVector().elementAt(row);
<SomeClass> myStatus = myObject.getTheStatus();
if ( myStatus.compareTo(Status.BAD_STAT) == 0)
c.setForeground(Color.red);
else if (myStatus.compareTo(Status.IN_PROGRESS) == 0)
c.setForeground(Color.green);
else if ( myObject.getOtherFactor().compareTo(OtherFactors.NOT_DEFAULT) == 0)
c.setForeground(Color.blue);
else
c.setForeground(Color.black);
// c.repaint(); NEVER DO THAT
return c;
StatusColorRenderer myRenderer = new StatusColorRenderer();
// only one instance of the renderer
for ( int i = myTableModel.getColumnCount() - 1 ; i >= 0 ; i-- )
//column = myTable.getColumnModel().getColumn( i );
myTable.setDefaultRenderer( myTable.getColumnClass( i ),
myRenderer );
}Comment:
AbstractTableModel and DefaultTableModel.getColumnClass method returns Object.class, it is used by the JTable to setup the editor and the renderer of a column
so if you're using the DefaultTableModel and JTable without overriden getColumnClass, you can set a generic renderer for your JTable by using:
myTable.setDefaultRenderer( Object.class, myRenderer ); -
How to chang the row's color in the Jtable?
I have a table and many rows,I want chang the rows color,like row one's color is red,row tow's color is write,row three's color is green.How can do it? And if i want chang row 3,coloum 2's color.How can do it?
thanks.hi,
I use jdbtable to view data,I want them to chang row color when some cells equal the value.But interface getTableCellRendererComponent not run.I don't know why!
jdbtable is extend jtable ,it made in borland jbuilder.
//1 class
package errorreport;
import java.awt.Component;
import java.awt.Color;
import javax.swing.table.DefaultTableCellRenderer;
import com.borland.dbswing.*;
public class rowcolor extends DefaultTableCellRenderer {
public Component getTableCellRendererComponent
(JdbTable table, Object value, boolean isSelected,
boolean hasFocus, int row, int column)
Component cell = super.getTableCellRendererComponent
(table, value, isSelected, hasFocus, row, column);
int iColcount = 0;
iColcount = table.getColumnCount();
if( value instanceof String )
String strAlmlev = (String)value;
if(strAlmlev.trim().equals("C1"))
cell.setBackground( Color.red );
// You can also customize the Font and Foreground this way
// cell.setForeground();
// cell.setFont();
else
if(strAlmlev.trim().equals("C2"))
cell.setBackground( Color.orange );
else
if(strAlmlev.trim().equals("C3"))
cell.setBackground( Color.yellow );
else
if(strAlmlev.trim().equals("ERR"))
cell.setBackground( Color.green );
return cell;
//2 class
JdbTable jdbTable2 = new JdbTable();
TableCellRenderer renderer = new rowcolor();
try
jdbTable2.setDefaultRenderer( Class.forName( "java.lang.String" ), renderer );
catch( ClassNotFoundException ex )
System.exit( 0 );
thanks -
Giveing the color to the JTable Heder
i have application with multiple Jtable
I want give the color to the Heder of the JTable
shrinathHey,
for a header of a particular column u can set using the following,
jTable1.getColumnModel().getColumn(1).setHeaderRenderer( new TableCellRenderer() {
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
JButton jb = new JButton();
jb.setText((value == null) ? "B" : value.toString());
jb.setMargin(new Insets(2, 14, 2, 14));
jb.setBackground(Color.CYAN);
return jb;
});For setting for the entire the tableheaders u can set it as,
jTable1.getTableHeader().setBackground(Color.ORANGE);Hope this helps u...
Regards,
Ciya. -
Set font color for the JTextField
I'm using JTextField in my project. However, can anybody teach me how to set the font color for the font in the JTextField to other color. The default font color is black. Any idea about this?
JTextField field = new JTextField(20);
field.setForeground(Color.blue);
Good luck ;) -
How to make the row header of the JTable respond to mouse events?
Is there an easy way to enable the row header of a JTable so it listens to mouse events? I have put a button in the first cell of the row header but I can't click in it.
I'm asking for an easy way because I've seen some fairly complicated examples on the web that seem close to what I want but I was hoping something simple like getRowHeader().setEnabled(true) would do the trick for my case...What's your row header, another JTable or something else? Check out camickr's [url http://tips4java.wordpress.com/2009/07/12/table-button-column/]Table Button Column.
db
edit Or to get better help sooner, post a [url http://mindprod.com/jgloss/sscce.html]SSCCE (Short, Self Contained, Compilable and Executable) example that demonstrates the problem.
Edited by: Darryl Burke -
Setting series color in the Business Graphics UI element
Hi all,
I'm using the Business Graphics UI element in my Web Dynpro application.
I set the color for various properties using chart designer, then I add the generated XML in the doModifyView method: bg.setDirectCustomizing(xml file) and most of it works.
However, when I change the series' color, I don't see any change in the xml file (see snapshot).
http://img90.imageshack.us/my.php?image=graphjy1.jpg
My xml starts with <SAPChartCustomizing> and ends with </SAPChartCustomizing>.
How can I set the series' color using the custom xml file?
Thanks,
OmriHi,
as the connection between your data and the customizing settings works via IDs you have to make sure that you set exactly the same ID for a data series as defined in the chart designer.
In case that you use point customizing make sure that you do not set an ID for the points of your series. Instead set the series customizing ID for the series only! Keep in mind that point customizing always overwrites series customizing if both are defined.
Regards, Kai -
Setting row color in REUSE_ALV_GRID_DISPLAY_LVC
Hi,
I'm not sure if this function perhaps is not released or has bugs? I didn't see any OSS notes. I've read several sources on how to set the color for a given line and I'm not sure why I don't see it. I see the values in my internal table in the ROWCOLOR column.
We are on 4.6C and I wanted to use the LVC function to move toward the ECC versions of the structures ( LVCS_LAYO instead of SLIS_S_LAYOUT). Perhaps this function module is not really supported???
Could the ZEBRA parameter throw it off?
Could it be a buffer issue? I've tried it alternate ways and don't see a difference.
Thanks for any suggestions,
Ray
Here is the code I'm using to set the layout and define the table.:
gs_alv-callback_pf_status = 'PF_STATUS_SET'.
gs_alv-callback_user_command = 'USER_COMMAND'.
gs_alv-callback_program = sy-repid.
gs_alv-structure_name = 'ZHUS_ST_SP_REPORT'.
gs_alv-itabname = 'GT_DATA'.
gs_alv-itabwaname = 'GS_DATA'.
gs_layo-zebra = 'X'.
gs_layo-cwidth_opt = 'X'.
gs_layo-box_fname = 'SELECTED'. " Checkbox
gs_layo-info_fname = 'ROWCOLOR'.
gs_layo-sel_mode = 'B'. "Default - multiple rows
CALL FUNCTION 'LVC_FIELDCATALOG_MERGE'
EXPORTING
i_structure_name = gs_alv-structure_name
CHANGING
ct_fieldcat = gt_fcat.
I set the color in my list table:
ls_data-rowcolor = 'C600'.
APPEND ls_data TO gt_data.
This is how I call the grid:
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY_LVC'
EXPORTING
i_buffer_active = 'X'
i_callback_program = gs_alv-callback_program
i_callback_pf_status_set = gs_alv-callback_pf_status
i_callback_user_command = gs_alv-callback_user_command
i_structure_name = gs_alv-structure_name
i_grid_title = gs_alv-grid_title
i_grid_settings = gs_glay
is_layout_lvc = gs_layo
it_fieldcat_lvc = gt_fcat
it_excluding = gt_excluding
i_default = gs_alv-default
i_save = gs_alv-save
is_variant = gs_disvariant
it_events = gt_event
it_event_exit = gt_event_exit
is_print_lvc = gs_prnt
TABLES
t_outtab = <gfs_t>
Edited by: Raymond Mannion on Jun 30, 2009 8:09 PM
Edited by: Raymond Mannion on Jun 30, 2009 8:10 PMTHis should help you:
Re: 'Reuse_alv_grid_display' : making a specific row bold
[ALV] Text in color and bold -
How to set row height of the row in the interactive report?
Some of the values in the columns of my interactive report are wrapped up changing the height of the row to 2 or 3 characters.
Is there any way to change it? I would like to have all rows set to 2 characters height.
RobertAdd the following style sheet to the page HTML Header.
<style>
table.apexir_WORKSHEET_DATA td {
height: 2.8em;
</style> -
Set background color to selected jtable columns cell
I am clicking on jtables column.I want to select any column and that column (only selected cell of column)should get displayed with specific background color.
plz helpYou can write your own tablemodel,
i.e. use DefaultTableModel or implement AbstractTableModel.
Good luck! -
FF31: How can I set a color for the empty page opened by new tab ?
Currently my new tabs open an empty, white colored page.
Is it possible to give this page a color of my choosing, say sky blue ?
If so, how ?Try:
*Customize about:newtab: https://addons.mozilla.org/firefox/addon/customize-aboutnewtab/ -
Is there a Terminal command to set alternate row colors in Mail?
I'd like to enable the alternate row colors in the message list, but the only way I can find to do it requires Developer tools to be installed.
Not bad for my Mac, but it seems a bit of a workload for the other 19 Macs in the office. There's nothing in Onyx or Secrets that'll do this.
Here's what I found on the web: http://pjkh.com/articles/2007/10/08/alternating-row-colors-in-apple-mail
There's a plug-in called WideMail which I'm using but it doesn't seem 100% stable.
ThanksJust do the hack once and copy that version of the Mail.app application (or just the MessageViewerContents.nib file) to the other machines. Just make sure that they are running exactly the same version of Mail and MacOS X. This hack will probably not work in Snow Leopard, by the way.
-
i would like to hide, show row/s in the JTable. I try it with setRowHeight but it affect all rows.
is it possible to do it ? what shoud i rewrite(add) to create this functionality?
i use JDK1.2.2 :-(
thanks TomasThe other way is to modify your table model class:
Let the getValueAt method return only the rows you need and the getRowsCount method return the needed row count.
It may be done in respect to some condition of the table
Best regards,
Martin -
Can you change the color of the interface rows in history in PS?
Hello all, . . .
I recently installed CS5.5 Standard and am giving the new app a run through. (PS right now) . . . I just upgraded from CS2 so it's a fairly big jump and a great one. I love pretty much all the new features. One thing I am wanting to change are the interface row colors in the history box to a color back to white or lighter? - I am having a hard time seeing which row is highlighted. The grays are very close in value. . . . . any ideas? or can I do this? . . . just curious.
Thanks!!!! : )
RickThanks PEC - I was thinking that might be the case. . . . it's just the gray is nice, but not so easy to see the highlighted row/rows you are working on from each other.
Maybe you are looking for
-
Preventing certain file types from being displayed in a directory listing
Someone before me used this code to create simple directory listings for our users to maintain certain sections of the site... http://www.asp101.com/samples/viewasp.asp?file=dir_list.asp My question is, can some additional code be added or change to
-
PL/SQL Portlet with edit defaults. HOW ???
Hi ! I created a pl/sql portlet that renders html. Now I want a customization option on the "Edit Defaults" page of the portlet. How do I do this ?? In the code of the portlet I set "l_portlet.has_show_edit_defaults" to true. So the Edit Defaults pag
-
How to upgrade from 10.4.11 to a OS.5?
I read the previously posted question re info on same topic but it did not answer my question. What exactly must I do to upgrade as stated in my question? I received something on desktop named mackeeper pkg but I cannot open the download because my s
-
I want to buy a retina display mac, Help!
Hi, I am student in my 11th grade, I wanna purchase a new macbook pro retina, i sold my old one for 1200. Coming to the point i need to decide upon the storage: 128 or 256GB. Please help, I primarily want to use it for school, some games and movies.
-
OIM: How does USR_POLICY_UPDATE flag get set?
We're finding that some of our users are getting the USR_POLICY_UPDATE flag set to "1", which is causing access policy evaluation when the user is updated. In some cases we do not want this evaluation to occur. My question is: What task or process is