Solved

JTable header problem

Posted on 2003-10-21
16
555 Views
Last Modified: 2013-11-23
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.
0
Comment
Question by:X4nd3r
  • 7
  • 4
  • 4
  • +1
16 Comments
 
LVL 35

Expert Comment

by:TimYates
ID: 9591734
Can you post your code that you use to create the panel and the table, and add it to the JTabbedPane?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9591911
You should add the  table to a scroll pane then add the scroll pane to the tabbed pane
0
 

Author Comment

by:X4nd3r
ID: 9606257

     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,jpNightLife,jpTouristPlaces,jpShopping,jpMultiplexPhotos;
   private JScrollPane jspTouristPlaces,jspMultiplex;  
   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.TOP);
     
      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(Color.BLACK);
      jpRecPark.setBackground(Color.BLACK);
      jpNightLife.setBackground(Color.BLACK);
      jpTouristPlaces.setBackground(Color.BLACK);
      jpShopping.setBackground(Color.BLACK);
     
      jbMultiplexPhotos= new JButton(" Click here to view Multiplex Photos ");
      jbMultiplexPhotos.setBackground(Color.CYAN);
      jbMultiplexPhotos.setForeground(Color.BLACK);
      //jbMultiplexPhotos.setBounds(250,350,250,30);
      jpMultiplex.add(jbMultiplexPhotos);
      jbMultiplexPhotos.addActionListener(this);
     
      jbMap= new JButton(" Click here to view Tourist Map ");
      jbMap.setBackground(Color.CYAN);
      jbMap.setForeground(Color.BLACK);
      jbMap.setBounds(250,30,300,20);
      jpTouristPlaces.add(jbMap);
      jbMap.addActionListener(this);
     
   
      final String Header[] = {"NAME","ADDRESS","PHONE"};
     
      String MultiplexData[][] = new String[20][3];

     
      jtpEntertainment.addTab("Multiplex",jpMultiplex);
      jtpEntertainment.addTab("Recreation Parks",jpRecPark);
      jtpEntertainment.addTab("Night Life",jpNightLife);
      jtpEntertainment.addTab("Tourist Places",jpTouristPlaces);
      jtpEntertainment.addTab("Shopping",jpShopping);
     
      jtpEntertainment.setSelectedIndex(0);
      pane2aa.add(jtpEntertainment);

      try
       {
         DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
         Connection connEnt = DriverManager.getConnection("jdbc:oracle:thin:@daiict_oracle_9:1521:btech", "200201054", "connecting...");

         Statement sMultiplex = connEnt.createStatement();
               
         ResultSet rsMultiplex = sMultiplex.executeQuery("SELECT * FROM Multiplex");
         
         try
          {
            while(rsMultiplex.next())
             {
                  Topic=rsMultiplex.getString("Name");
                  Addr=rsMultiplex.getString("Address");
                  phNo=rsMultiplex.getString("Phone");
                  MultiplexData[i][0]=Topic;
                  MultiplexData[i][1]=Addr;
                  MultiplexData[i][2]=phNo;
                  i++;
             }
           
           
          }
         catch(SQLException e){System.err.println("SQLException: " + e.getMessage());}
         
       }
      catch(SQLException e){System.err.println("SQLException: " + e.getMessage());}
 
      jtMultiplex = new JTable(MultiplexData,Header);
      //jtMultiplex.setBounds(200,20,350,300);
      jpMultiplex.add(jtMultiplex);

      addWindowListener
       (
         new WindowAdapter()
          {
            public void WindowClosing(WindowEvent we)
             {
               System.exit(0);
             }
          }
       );
    }
   public void actionPerformed(ActionEvent ae)
    {
      if(ae.getSource()==jbMap)
       {
         TouristMap tMap = new TouristMap();
         tMap.setSize(800,575);
         tMap.setLocation(0,0);
         tMap.show();
       }
      else if(ae.getSource()==jbMultiplexPhotos)
       {
         MultiplexPhotos tMultiplexPhotos = new MultiplexPhotos();
         tMultiplexPhotos.setSize(800,575);
         tMultiplexPhotos.setLocation(0,0);
         tMultiplexPhotos.show();
       }
    }
  public static void main(String args[])
  {
         tabEntertainmnet  teObj = new tabEntertainment();
         teObj.setSize(800,575);
         teObj.setLocation(0,0);
         teObj.show();
   }
 }
