Results 1 to 8 of 8

Thread: JTable Headers

  1. #1
    dking is offline Member
    Join Date
    Apr 2011
    Posts
    7
    Rep Power
    0

    Smile JTable Headers

    when i scroll horizontally, my table headers wont display. what cud b wrong?
    can som1 help? wud appreciate a working code!, thanks :)

  2. #2
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,233
    Rep Power
    7

    Default

    See: How to Use Tables (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Swing Components) for working examples.

    All you have to do is add:

    Java Code:
    table.setAutoResizeMode( JTable.AUTO_RESIZE_OFF );
    to one of the examples and then shrink the size of the frame to see the scrollbar.

  3. #3
    dking is offline Member
    Join Date
    Apr 2011
    Posts
    7
    Rep Power
    0

    Default

    thanks camickr, i'v already done setAutoResizeMode(AUTO_RESIZE_OFF); my table has many columns, i set d column width 2 some value so that d header text can b visible but d header of other columns wont display went i use d horizontal scroll bar to view those columns.

  4. #4
    RichersooN is offline Senior Member
    Join Date
    Jan 2011
    Location
    Rizal Province, Philippiines
    Posts
    167
    Rep Power
    0

    Default

    #1) Make sure that the table is inside JScollPane
    #2) Can you show us how you do (#1)

  5. #5
    dking is offline Member
    Join Date
    Apr 2011
    Posts
    7
    Rep Power
    0

    Default

    /*
    * To change this template, choose Tools | Templates
    * and open the template in the editor.
    */

    package tableDemo;

    /**
    *
    * @author DKING
    */

    import java.awt.Color;
    import java.awt.Component;
    import java.awt.Dimension;
    import java.sql.ResultSet;
    import java.sql.ResultSetMetaData;
    import java.sql.SQLException;

    import javax.swing.JTable;
    import javax.swing.table.DefaultTableModel;
    import javax.swing.table.JTableHeader;
    import javax.swing.table.TableCellRenderer;
    import javax.swing.table.TableRowSorter;

    /**
    * A JTable used to display a SQL ResultSet.
    * @author fahdshariff
    *
    */
    public class MyTable extends JTable{

    private final DefaultTableModel dataModel;
    static TableRowSorter<DefaultTableModel> sorter;

    public MyTable(ResultSet rs)
    throws SQLException{

    super();
    dataModel = new DefaultTableModel();
    setModel(dataModel);
    setSelectionForeground(Color.white);
    setSelectionBackground(new Color(105,74,33));
    setShowGrid(false);
    setFillsViewportHeight(true);
    setAutoCreateRowSorter(true);
    setAutoResizeMode(AUTO_RESIZE_OFF);
    //setPreferredScrollableViewportSize(new Dimension(1000, 1000));

    //filter records by user entry
    sorter = new TableRowSorter<DefaultTableModel>(dataModel);
    setRowSorter(sorter);

    try {
    //create an array of column names
    ResultSetMetaData mdata = rs.getMetaData();
    int colCount = mdata.getColumnCount();
    String[] columnNames = {"Program Name", "Program Code", "Maximum Amount Of Loan", "Interest Rate", "Time Duration", "Approval Authority"};
    dataModel.setColumnIdentifiers(columnNames);

    //now populate the data
    while (rs.next()) {
    String[] rowData = new String[colCount];
    for (int i = 1; i <= colCount; i++) {
    rowData[i - 1] = rs.getString(i);
    }
    dataModel.addRow(rowData);
    }
    for(int i = 1; i < colCount; i++)
    getColumnModel().getColumn(i -1).setPreferredWidth(100);
    }
    finally{
    try {
    rs.close();
    }
    catch (SQLException ignore) {
    }
    }

    JTableHeader header = this.getTableHeader();
    header.setForeground(new Color(239, 244, 8));
    header.setBackground(new Color(58, 89, 123));
    header.setPreferredSize(new Dimension(50, 30));
    }
    @Override
    public boolean isCellEditable(int row, int col) {
    //Note that the data/cell address is constant,
    //no matter where the cell appears onscreen.
    if (col < 6) {
    return false;
    } else {
    return true;
    }
    }

    @Override
    public Component prepareRenderer(TableCellRenderer renderer,int Index_row, int Index_col) {
    Component comp = super.prepareRenderer(renderer, Index_row, Index_col);
    //even index, selected or not selected
    if (Index_row % 2 == 0) {
    comp.setBackground(new Color(144, 203, 237));
    }
    else {
    comp.setBackground(new Color(176, 221, 240));
    }
    return comp;
    }
    }

    /////////////////////////////////////////////////////////////////////////

    /*
    * To change this template, choose Tools | Templates
    * and open the template in the editor.
    */

    package tableDemo;

    /**
    *
    * @author DKING
    */
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.JTable;
    import java.awt.GridLayout;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.Statement;
    import javax.swing.JFrame;

    public class MyTableContainer extends JPanel {
    private boolean DEBUG = false;
    private JTable table = null;

    public MyTableContainer() {
    super(new GridLayout(1,0));
    try
    {
    String query = "select * from LoanProgramDetails";
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    Connection con = DriverManager.getConnection("jdbc:odbc:HeartlandHo meFinance", "sa", "");
    Statement stmt = con.createStatement();
    ResultSet rs = stmt.executeQuery(query);

    table = new tableDemo.MyTable(rs);

    con.close();
    }
    catch(Exception e)
    {}

    //Create the scroll pane and add the table to it.
    JScrollPane scrollPane = new JScrollPane(table);

    //Add the scroll pane to this panel.
    add(scrollPane);
    }


    private static void createAndShowGUI() {
    //Create and set up the window.
    JFrame frame = new JFrame("TableDemo");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);

    //Create and set up the content pane.
    MyTableContainer newContentPane = new MyTableContainer();
    newContentPane.setOpaque(true); //content panes must be opaque
    frame.setContentPane(newContentPane);

    //Display the window.
    frame.pack();
    frame.setVisible(true);
    }

    public static void main(String[] args) {
    //Schedule a job for the event-dispatching thread:
    //creating and showing this application's GUI.
    javax.swing.SwingUtilities.invokeLater(new Runnable() {
    public void run() {
    createAndShowGUI();
    }
    });
    }

    }

  6. #6
    dking is offline Member
    Join Date
    Apr 2011
    Posts
    7
    Rep Power
    0

    Default here's d code

    /*
    * To change this template, choose Tools | Templates
    * and open the template in the editor.
    */

    package tableDemo;

    /**
    *
    * @author DKING
    */

    import java.awt.Color;
    import java.awt.Component;
    import java.awt.Dimension;
    import java.sql.ResultSet;
    import java.sql.ResultSetMetaData;
    import java.sql.SQLException;

    import javax.swing.JTable;
    import javax.swing.table.DefaultTableModel;
    import javax.swing.table.JTableHeader;
    import javax.swing.table.TableCellRenderer;
    import javax.swing.table.TableRowSorter;

    /**
    * A JTable used to display a SQL ResultSet.
    * @author fahdshariff
    *
    */
    public class MyTable extends JTable{

    private final DefaultTableModel dataModel;
    static TableRowSorter<DefaultTableModel> sorter;

    public MyTable(ResultSet rs)
    throws SQLException{

    super();
    dataModel = new DefaultTableModel();
    setModel(dataModel);
    setSelectionForeground(Color.white);
    setSelectionBackground(new Color(105,74,33));
    setShowGrid(false);
    setFillsViewportHeight(true);
    setAutoCreateRowSorter(true);
    setAutoResizeMode(AUTO_RESIZE_OFF);
    //setPreferredScrollableViewportSize(new Dimension(1000, 1000));

    //filter records by user entry
    sorter = new TableRowSorter<DefaultTableModel>(dataModel);
    setRowSorter(sorter);

    try {
    //create an array of column names
    ResultSetMetaData mdata = rs.getMetaData();
    int colCount = mdata.getColumnCount();
    String[] columnNames = {"Program Name", "Program Code", "Maximum Amount Of Loan", "Interest Rate", "Time Duration", "Approval Authority"};
    dataModel.setColumnIdentifiers(columnNames);

    //now populate the data
    while (rs.next()) {
    String[] rowData = new String[colCount];
    for (int i = 1; i <= colCount; i++) {
    rowData[i - 1] = rs.getString(i);
    }
    dataModel.addRow(rowData);
    }
    for(int i = 1; i < colCount; i++)
    getColumnModel().getColumn(i -1).setPreferredWidth(100);
    }
    finally{
    try {
    rs.close();
    }
    catch (SQLException ignore) {
    }
    }

    JTableHeader header = this.getTableHeader();
    header.setForeground(new Color(239, 244, 8));
    header.setBackground(new Color(58, 89, 123));
    header.setPreferredSize(new Dimension(50, 30));
    }
    @Override
    public boolean isCellEditable(int row, int col) {
    //Note that the data/cell address is constant,
    //no matter where the cell appears onscreen.
    if (col < 6) {
    return false;
    } else {
    return true;
    }
    }

    @Override
    public Component prepareRenderer(TableCellRenderer renderer,int Index_row, int Index_col) {
    Component comp = super.prepareRenderer(renderer, Index_row, Index_col);
    //even index, selected or not selected
    if (Index_row % 2 == 0) {
    comp.setBackground(new Color(144, 203, 237));
    }
    else {
    comp.setBackground(new Color(176, 221, 240));
    }
    return comp;
    }
    }

    /////////////////////////////////////////////////////////////////////////

    /*
    * To change this template, choose Tools | Templates
    * and open the template in the editor.
    */

    package tableDemo;

    /**
    *
    * @author DKING
    */
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.JTable;
    import java.awt.GridLayout;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.Statement;
    import javax.swing.JFrame;

    public class MyTableContainer extends JPanel {
    private boolean DEBUG = false;
    private JTable table = null;

    public MyTableContainer() {
    super(new GridLayout(1,0));
    try
    {
    String query = "select * from LoanProgramDetails";
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    Connection con = DriverManager.getConnection("jdbc:odbc:HeartlandHo meFinance", "sa", "");
    Statement stmt = con.createStatement();
    ResultSet rs = stmt.executeQuery(query);

    table = new tableDemo.MyTable(rs);

    con.close();
    }
    catch(Exception e)
    {}

    //Create the scroll pane and add the table to it.
    JScrollPane scrollPane = new JScrollPane(table);

    //Add the scroll pane to this panel.
    add(scrollPane);
    }


    private static void createAndShowGUI() {
    //Create and set up the window.
    JFrame frame = new JFrame("TableDemo");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);

    //Create and set up the content pane.
    MyTableContainer newContentPane = new MyTableContainer();
    newContentPane.setOpaque(true); //content panes must be opaque
    frame.setContentPane(newContentPane);

    //Display the window.
    frame.pack();
    frame.setVisible(true);
    }

    public static void main(String[] args) {
    //Schedule a job for the event-dispatching thread:
    //creating and showing this application's GUI.
    javax.swing.SwingUtilities.invokeLater(new Runnable() {
    public void run() {
    createAndShowGUI();
    }
    });
    }

    }

  7. #7
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,233
    Rep Power
    7

    Default

    here's d code
    Use proper words.

    I'm not going to read through all that code. How is your code different from the tutorial code?

    Post a Short, Self Contained, Correct Example if you need more help.

  8. #8
    RichersooN is offline Senior Member
    Join Date
    Jan 2011
    Location
    Rizal Province, Philippiines
    Posts
    167
    Rep Power
    0

    Default

    You should place the JTable in JScrollPane like this:
    Java Code:
    scollPane.setViewportView(table);
    scollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);

Similar Threads

  1. Problem in setting headers of jTable
    By arshadalisoomro in forum AWT / Swing
    Replies: 4
    Last Post: 03-04-2011, 07:13 AM
  2. resultset headers
    By tvrulesme in forum JDBC
    Replies: 7
    Last Post: 11-16-2010, 08:56 AM
  3. Sorting in JTable without clicking on column headers
    By ProgrammingPup in forum Advanced Java
    Replies: 6
    Last Post: 11-04-2009, 05:26 AM
  4. HttpClient invalid headers
    By Nicole in forum Advanced Java
    Replies: 6
    Last Post: 03-31-2009, 12:36 AM
  5. tooltips for JTable column headers
    By fossildoc in forum AWT / Swing
    Replies: 2
    Last Post: 12-18-2008, 11:42 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •