GeeCON Prague 2014*
The moose likes Swing / AWT / SWT and the fly likes Color problem with JTable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Color problem with JTable" Watch "Color problem with JTable" New topic
Author

Color problem with JTable

francis joby
Greenhorn

Joined: Aug 30, 2005
Posts: 6
Dear/Respected Friends/Sirs/Madam

I can't set background and forground color for jtable heading. My code is below.


import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;
import java.sql.*;


class FeesEdit extends JFrame implements ActionListener,MouseListener
{
dbConnection dbcon;
ResultSet rs;
ResultSetMetaData rsmd;

JScrollPane scroll;
JSplitPane split;
JTable table;
DefaultTableModel model;
JButton btn_delete,btn_edit;
JPanel panel,panel1;
Container container;
BorderLayout bl;
JLabel label_regno,label_coursecode,label_amount,label_date;
JTextField txt_regno,txt_coursecode,txt_amount,txt_date;

FeesEdit()
{
dbcon=new dbConnection();

container=getContentPane();
bl=new BorderLayout();
panel=new JPanel();
panel1=new JPanel();
panel1.setLayout(bl);
panel.setLayout(null);

label_regno=new JLabel("RegisterNo");
label_coursecode=new JLabel("CourseCode");
label_amount=new JLabel("Amount");
label_date=new JLabel("Date");

txt_regno=new JTextField();
txt_coursecode=new JTextField();
txt_amount=new JTextField();
txt_date=new JTextField();

btn_delete=new JButton("Delete");
btn_edit=new JButton("Edit");

label_regno.setBounds(30,20,80,25);
label_coursecode.setBounds(30,50,80,25);
label_amount.setBounds(30,80,80,25);
label_date.setBounds(30,110,80,25);

txt_regno.setBounds(150,20,130,25);
txt_coursecode.setBounds(150,50,130,25);
txt_amount.setBounds(150,80,130,25);
txt_date.setBounds(150,110,130,25);

btn_delete.setBounds(60,150,80,25);
btn_edit.setBounds(180,150,80,25);

panel.add(label_regno);
panel.add(txt_regno);


panel.add(label_coursecode);
panel.add(txt_coursecode);


panel.add(label_amount);
panel.add(txt_amount);


panel.add(label_date);
panel.add(txt_date);


panel.add(btn_delete);
panel.add(btn_edit);

int v=ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED;
int h=ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED;


try{

rs=dbcon.stm.executeQuery("select * from fees");

String colname[]={"RegisterNo","CourseCode","Amount","Date"};
String dbcolname[]={"regno","coursecode","amount","date"};
int rowcount=0;

while(rs.next())
{
rowcount++;
}
rs.close();
rs=dbcon.stm.executeQuery("select * from fees");

int row = 0;


String coldata[][]=new String[rowcount][4];

while(rs.next())
{
for(int k=0;k<4;k++)
{
coldata[row][k] = rs.getString(dbcolname[k]);
}
row++;
}




model=new DefaultTableModel(coldata,colname);

table=new JTable(model);
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
table.setGridColor(Color.green);
table.setShowGrid(true);
table.setSelectionForeground(Color.red);
table.setSelectionBackground(Color.blue);
table.setRowHeight(25);


scroll=new JScrollPane(table,v,h);
}
catch(Exception e)
{
System.out.println("sql error from FeesEdit"+e);}
panel1.add(scroll,BorderLayout.CENTER);
split=new JSplitPane(JSplitPane.VERTICAL_SPLIT,panel1,panel);
split.setDividerLocation(200);
container.add(split);
setSize(400,420);
setVisible(true);
btn_delete.addActionListener(this);
btn_edit.addActionListener(this);


table.addMouseListener(this);

}


public void mouseClicked(MouseEvent me)
{
System.out.println("Mouseclicked");
try{

int x=table.getSelectedRow();
System.out.println(x);
boolean flag=table.isRowSelected(x);
if(flag==true)
{
String str[]=new String[4];
for(int i=0;i<4;i++)
{
str[i]=String.valueOf(table.getValueAt(x,i));
}
txt_regno.setText(str[0]);
txt_coursecode.setText(str[1]);
txt_amount.setText(str[2]);
txt_date.setText(str[3]);

}
}
catch(Exception e) {System.out.println(e);}
}

public void mouseEntered(MouseEvent me){}
public void mouseExited(MouseEvent me){}
public void mousePressed(MouseEvent me){}
public void mouseReleased(MouseEvent me){}

public void actionPerformed(ActionEvent ae)
{
if(ae.getSource()==btn_delete)
{
try{
int x=table.getSelectedRow();
System.out.println(x);
rs=dbcon.stm.executeQuery("select * from fees");


String st=String.valueOf(table.getValueAt(x,0));
dbcon.stm.executeUpdate("delete from fees where regno='"+st+"'");

model.removeRow(x);

}catch(Exception e){System.out.println(e);}

} // btn_delete


if(ae.getSource()==btn_edit)
{
try{
int x=table.getSelectedRow();

rs=dbcon.stm.executeQuery("select * from fees");

String str[]=new String[4];
for(int i=0;i<4;i++)
{
str[i]=String.valueOf(table.getValueAt(x,i));
}


String st=String.valueOf(table.getValueAt(x,0));
String regno=new String(txt_regno.getText());
String coursecode=new String(txt_coursecode.getText());
String amount=new String(txt_amount.getText());
String date=new String(txt_date.getText());

table.setValueAt(regno,x,0);
table.setValueAt(coursecode,x,1);
table.setValueAt(amount,x,2);
table.setValueAt(date,x,3);
rs.close();

String qry="update fees set regno='"+regno+"',coursecode='"+coursecode+"',amount='"+amount+"',date='"+date+"' where regno='"+st+"'";
System.out.println(qry);
dbcon.stm.executeUpdate(qry);

model.removeRow(x);
String newdata[]={regno,coursecode,amount,date};
model.insertRow(x,newdata);
} catch(Exception e) {System.out.println(e);}

} //btn_ edit


} // actionPerformed

public static void main(String args[])
{
new FeesEdit();
}
}
Rachel Swailes
Ranch Hand

Joined: May 18, 2004
Posts: 434
Hi there

If you want to set the color of the table header, you actually need to do the following

myTable.getTableHeader().setBackground(myColor);

This example sets the background color, for the foreground color and other settings, have a look at the javadocs.

Cheers,
Rachel
francis joby
Greenhorn

Joined: Aug 30, 2005
Posts: 6
Dear Rachal Sir,

Your replay is very helpful for me. I have very grateful towards you. I have implemented this in my code and it is very successful.

Regards
Francis Joby
 
GeeCON Prague 2014
 
subject: Color problem with JTable