Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Java > Other Java > Java GUI
Password Reminder
| FAQ | Members List | Search | Today's Posts | Mark Forums Read
Java GUI Discussions specific to programming Java GUI.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Java GUI section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old February 4th, 2005, 03:20 PM
Authorized User
Join Date: Feb 2005
Location: Hyderabad, AP, India.
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to Sandz
Default Whats wrong with the GUI display

Hi everyone,

I got a piece of Swing code and instead of showing the column names as One,Two,Three it shows A,B,C in the JTable!!! Can someone tell me whats wrong?

package test;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.color.*;
import javax.swing.BorderFactory;
import javax.swing.border.Border;
import javax.swing.border.TitledBorder;
import javax.swing.event.*;
import java.sql.*;
import javax.swing.table.AbstractTableModel;
import java.util.*;

class TableDemo extends JPanel implements ActionListener{

    //public variables
    public JTable table;
    public JButton button;
    private Vector data=new Vector(100);
    String[] columnNames={"one","two","Three"};
    public final AbstractTableModel model;

    public TableDemo(int totalRows,int totalCols){
       super(new GridBagLayout());
       model=new MyTable(totalRows,totalCols);
       table=new JTable(model);
       //adding the table to the scroll pane
       add(new JScrollPane(table));

       //adding the button and the event listener
       button=new JButton("Go");


     public void add(int rows,int cols){
         for(int r=0;r<rows;r++)
           for (int c=0;c<cols;c++){
                data.add("Extra Hi");

     //Action Event
     public void actionPerformed(ActionEvent e){

         for (int i=0;i<columnNames.length;i++){
         data.add("Extra Hi");

         System.out.println("someone pressed the button!");
        System.out.println("Current No of Rows "+data.size()/columnNames.length);


     //main thread
     public static void main(String[] args){
       JFrame frame=new JFrame();
       frame.getContentPane().add(new TableDemo(10,3));
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);

   //this is the Abstract Model class for the Table
   class MyTable extends AbstractTableModel{

     //this will go in the constructor
     public MyTable(int noOfRows,int noOfCols){

             //gotta get the total no of elements reqd to be populated in the vector
             //when we consider it as a 2D matrix
             int totalNoOfElements=noOfRows*noOfCols;
             for (int i=0;i<totalNoOfElements;i++){


     public void addNewRows(int row,int col){
              int totalNoOfElements=row*col;
             for (int i=0;i<totalNoOfElements;i++){
                 data.add("Extra Hi");


  public int getRowCount(){

             //this should be the total elements/no fo columns!Pretty simple huh?
             int size=data.size()/columnNames.length;
             System.out.println("Row Count:"+size);
             return size;
            //return data.length;


  public int getColumnCount(){
          return columnNames.length;

  public Object getValueAt(int row,int col){
          //this needs to be done for a vector now!
          return (Object)data.get((row)*(col));

  public void setValueAt(Object obj,int row,int col){


}//inner class ends here

}//main class ends here


Reply With Quote
  #2 (permalink)  
Old February 5th, 2005, 08:28 AM
Authorized User
Join Date: Feb 2005
Location: Hyderabad, AP, India.
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to Sandz

Hey I guess I got the error...
Well i hadnt included the getColumnName() method...sometimes we forget how dumb we can get...

Reply With Quote

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Textbox GUI display different from it's .Text mikehsu317 .NET Framework 2.0 0 March 15th, 2007 03:31 PM
Whats wrong? Agentofnight Beginning PHP 3 April 17th, 2005 04:11 AM
whats wrong with this code? sand133 VB.NET 2002/2003 Basics 2 September 12th, 2004 10:07 PM
Whats wrong with this code? Thomas82 Classic ASP Databases 6 February 26th, 2004 09:40 AM
whats wrong with this code ? qwprince Classic ASP Basics 5 July 27th, 2003 07:46 PM

All times are GMT -4. The time now is 12:49 AM.

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.