Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Increasing the column size in JTabe

  1. #1
    Junior Member
    Join Date
    May 2012
    Posts
    4
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Question Increasing the column size in JTabe

    hi,
    im trying to get data from my database and add it to the JTable directly...
    here is my code..


    import java.awt.*;
    import java.util.*;
    import javax.swing.*;
    import java.awt.event.*;
    import javax.swing.table.*;
    import java.sql.*;
    class JTableDatabase
    {
    public static JFrame f;
    public static JButton b;

    public void callFrame()
    {
    b=new JButton("back");
    b.setBounds(400,600,100,25);
    b.addActionListener(new CallAction());

    Vector columnNames = new Vector();

    Vector data = new Vector();
    JPanel p=new JPanel();
    p.add(b);
    try
    {

    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    Connection con = DriverManager.getConnection("jdbcdbc:leeSQL","sa","adminadmin");
    String str = "Select * from payroll.dbo.emp";
    Statement stmt = con.createStatement();
    ResultSet rs = stmt.executeQuery(str);
    ResultSetMetaData md = rs.getMetaData();
    int columns = md.getColumnCount();
    for (int i = 1; i <= columns; i++)
    {
    columnNames.addElement( md.getColumnName(i) );

    }


    while (rs.next())
    {

    Vector row = new Vector(columns);

    for (int i = 1; i <= columns; i++)
    {
    row.addElement( rs.getObject(i) );
    }

    data.addElement( row );

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

    JTable table = new JTable(data, columnNames);

    // to be changed wen u get error
    table = new JTable(data, columnNames)
    {
    public boolean isCellEditable(int rowIndex,int colIndex)
    {
    return false;
    }
    };
    TableColumn col;
    for (int i = 0; i < table.getColumnCount(); i++) {
    col = table.getColumnModel().getColumn(i);
    //col.setMaxWidth(1000);

    }

    JTableHeader header = table.getTableHeader();
    header.setBackground(Color.yellow);

    JScrollPane scrollPane = new JScrollPane( table );
    p.add( scrollPane );

    f=new JFrame("REPORT");

    f.add(p);
    table.setFillsViewportHeight(false);
    // to be edited wen u get error.....

    //till this...

    f.setSize(2000,1500);
    f.setVisible(true);


    JTableDatabase jtdb=new JTableDatabase();

    }

    public void actionPerformed(ActionEvent ae){

    adminfunc af=new adminfunc();
    af.launchframe();
    f.setVisible(false);
    }

    public static void main(String args[])throws Exception{
    JTableDatabase j=new JTableDatabase();
    j.callFrame();
    }

    }


    finally my data from database appears in the JTable but im unable to change the column size since it looks stingy...so pls help me with it..

    dboutput.jpg

    as u can see the column size is small..so i need little help in changing the column size...pls help..thanks in advance...


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Increasing the column size in JTabe

    Please wrap your code in the code tags to maintain proper formatting.

    To change the column width, get the appropriate TableColumn (see the API for JTable) and call the appropriate method (set*Width)). From the image, it does not look as though your JTable is in a JScrollPane, which -if not - may be something you might wish to do

Similar Threads

  1. Increasing or decreasing!
    By pesha in forum Algorithms & Recursion
    Replies: 4
    Last Post: November 6th, 2011, 04:06 PM
  2. Applets progress bar not increasing
    By Edvinauskas in forum Java Servlet
    Replies: 8
    Last Post: November 22nd, 2010, 07:04 PM
  3. Limit File Size or Request Size
    By tarek.mostafa in forum Java Servlet
    Replies: 3
    Last Post: June 12th, 2010, 04:28 PM
  4. Limit File Size or Request Size
    By tarek.mostafa in forum JavaServer Pages: JSP & JSTL
    Replies: 3
    Last Post: June 11th, 2010, 07:21 AM
  5. increasing my java knowledge
    By doogiexiv in forum Java Theory & Questions
    Replies: 1
    Last Post: March 8th, 2010, 11:00 PM

Tags for this Thread