0
 
LVL 1

Accepted Solution

by:
bapi earned 125 total points
ID: 9633381
jtMultiplex = new JTable(MultiplexData,Header);
 //jtMultiplex.setBounds(200,20,350,300);
jpMultiplex.add(jtMultiplex);

Replace the above with this
jtMultiplex = new JTable(MultiplexData,Header);
JScrollPane scrollpane = new JScrollPane(jtMultiplex);
jpMultiplex.add(scrollpane);


Whenever you are going for a JTable, you have to first add it to a scroll pane, then add to panel or whatever so.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9633402
Isn't that exactly what i said a week ago?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9634261
Sorry - i'd already posted that answer
0
 

Author Comment

by:X4nd3r
ID: 9634275
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...
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9634312
Well i don't think i'll be answering this or any other of your future questions then...
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:X4nd3r
ID: 9634556
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.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9634683
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...
0
 
LVL 1

Expert Comment

by:bapi
ID: 9639355
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.  
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9640663
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
0
 
LVL 1

Expert Comment

by:bapi
ID: 9640859
for the setting the width of each column independently,

jtMultiplex.getColumnModel().getColumn(<colnumber>).setWidth(<size>);
jtMultiplex.getColumnModel().getColumn(<colnumber>).setPreferredWidth(<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,iconLabel;
  private javax.swing.border.Border border = UIManager.getBorder("TableHeader.cellBorder");
  private Font font = UIManager.getFont("TableHeader.font");

  private Color fgColor = UIManager.getColor("TableHeader.foreground");
  private Color bgColor = UIManager.getColor("TableHeader.background");

  private int horAlign = 2;
  private int verAlign = 0;



  public HeaderRenderer() {

  }

 /**
 *This method returns the TableCellRenderer component
 */

      public Component getTableCellRendererComponent(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(getForegroundColor());
          label.setBackground(getBackgroundColor());
          label.setHorizontalAlignment(horAlign);
          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.border.Border border) {
              this.border = border;
      }
// Gets the header border
      public javax.swing.border.Border getBorder() {
              return border;
      }

      public void setHeaderHorizontalAlignment(int horAlign) {
            this.horAlign = horAlign;
      }

      public void setHeaderVerticalAlignment(int verAlign) {
            this.verAlign = verAlign;
      }
      public int getHeaderHorizontalAlignment() {
            return horAlign;
      }
      public int getHeaderVerticalAlignment() {
            return verAlign;
    }
}

After that add this renderer to each and every column header

HeaderRenderer renderer = new HeaderRenderer();
jtMultiplex.getColumnModel().getColumn(0).setHeaderRenderer(renderer);
jtMultiplex.getColumnModel().getColumn(1).setHeaderRenderer(renderer);
jtMultiplex.getColumnModel().getColumn(2).setHeaderRenderer(renderer);

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)(jtMultiplex.getColumnModel().getColumn(<colnumber>)).getHeaderRenderer();
renderer.setBackgroundColor(Color.GREEN);
renderer.setForegroundColor(Color.RED);
jtMultiplex.getTableHeader().repaint();
0
 

Author Comment

by:X4nd3r
ID: 9640874
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.
0
 
LVL 1

Expert Comment

by:bapi
ID: 9641240
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.

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9641523
All questions about points can be answered by http://www.experts-exchange.com/Community_Support/

bapi should of course be given credit for his contributions
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
How to install Selenium IDE and loops for quick automated testing. Get Selenium IDE from http://seleniumhq.org Go to that link and select download selenium in the right hand columnThat will then direct you to their download page.From that page s…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This video teaches viewers about errors in exception handling.

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now