Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 600
  • Last Modified:

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.
0
X4nd3r
Asked:
X4nd3r
  • 7
  • 4
  • 4
  • +1
1 Solution
 
TimYatesCommented:
Can you post your code that you use to create the panel and the table, and add it to the JTabbedPane?
0
 
CEHJCommented:
You should add the  table to a scroll pane then add the scroll pane to the tabbed pane
0
 
X4nd3rAuthor Commented:

     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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
bapiCommented:
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
 
CEHJCommented:
Isn't that exactly what i said a week ago?
0
 
CEHJCommented:
Sorry - i'd already posted that answer
0
 
X4nd3rAuthor Commented:
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
 
CEHJCommented:
Well i don't think i'll be answering this or any other of your future questions then...
0
 
X4nd3rAuthor Commented:
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
 
CEHJCommented:
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
 
bapiCommented:
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
 
CEHJCommented:
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
 
bapiCommented:
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
 
X4nd3rAuthor Commented:
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
 
bapiCommented:
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
 
CEHJCommented:
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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 7
  • 4
  • 4
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now