X4nd3r
asked on
JTable header problem
I am working with JTabbedPane.In the associated JPanel which i am passing in the JTabbedPane constructor, i want to add a JTable.But the table header is not showing up.This is not so when i am working with normal JPanels.
Please help.
Please help.
Can you post your code that you use to create the panel and the table, and add it to the JTabbedPane?
You should add the table to a scroll pane then add the scroll pane to the tabbed pane
ASKER
I have atached the code.actually this is a part of my project which is a city guide manager.i take values from my oracle database and display it on screen using jtable.yiu may find some errors bcoz i have declared objects of some other classes.you may ignore that.help me if u can.
//AWT PACKAGES.
import java.sql.*;
import java.net.*;
import java.util.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
//SWING PACKAGES.
import javax.swing.*;
import javax.swing.event.*;
/* This part of the program leads to the various entertainment facilities available at the city.
The visitors can have a great time at the different multiplexes, shopping complexes, and other
tourist places. */
public class tabEntertainment extends JFrame implements ActionListener
{
private JTabbedPane jtpEntertainment;
private JTable jtMultiplex;
private JPanel jpMultiplex,jpRecPark,jpNi
private JScrollPane jspTouristPlaces,jspMultip
private JButton jbMap,jbMultiplexPhotos;
private JLabel jlT1,jlM1;
private ImageIcon iconT1,iconM1;
private String Topic,Addr,phNo;
private int i=0;
public tabEntertainment()
{
super("ENTERTAINMENT");
Container pane2aa = getContentPane();
jtpEntertainment = new JTabbedPane(JTabbedPane.TO
jpMultiplex = new JPanel(new FlowLayout());
jpRecPark = new JPanel(new FlowLayout());
jpNightLife = new JPanel(new FlowLayout());
jpTouristPlaces = new JPanel(null);
jpShopping = new JPanel(new FlowLayout());
jpMultiplex.setBackground(
jpRecPark.setBackground(Co
jpNightLife.setBackground(
jpTouristPlaces.setBackgro
jpShopping.setBackground(C
jbMultiplexPhotos= new JButton(" Click here to view Multiplex Photos ");
jbMultiplexPhotos.setBackg
jbMultiplexPhotos.setForeg
//jbMultiplexPhotos.setBou
jpMultiplex.add(jbMultiple
jbMultiplexPhotos.addActio
jbMap= new JButton(" Click here to view Tourist Map ");
jbMap.setBackground(Color.
jbMap.setForeground(Color.
jbMap.setBounds(250,30,300
jpTouristPlaces.add(jbMap)
jbMap.addActionListener(th
final String Header[] = {"NAME","ADDRESS","PHONE"}
String MultiplexData[][] = new String[20][3];
jtpEntertainment.addTab("M
jtpEntertainment.addTab("R
jtpEntertainment.addTab("N
jtpEntertainment.addTab("T
jtpEntertainment.addTab("S
jtpEntertainment.setSelect
pane2aa.add(jtpEntertainme
try
{
DriverManager.registerDriv
Connection connEnt = DriverManager.getConnectio
Statement sMultiplex = connEnt.createStatement();
ResultSet rsMultiplex = sMultiplex.executeQuery("S
try
{
while(rsMultiplex.next())
{
Topic=rsMultiplex.getStrin
Addr=rsMultiplex.getString
phNo=rsMultiplex.getString
MultiplexData[i][0]=Topic;
MultiplexData[i][1]=Addr;
MultiplexData[i][2]=phNo;
i++;
}
}
catch(SQLException e){System.err.println("SQL
}
catch(SQLException e){System.err.println("SQL
jtMultiplex = new JTable(MultiplexData,Heade
//jtMultiplex.setBounds(20
jpMultiplex.add(jtMultiple
addWindowListener
(
new WindowAdapter()
{
public void WindowClosing(WindowEvent we)
{
System.exit(0);
}
}
);
}
public void actionPerformed(ActionEven
{
if(ae.getSource()==jbMap)
{
TouristMap tMap = new TouristMap();
tMap.setSize(800,575);
tMap.setLocation(0,0);
tMap.show();
}
else if(ae.getSource()==jbMulti
{
MultiplexPhotos tMultiplexPhotos = new MultiplexPhotos();
tMultiplexPhotos.setSize(8
tMultiplexPhotos.setLocati
tMultiplexPhotos.show();
}
}
public static void main(String args[])
{
tabEntertainmnet teObj = new tabEntertainment();
teObj.setSize(800,575);
teObj.setLocation(0,0);
teObj.show();
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Isn't that exactly what i said a week ago?
Sorry - i'd already posted that answer
ASKER
thanx bapi and CEHJ,
i did not take the first quick reply from the rockin CEHJ seriously. sorry dude,not to ignore any of ur answers from next time.the thing did work out nicely.Could any of you please tell me how to set different column widths to different columns(text size is different for diff. columns) and change header colours???Please...
i did not take the first quick reply from the rockin CEHJ seriously. sorry dude,not to ignore any of ur answers from next time.the thing did work out nicely.Could any of you please tell me how to set different column widths to different columns(text size is different for diff. columns) and change header colours???Please...
Well i don't think i'll be answering this or any other of your future questions then...
ASKER
well well CEHJ
i m new to this forum and did not have ne ideas bout ur capabilities...sorry if that hurt u,what i wrote in the prev. comment.
i m new to this forum and did not have ne ideas bout ur capabilities...sorry if that hurt u,what i wrote in the prev. comment.
OK - please be more careful to read the answers in future. Maybe bapi can answer your other question since he's only added the value of implementing my suggestion so far...
Mr. X4nd3r,
I think you should give the points to CEHJ.
Actually why i posted again the answer was, even after the solution was given by CEHJ, you posted your code...so i thought you didnot understand, the solution givne by CEHJ, so i posted the code so that u can understand it.
Since CEHJ has given the answer, he should get the points..so revert back the points given to me and allocate it to CEHJ..
Hey CEHJ cool man...we are here to assist the guys who need help.
I think you should give the points to CEHJ.
Actually why i posted again the answer was, even after the solution was given by CEHJ, you posted your code...so i thought you didnot understand, the solution givne by CEHJ, so i posted the code so that u can understand it.
Since CEHJ has given the answer, he should get the points..so revert back the points given to me and allocate it to CEHJ..
Hey CEHJ cool man...we are here to assist the guys who need help.
Don't worry bapi - i'm not blaming you ;-) Really it would have been nice if X4nd3r had posted some points for me and you'd answered his extra problem
for the setting the width of each column independently,
jtMultiplex.getColumnModel ().getColu mn(<colnum ber>).setW idth(<size >);
jtMultiplex.getColumnModel ().getColu mn(<colnum ber>).setP referredWi dth(<size> );
For setting the Header Colors or fonts you need to render it with a JLabel
Example of Renderer:
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.plaf.basic.*;
public class HeaderRenderer extends JLabel implements TableCellRenderer, java.io.Serializable {
private JLabel downLabel,upLabel,iconLabe l;
private javax.swing.border.Border border = UIManager.getBorder("Table Header.cel lBorder");
private Font font = UIManager.getFont("TableHe ader.font" );
private Color fgColor = UIManager.getColor("TableH eader.fore ground");
private Color bgColor = UIManager.getColor("TableH eader.back ground");
private int horAlign = 2;
private int verAlign = 0;
public HeaderRenderer() {
}
/**
*This method returns the TableCellRenderer component
*/
public Component getTableCellRendererCompon ent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int row, int column) {
JLabel label = this;
label.setOpaque(true);
label.setText((value ==null) ? "" : (" "+value.toString()));
label.setBorder(getBorder( ));
label.setForeground(getFor egroundCol or());
label.setBackground(getBac kgroundCol or());
label.setHorizontalAlignme nt(horAlig n);
label.setVerticalAlignment (verAlign) ;
return label;
}
// Sets the foreground color for header
public void setForegroundColor(Color fgColor) {
this.fgColor=fgColor;
}
// Gets the foreground color of header
public Color getForegroundColor() {
return fgColor;
}
// Sets the background color for header
public void setBackgroundColor(Color bgColor) {
this.bgColor=bgColor;
}
// Gets the background color of header
public Color getBackgroundColor() {
return bgColor;
}
// Sets the border for header
public void setBorder(javax.swing.bord er.Border border) {
this.border = border;
}
// Gets the header border
public javax.swing.border.Border getBorder() {
return border;
}
public void setHeaderHorizontalAlignme nt(int horAlign) {
this.horAlign = horAlign;
}
public void setHeaderVerticalAlignment (int verAlign) {
this.verAlign = verAlign;
}
public int getHeaderHorizontalAlignme nt() {
return horAlign;
}
public int getHeaderVerticalAlignment () {
return verAlign;
}
}
After that add this renderer to each and every column header
HeaderRenderer renderer = new HeaderRenderer();
jtMultiplex.getColumnModel ().getColu mn(0).setH eaderRende rer(render er);
jtMultiplex.getColumnModel ().getColu mn(1).setH eaderRende rer(render er);
jtMultiplex.getColumnModel ().getColu mn(2).setH eaderRende rer(render er);
or else u can take an enumeration and do it in a loop if you have many columns
After rendering them u can call the methods in HeaderRenderer for setting the background or foreground or bla..bla...
renderer = (HeaderRenderer)(jtMultipl ex.getColu mnModel(). getColumn( <colnumber >)).getHea derRendere r();
renderer.setBackgroundColo r(Color.GR EEN);
renderer.setForegroundColo r(Color.RE D);
jtMultiplex.getTableHeader ().repaint ();
jtMultiplex.getColumnModel
jtMultiplex.getColumnModel
For setting the Header Colors or fonts you need to render it with a JLabel
Example of Renderer:
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.plaf.basic.*;
public class HeaderRenderer extends JLabel implements TableCellRenderer, java.io.Serializable {
private JLabel downLabel,upLabel,iconLabe
private javax.swing.border.Border border = UIManager.getBorder("Table
private Font font = UIManager.getFont("TableHe
private Color fgColor = UIManager.getColor("TableH
private Color bgColor = UIManager.getColor("TableH
private int horAlign = 2;
private int verAlign = 0;
public HeaderRenderer() {
}
/**
*This method returns the TableCellRenderer component
*/
public Component getTableCellRendererCompon
boolean isSelected, boolean hasFocus, int row, int column) {
JLabel label = this;
label.setOpaque(true);
label.setText((value ==null) ? "" : (" "+value.toString()));
label.setBorder(getBorder(
label.setForeground(getFor
label.setBackground(getBac
label.setHorizontalAlignme
label.setVerticalAlignment
return label;
}
// Sets the foreground color for header
public void setForegroundColor(Color fgColor) {
this.fgColor=fgColor;
}
// Gets the foreground color of header
public Color getForegroundColor() {
return fgColor;
}
// Sets the background color for header
public void setBackgroundColor(Color bgColor) {
this.bgColor=bgColor;
}
// Gets the background color of header
public Color getBackgroundColor() {
return bgColor;
}
// Sets the border for header
public void setBorder(javax.swing.bord
this.border = border;
}
// Gets the header border
public javax.swing.border.Border getBorder() {
return border;
}
public void setHeaderHorizontalAlignme
this.horAlign = horAlign;
}
public void setHeaderVerticalAlignment
this.verAlign = verAlign;
}
public int getHeaderHorizontalAlignme
return horAlign;
}
public int getHeaderVerticalAlignment
return verAlign;
}
}
After that add this renderer to each and every column header
HeaderRenderer renderer = new HeaderRenderer();
jtMultiplex.getColumnModel
jtMultiplex.getColumnModel
jtMultiplex.getColumnModel
or else u can take an enumeration and do it in a loop if you have many columns
After rendering them u can call the methods in HeaderRenderer for setting the background or foreground or bla..bla...
renderer = (HeaderRenderer)(jtMultipl
renderer.setBackgroundColo
renderer.setForegroundColo
jtMultiplex.getTableHeader
ASKER
Hi bapi
i am sorry to say that i do not know how to revert back the points for CEHJ...i would be happy to do so.Could u please tell me that,and answer the second portion for me.I know that i would not be able to give points for that to u.
i am sorry to say that i do not know how to revert back the points for CEHJ...i would be happy to do so.Could u please tell me that,and answer the second portion for me.I know that i would not be able to give points for that to u.
Since CEHJ wants the points, let him answer how to rever that..
I have posted the answer for your second question.
Hey X4nd3r, I don't need any points for this...cool buddy.
I have posted the answer for your second question.
Hey X4nd3r, I don't need any points for this...cool buddy.
All questions about points can be answered by https://www.experts-exchange.com/Community_Support/
bapi should of course be given credit for his contributions
bapi should of course be given credit for his contributions