Set Background color of columns in JTable
Hi,
I would like to set the background color of columns, which are not editable, to a grey color.
I have a JTable with 8 columns where the first is not editable (set through a TableModel.
Regards
Thomas
one can use a most efficient method of changing looks of table data by overidding prepareRenderer() method of JTable. this method returns a component which JTable uses to display a cell. a solution could be
public class mytable extends JTable
public component prepareRenderer(.......)
component c = super.prepareRenderer(....);
if ( not_my_column )
return c;
c.setBackground(Color.grey);
return c;
}
Similar Messages
-
How to set background color in row of JTable ?
i am new in java please tell me about How to set background color in row of JTable ? please example code. Thnak you.
Here is an example: http://www.javaworld.com/javaworld/javaqa/2001-09/03-qa-0928-jtable.html
For more info on how to use tables read the swing tutorial: http://java.sun.com/docs/books/tutorial/uiswing/components/table.html
ICE -
How to set background color of row in JTable
Hi,I want to set different background color to rows in JTable according to some value in the this row.
eg.
no name isGood
1 aaa yes (this row's background color is red)
2 bbb no (this row's background color is blue)
3 ccc yes (this row's background color is red)
4 ddd yes (this row's background color is red)
5 eee no (this row's background color is blue)
thanksthanks!*_*
-
How do I set background color of cell using JTable?
Hello all!
I have a question on the use of JTables. I am using a JDialog consisting of a JTable and a number of buttons to insert and to select OK or cancel. Everything is working perfectly, but I now must add editing capabilities to prevent invalid data being used in a SQL query to update or insert rows.
So far I can't figure out how to get at a cell to set the background to red if the user inputs a Date incorrectly. Upon setValueAt(), I take this value and check for standard format errors. I hope to someday convert the column altogether to a Data object (or some form since Date is deprecated):-) For the time being, I know when an error occurs, but don't have the foggiest idea how to render the error (set backround to red).
I need to give you more info too, since the way we do things is to make it as complicated as possible for job security and to impress the newbies(or scare them back to COBOL). When I setValueAt(), I have a DefaultDataElement object that houses everything for a given column in DB2. It has column name, length, data type, is it new, original value, current value, etc. If one doesn't exist, then I create one. After all said and done, I parse thru these elements and build my update/insert query and then execute the query.
The event starts as soon as the user presses the OK button. But, if a date cell contains an invalid date, I need to halt processing (which I've done) and somehow let the user know which cell was invalid. We consider our users dumb (they really aren't, but the assumption is made), so I can't just say "error occurred in table. Verify data and try again". That won't work:-) Gotta be able to show them what cell is bad.
Any ideas?
Thanks,
PatrickI finally figured it out. I was always close, but it wasn't until late yesterday that the light bulb came on. I was setting the background on the component in getTableCellEditorComponent() by grabbing the super.getTableCellEditorComponent(). The problem was all this was located in an editor, not renderer. So what happened was as I edited the cell the check would be performed then, not as focus was lost. When focus was lost, the red background went back to normal.
I ripped the code out and made a custom renderer and got closer. Problem was the whole column had a red background. Even specifying the row and column in getTableCellRendererComponent didn't apply just to that cell. That row and column is only for obtaining the data in the cell, but anything affecting the component will affect the column, since I added the renderer to my TableColumn:-) I need to add error logic to my model and when an error occurs, set a boolean flag in a 2 dimensional array where the row and column is used to get to it from the renderer.
Thanks for the help!!! -
Set background color when condition applied
hi,
Can anybody tell me how to set background color when condition applied.
My initial plan was to highlight a specify row(Change color) when the third column of this JTable consists of OK word.
int rowCount = table.getRowCount();
for(int i=0 ;i <rowCount ; i++)
String word = table.getValueAt(i,3).toString();
if(word.equals("Word")) {
table.setSelectionBackground(Color.red);
break;
else {
table.setBackground(getBackground());
Thanks.import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.table.*;
public class TableChanges
public TableChanges()
String[] headers = { "column 1", "column 2", "column 3", "column 4" };
int rows = 18;
int cols = 4;
String[][] data = new String[rows][cols];
for(int row = 0; row < rows; row++)
for(int col = 0; col < cols; col++)
data[row][col] = "item " + (row * cols + col + 1);
JTable table = new JTable(data, headers);
// add custom cell renderer to each column
TableColumnModel columnModel = table.getColumnModel();
for(int j = 0; j < columnModel.getColumnCount(); j++)
TableColumn col = columnModel.getColumn(j);
col.setCellRenderer(new CellRenderer());
// add table model listener to listen for cell edits
// so we can detect our special selection edit value
// which is "word"
TableModel tableModel = table.getModel();
tableModel.addTableModelListener(new ModelListener(table));
JScrollPane scrollPane = new JScrollPane(table);
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().add(scrollPane);
f.setSize(400,400);
f.setLocation(200,200);
f.setVisible(true);
Dimension d = scrollPane.getViewport().getSize();
table.setRowHeight(d.height/rows);
public static void main(String[] args)
new TableChanges();
class ModelListener implements TableModelListener
JTable table;
public ModelListener(JTable table)
this.table = table;
public void tableChanged(TableModelEvent e)
int firstRow = e.getFirstRow();
int lastRow = e.getLastRow();
int colIndex = e.getColumn();
if(e.getType() == TableModelEvent.UPDATE &&
firstRow != TableModelEvent.HEADER_ROW &&
colIndex != TableModelEvent.ALL_COLUMNS &&
colIndex == 2) // column 3 only
String value = (String)table.getValueAt(firstRow, colIndex);
// check for our special selection criteria
if(value.equals("word"))
for(int j = 0; j < table.getColumnCount(); j++)
CellRenderer renderer =
(CellRenderer)table.getCellRenderer(firstRow, j);
renderer.setSpecialSelection(firstRow, j);
class CellRenderer extends DefaultTableCellRenderer
int specRow, specCol;
public CellRenderer()
specRow = -1;
specCol = -1;
public Component getTableCellRendererComponent(JTable table,
Object value,
boolean isSelected,
boolean hasFocus,
int row, int col)
setHorizontalAlignment(JLabel.CENTER);
Color color = Color.black;
if(hasFocus)
color = Color.blue;
else if(isSelected)
color = Color.green.darker();
else if(row == specRow && col == specCol)
color = color.red;
setForeground(color);
setText((String)value);
return this;
public void setSpecialSelection(int row, int col)
specRow = row;
specCol = col;
} -
Background color for column heading
Hi,
I have 5 columns in my report and I would like to have different background color for column headings. How can I do this in report templates?
I was looking at Column heading section-
<th class="t10ReportHeader"#ALIGNMENT# id="#COLUMN_HEADER_NAME#">#COLUMN_HEADER#</th>
How can I specify backgrund colors for all columns here?
Thanks in advanceG'day Karen,
Thanks for ur reply.
I have seen this note before, its not that the header colors have changed after latest SP was applied in our systems. Also the ABAP and JAVA stacks are in sync.
Scenario is that we are trying to set up a new Portal theme for Enterprise Reporting with some specific color codes (hex codes).
What i have observed is Characterstics Header background color is set up using the element "Background Color of Level 1 Column Heading ".
While the Background color for Key Figures header is using element "Background Color of Level 2 Column Heading ".
But the problem is element "Background Color of Level 2 Column Heading " is also used as the Background color for Standard Characterstics cell, so if we make it same the whole report of the same color, which we dont want.
Requirement is to get the same Background color for Header cells (both Char. and Key Figure headers).
Any further suggestions will be appreciated.
Thanks
CK -
Setting background for single row in JTable
Hi,
I have a table which uses the DefaultCellRenderer for cols0,1,3-5
while column 2 uses a custom renderer.
When user right clicks mouse on one of the rows i get the rowindex and colindex selected and tries to set background color for this row. but the whole table get painted(of course except 2nd column)...rather i need just the row to be colored.....any solution is greatly appreciated.the code is below....
int rowIndex=table.rowAtPoint(p);
int colIndex = table.columnAtPoint(p);
for (int i = 0; i < 6; i++) {
if (i!=2){
DefaultTableCellRenderer r = (DefaultTableCellRenderer) table.getCellRenderer(rowIndex,i);
Component c = taskListTbl.prepareRenderer(r, rowIndex, i);
c.setBackground(Color.yellow);
}Maybe my reply in this posting will help:
http://forum.java.sun.com/thread.jspa?forumID=57&messageID=1730267 -
Set Background color for headers in excel
DECLARE
v_fh UTL_FILE.file_type;
v_dir VARCHAR2 (30) := 'my dir';
v_file VARCHAR2 (30) := 'test.xls';
PROCEDURE run_query (p_sql IN VARCHAR2)
IS
v_v_val VARCHAR2 (4000);
v_n_val NUMBER;
v_d_val DATE;
v_ret NUMBER;
c NUMBER;
d NUMBER;
col_cnt INTEGER;
f BOOLEAN;
rec_tab DBMS_SQL.desc_tab;
col_num NUMBER;
BEGIN
c := DBMS_SQL.open_cursor;
-- parse the SQL statement
DBMS_SQL.parse (c, p_sql, DBMS_SQL.native);
-- start execution of the SQL statement
d := DBMS_SQL.EXECUTE (c);
-- get a description of the returned columns
DBMS_SQL.describe_columns (c, col_cnt, rec_tab);
-- bind variables to columns
FOR j IN 1 .. col_cnt
LOOP
CASE rec_tab (j).col_type
WHEN 1
THEN
DBMS_SQL.define_column (c, j, v_v_val, 4000);
WHEN 2
THEN
DBMS_SQL.define_column (c, j, v_n_val);
WHEN 12
THEN
DBMS_SQL.define_column (c, j, v_d_val);
ELSE
DBMS_SQL.define_column (c, j, v_v_val, 4000);
END CASE;
END LOOP;
-- Output the column headers
UTL_FILE.put_line (v_fh, '<ss:Row>');
FOR j IN 1 .. col_cnt
LOOP
UTL_FILE.put_line (v_fh, '<ss:Cell>');
UTL_FILE.put_line (v_fh,
'<ss:Data ss:Type="String">'
|| rec_tab (j).col_name
|| '</ss:Data>'
UTL_FILE.put_line (v_fh, '</ss:Cell>');
END LOOP;
UTL_FILE.put_line (v_fh, '</ss:Row>');
-- Output the data
LOOP
v_ret := DBMS_SQL.fetch_rows (c);
EXIT WHEN v_ret = 0;
UTL_FILE.put_line (v_fh, '<ss:Row>');
FOR j IN 1 .. col_cnt
LOOP
CASE rec_tab (j).col_type
WHEN 1
THEN
DBMS_SQL.COLUMN_VALUE (c, j, v_v_val);
UTL_FILE.put_line (v_fh, '<ss:Cell>');
UTL_FILE.put_line (v_fh,
'<ss:Data ss:Type="String">'
|| v_v_val
|| '</ss:Data>'
UTL_FILE.put_line (v_fh, '</ss:Cell>');
WHEN 2
THEN
DBMS_SQL.COLUMN_VALUE (c, j, v_n_val);
UTL_FILE.put_line (v_fh, '<ss:Cell>');
UTL_FILE.put_line (v_fh,
'<ss:Data ss:Type="Number">'
|| TO_CHAR (v_n_val)
|| '</ss:Data>'
UTL_FILE.put_line (v_fh, '</ss:Cell>');
WHEN 12
THEN
DBMS_SQL.COLUMN_VALUE (c, j, v_d_val);
UTL_FILE.put_line (v_fh,
'<ss:Cell ss:StyleID="OracleDate">'
UTL_FILE.put_line (v_fh,
'<ss:Data ss:Type="DateTime">'
|| TO_CHAR (v_d_val,
'YYYY-MM-DD"T"HH24:MI:SS'
|| '</ss:Data>'
UTL_FILE.put_line (v_fh, '</ss:Cell>');
ELSE
DBMS_SQL.COLUMN_VALUE (c, j, v_v_val);
UTL_FILE.put_line (v_fh, '<ss:Cell>');
UTL_FILE.put_line (v_fh,
'<ss:Data ss:Type="String">'
|| v_v_val
|| '</ss:Data>'
UTL_FILE.put_line (v_fh, '</ss:Cell>');
END CASE;
END LOOP;
UTL_FILE.put_line (v_fh, '</ss:Row>');
END LOOP;
DBMS_SQL.close_cursor (c);
END;
PROCEDURE start_workbook
IS
BEGIN
UTL_FILE.put_line (v_fh, '<?xml version="1.0"?>');
UTL_FILE.put_line
(v_fh,
'<ss:Workbook xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet">'
END;
PROCEDURE end_workbook
IS
BEGIN
UTL_FILE.put_line (v_fh, '</ss:Workbook>');
END;
PROCEDURE start_worksheet (p_sheetname IN VARCHAR2)
IS
BEGIN
UTL_FILE.put_line (v_fh,
'<ss:Worksheet ss:Name="' || p_sheetname || '">'
UTL_FILE.put_line (v_fh, '<ss:Table>');
END;
PROCEDURE end_worksheet
IS
BEGIN
UTL_FILE.put_line (v_fh, '</ss:Table>');
UTL_FILE.put_line (v_fh, '</ss:Worksheet>');
END;
PROCEDURE set_date_style
IS
BEGIN
UTL_FILE.put_line (v_fh, '<ss:Styles>');
UTL_FILE.put_line (v_fh, '<ss:Style ss:ID="OracleDate">');
UTL_FILE.put_line
(v_fh,
'<ss:NumberFormat ss:Format="dd/mm/yyyy\ hh:mm:ss"/>'
UTL_FILE.put_line (v_fh, '</ss:Style>');
UTL_FILE.put_line (v_fh, '</ss:Styles>');
END;
BEGIN
v_fh := UTL_FILE.fopen (v_dir, v_file, 'w', 32767);
start_workbook;
set_date_style;
start_worksheet ('OM');
run_query ('select PARTY_ID,PARTY_NAME from HZ_PARTIES
where PARTY_ID<1080');
end_worksheet;
start_worksheet ('PO');
run_query ('SELECT AGENT_ID,TYPE_LOOKUP_CODE FROM PO_HEADERS_ALL
WHERE PO_HEADER_ID<20');
end_worksheet;
end_workbook;
UTL_FILE.fclose (v_fh);
END;
Here i will get two outputs in same excel with different spread sheets,
now i want to set background color for headers i
ex:PARTY_ID,PARTY_NAME,AGENT_ID,TYPE_LOOKUP_CODE are the headers,in excel output i want background color as blue.
Please do need full help,its urgent req.Hello,
open the file in Excel and save it, just to ensure that Excel writes all the additional stuff it thinks is necessary.
Now change the background colour of the header and save the file again but with a different name.
Open both files in a text editor and look at the differences. That's what you need to change in your code.
Alternatively you can use packages like xml_spreadsheet or ExcelDocumentType, both write the same file format that you use in your code and you don't have to reinvent the wheel.
Regards
Marcus -
How to set background color in PL/SQL
Hi,
I have tried to set background color in SQL Query. When I execute in Report Region, I am not getting the background color.
SELECT first query
UNION
SELECT ' || v_sel_organization_id || ' organization_id FROM DUAL) org , hr_all_organization_units haou WHERE positions_set.position_id = allocations_set.position_id(+) AND org.organization_id = positions_set.organization_id AND positions_set.position_id = encumbrance_set.enc_position_id(+) AND positions_set.position_id = payclass_set.position_id(+) AND org.organization_id = haou.organization_id AND TRUNC (SYSDATE) BETWEEN haou.date_from AND NVL (haou.date_to, TRUNC (SYSDATE)) AND (NVL (allocations_set.allocations, 0) + NVL (encumbrance_set.enc_count, 0) + NVL (payclass_set.pay_count, 0)) != 0 union select null , '' '' ,'' '',''<SPAN STYLE="background-color: red;">Total</span>'' ,''' || v_sum_alloc ||''','|| v_sum_vac ||','''|| v_sum_encum ||''',''' || v_sum_pay ||''' ,null,null,null,null,2 from dual' ; Can anyone help me to resolve this issue.
Regards
Balaji S
Edited by: Balaji Subramaniam on Jan 19, 2010 5:53 PM
Edited by: Balaji Subramaniam on Jan 19, 2010 5:53 PM
Edited by: Balaji Subramaniam on Jan 19, 2010 6:46 PM
Edited by: Balaji Subramaniam on Jan 19, 2010 6:48 PMHi
A couple of things...
Thats SQL - not PL/SQL
That is not a valid SQL statement, I'm surprised its not giving an error. Please post the exact region source.
When posting code on the forum, put {noformat}{noformat} (with the curly brackets and the word code in lowercase) above and below your code like this...
{noformat}{noformat}
SELECT *
FROM emp
{noformat}{noformat}
It will then appear like this... SELECT *
FROM emp
Cheers
Ben -
How to set background color in JTF GRID
Is it possible to set background color in JTF GRID ?
Hello Pavo,
it's also possible to take the code from ebitar and use the expression within styleClass instead of inlineStyle.
E.g. you can define a custom style "StyleClassEmptyText" in your skin and set this styleclass if af:inputtext is empty.
By using style classes you can have the same style in the whole application and you are able to change this style on a single point(in the styleclass) for the whole application.
br
Peter -
How to set background color in af:inputText in an af:table
Hi,
how to set background color in af:inputText in an af:table depending on the value of af:inputText.
For example, how to set background red if the af:inpuText is empty
ThanksHello Pavo,
it's also possible to take the code from ebitar and use the expression within styleClass instead of inlineStyle.
E.g. you can define a custom style "StyleClassEmptyText" in your skin and set this styleclass if af:inputtext is empty.
By using style classes you can have the same style in the whole application and you are able to change this style on a single point(in the styleclass) for the whole application.
br
Peter -
How to set background color for selected days in DateChooser
How to set background color for selected days. I created
checkbox for each day [Son,Mon,Tue,Wed,Thu,Fri,Sat] and a
DateChooser, I want to change the background color for the selected
day when i click on a button after selecting the desired checkboxs
[ monthly wise/yearly wise]
Thanks in advanceThere is no button involved in the following code, but it may
be of use to you:
<?xml version="1.0"?>
<mx:Application xmlns:mx="
http://www.adobe.com/2006/mxml"
creationComplete="init()">
<mx:Script>
<![CDATA[
private var origColor:uint;
private function init():void {
origColor = dc.getStyle("selectionColor");
public function setBackGrdColors(newColor:uint):void {
dc.setStyle("selectionColor", origColor);
if(dc.selectedDate){
var dayOfWeek:Number = dc.selectedDate.day;
else{
return;
switch(dayOfWeek) {
case 0:
if(sun.selected)
dc.setStyle("selectionColor", newColor);
break;
case 1:
if(mon.selected)
dc.setStyle("selectionColor", newColor);
break;
case 2:
if(tue.selected)
dc.setStyle("selectionColor", newColor);
break;
case 3:
if(wed.selected)
dc.setStyle("selectionColor", newColor);
break;
case 4:
if(thu.selected)
dc.setStyle("selectionColor", newColor);
break;
case 5:
if(fri.selected)
dc.setStyle("selectionColor", newColor);
break;
case 6:
if(sat.selected)
dc.setStyle("selectionColor", newColor);
break;
default:
break;
]]>
</mx:Script>
<mx:VBox horizontalAlign="center" verticalGap="20">
<mx:DateChooser id="dc" textAlign="left"
change="setBackGrdColors(cellColor.selectedColor)"/>
<mx:HBox width="100%" horizontalAlign="center">
<mx:CheckBox id="sun" label="Sun"/>
<mx:CheckBox id="mon" label="Mon"/>
<mx:CheckBox id="tue" label="Tue"/>
<mx:CheckBox id="wed" label="Wed"/>
</mx:HBox>
<mx:HBox width="100%" horizontalAlign="center">
<mx:CheckBox id="thu" label="Thu"/>
<mx:CheckBox id="fri" label="Fri"/>
<mx:CheckBox id="sat" label="Sat"/>
</mx:HBox>
<mx:HBox width="300" horizontalAlign="center">
<mx:Label text="Background Color" />
<mx:ColorPicker id="cellColor"
selectedColor="#FF00FF"/>
</mx:HBox>
</mx:VBox>
</mx:Application> -
Setting background Color to Form in J2ME
Hello All,
I want to set background color to the form. Can anybody know how to set the background color to form. Please help ..... :)
--- SachinIt's impossible, you have to use canvas instead of form to set background color
-
Could set background color of scrollbar?
Hy
is it possibile set background color of scrollbar?
thanksFrom my experience on this - yes, setting the colour (or the Visual Attribute) of the Block object will determine the background colour of the scrollbar. As far as I can see, it doesn't affect anything else.
I've had to ensure all my Blocks have a background colour set (to r88g88b88), because if it's left as <unspecified>, then the background of the scrollbar will be transparent. In Client-Server 6i, this isn't too bad, as the scrollbar still has a visible 'edge', but in 10.1.2.0.2 WebForms, this edge isn't present, meaning the background of the scrollbar isn't visible at all, just the two ends and a disembodied drag button. Not the end of the world, but some users thought it looked funny. -
Set background color to inside of the border in JPanel.
Hi all,
I want to set background color for JPanel with in the TitledBorder limit.
Thanks in Advance.
Siddhes.>
I want to set background color for JPanel with in the TitledBorder limit.>Here is another approach..
import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
class ColoredInnerPanel {
public static void main(String[] args) {
JPanel coloredPanel = new JPanel();
coloredPanel.setBackground(Color.RED);
coloredPanel.setPreferredSize( new Dimension(300,400) );
JPanel outerPanel = new JPanel(new BorderLayout());
outerPanel.add( coloredPanel );
outerPanel.setBorder(new TitledBorder("I See Red"));
JOptionPane.showMessageDialog(null, outerPanel);
}
Maybe you are looking for
-
WCF Service and Sharepoint Form library : How i can read or access a form libray and query a item from file name and read form xml in WCF service ? Ahsan Ranjha
-
The headings bar in Safari has gone negative ie white text on a black background. How can I get it back to black on white? <Invert Colours> does it but leaves everything else totally weird!
-
hi I have a dimension with two levels 1. Program 2. Courses Program which is the parent level contains the name of the Programs like: MSc Electrical Engineering, MSc Computer Science, PhD Environmental Engineering.... Course is the child level and co
-
Hello! I want to sell my iphone 3g because i have 3gs- i cleared the info in settings and now have black screen saying no service and usb port pointing to itunes - is that all i do ? i am pretty ignorant of computers- lol- i had read that i need to p
-
Reconnecting the footage to the sequence
Could really do with you're help. I have delete all the media files from my film (accidently). I have now recaptured every one of them again. There is no red slash through the clips on the browser. Now that I have recaptured it all I cannot seem to g