Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Java > Other Java > Java GUI
|
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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old February 4th, 2005, 03:20 PM
Authorized User
 
Join Date: Feb 2005
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");
       button.addActionListener(this);
       add(button);


        }

     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);
       frame.setSize(700,700);
       frame.setVisible(true);
     }







   //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++){
                 data.add("Hi");
             }


     }


     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){

          data.setElementAt(obj,(row)*(col));
          fireTableCellUpdated(row,col);
  }

}//inner class ends here



}//main class ends here


ciao,
Sandz

sands
__________________
Sandz
 
Old February 5th, 2005, 08:28 AM
Authorized User
 
Join Date: Feb 2005
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to Sandz
Default

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



Sandz





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





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