Refreshing JTable

I am still having many of the same problems as before and am looking for more help. I am beginning to wonder if I created my JTable correctly.

First of all, my program receives an integer from another class to define the number of rows for the table. The user will then enter in their numbers into the table. However, I cannot get the fireTableCellUpdated() method to work as the same with others inherited by AbstractTable Model. The numbers entered by the user turn back to 0 and are not refreshed.

Please check my coding methods, I am totally lost! I know there is a lot of code, but I know my problem is within the second class. That is where I need help.

import javax.swing.*;

import javax.swing.JPanel.*;

import java.awt.*;

import java.awt.event.*;

import javax.swing.table.AbstractTableModel;

publicclass RegressInputextends JFrame

{

private JComboBox listJComboBox;

private JButton answerButton;

private JPanel textFieldPanel;

private CardLayout cardSelector;/

private JPanel cardDeck;

public RegressInput(String title)

{

super(title);

//CREATE A CONTAINER

Container container = getContentPane();

cardSelector =new CardLayout();

cardDeck =new JPanel();

cardDeck.setLayout(cardSelector);

Label question =new Label("How many PAIRS of data would you like to enter?");

answerButton =new JButton("OK");

listJComboBox =new JComboBox( getArray() );

listJComboBox.setMaximumRowCount(10);

//BUILD CARD DECK

JPanel comboBoxCard =new JPanel();

comboBoxCard.add(question);

comboBoxCard.add(listJComboBox);

comboBoxCard.add(answerButton); /

cardDeck.add(comboBoxCard,"Step 1");

container.add(cardDeck);

//DEFINE BUTTON HANDLER OBJECT

ButtonHandler buttonHandler =new ButtonHandler();

//ADD ACTION LISTENER FOR BUTTONS

answerButton.addActionListener(new ButtonHandler());

}//END RegressInput() CONSTRUCTOR

//METHOD TO CREATE AND RETURN AN ARRAY OF VALUES FOR JComboBox

private String[] getArray()

{

int numbers[] =newint[49];

int number = 2;

//ASSIGN VALUES FROM 2 TO 30 TO numbers[] ARRAY

for(int count=0; count < 49; ++count)

{

numbers[count] = number;

number++;

}//END for LOOP

//CREATE pairs[] ARRAY TO HOLD 30 STRINGS

String pairs[] =new String[49];

//ASSIGN VALUES 1 TO 30 IN STRING ARRAY FOR COMBO BOX

for(int count = 0; count < 49; ++count)

{

pairs[count] ="" + numbers[count];

}//END for LOOP

return pairs;

}//END getArray() METHOD8

//RETURNS THE ITEM SELECTED BY THE USER FROM THE JComboBox

privateint getValue()

{

//ASSIGNS STRING VALUE OF THE JComboBox TO A WRAPPER

Integer v =new Integer((String) listJComboBox.getSelectedItem());

//ASSIGNS WRAPPER VALUE TO int

int value = v.intValue();

//RETURN VALUE OF SELECTED ITEM

return value;

}//END getValue()

//BUTTON EVENT HANDLER CLASS

privateclass ButtonHandlerimplements ActionListener

{

//PROCESS EVENT

publicvoid actionPerformed(ActionEvent e)

{

//WHICH BUTTON CAUSED THE EVENT?

if(e.getSource() == answerButton)

{

Table table =new Table(getValue());

cardDeck.add(table,"Step 2");//ADD SECOND CARD TO DECK

cardSelector.last(cardDeck);

cardDeck.setSize(600,600);

}//END if STATEMENT

}//END actionPerformed()

}//END ButtonHandler CLASS

}//END RegressInput CLASS

My problem is somewhere in this class, and I know the problem is not the definition of the table in the constructor.

import javax.swing.*;//FOR SWING COMPONENT CLASSES

import javax.swing.JPanel.*;

import java.awt.*;//FOR CONTAINER CLASS

import java.awt.event.*;//FOR EVENT HANDLING

import javax.swing.table.AbstractTableModel;

publicclass Tableextends JPanel

{

privateboolean DEBUG =false;

public Table(int value)

{

super(new GridLayout(1,0));

int row = value;

JTable table =new JTable(new InputTable(row));

table.setPreferredScrollableViewportSize(new Dimension(500, 70));

//Create the scroll pane and add the table to it.

JScrollPane scrollPane =new JScrollPane(table);

//Add the scroll pane to this panel.

add(scrollPane);

}

class InputTableextends AbstractTableModel{

privateboolean DEBUG =false;

private String[] columnNames ={"X Values","Y Values"};

privatefinalint COLUMNS = getColumnCount();

privateint[][] data;

public InputTable(int row)

{

int rows = 2;

rows = row;

data =newint[rows][COLUMNS];

}

publicint getColumnCount(){

return columnNames.length;

}

publicint getRowCount(){

return data.length;

}

public String getColumnName(int col){

return columnNames[col];

}

public Integer getValueAt(int row,int col){

return data[row][col];

}

public Class getColumnClass(int c){

return getValueAt(0, c).getClass();

}

publicboolean isCellEditable(int row,int col){

returntrue;

}

publicvoid setValueAt(Integer value,int row,int col){

if (DEBUG){

System.out.println("Setting value at " + row +"," + col

+" to " + value

+" (an instance of "

+ value.getClass() +")");

}

data[row][col] = value;

fireTableCellUpdated(row, col);

if (DEBUG){

System.out.println("New value of data:");

printDebugData();

}

}

privatevoid printDebugData(){

int numrows = getRowCount();

int numCols = getColumnCount();

for (int i=0; i < numrows; i++){

System.out.print("row " + i +":");

for (int j=0; j < numCols; j++){

System.out.print(" " + data[i][j]);

}

System.out.println();

}

System.out.println("--");

}

}

}

And finally, main.

import javax.swing.JFrame;

publicclass TestSharpStats

{

publicstaticvoid main(String[] args)

{

//DEFINE FRAME OBJECT

RegressInput window =new RegressInput("Hi");//SETS TITLE BAR

window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//CLOSES WINDOW

window.setSize(400, 600);//SET FRAME SIZE

window.setResizable(true);//PREVENTS USER FROM RESIZING WINDOW

window.setVisible(true);//SETS window TO VISIBLE

}//END main()

}//END TestSharpStats CLASS

[13995 byte] By [greenspacechunksa] at [2007-10-3 9:27:50]
# 1

> I am totally lost!

So start with something simple. Don't write your own TableModel. Use the DefaultTableModel. It fires all the appropriate events. It has method that allow you to set the number of rows or add rows dynamically.

http://forum.java.sun.com/thread.jspa?forumID=57&threadID=577919

By the way how many times do you have to be told not to mix AWT and Swing components? You are still using an AWT component.

camickra at 2007-7-15 4:42:09 > top of Java-index,Desktop,Core GUI APIs...
# 2
I meant to reply on all those. What is it exactly I am doing wrong as far as AWT and Swing?
greenspacechunksa at 2007-7-15 4:42:09 > top of Java-index,Desktop,Core GUI APIs...
# 3
Swing components start with "J".Swing - JLabelAWT - Label
camickra at 2007-7-15 4:42:09 > top of Java-index,Desktop,Core GUI APIs...
# 4
Well, that wasn't too hard to get now was it. Sorry. I would rather fix what code I do have because it is somewhat complex code. I'm a beginning programmer and rather fond of how much time i put into making it work this way. I just need it to refresh the cells!
greenspacechunksa at 2007-7-15 4:42:09 > top of Java-index,Desktop,Core GUI APIs...
# 5

The TableModel stores its data as Objects, not Integers.

a) the data array should be a two dimensional array of Objects

b) the getColumnClass should return Integer.class as the class for both columns. This will cause the approriated renderer and editor to be used for display and editing of the data

c) getValueAt(...) returns an Object not and Integer

d) setValueAt(...) receives an Object not an Integer

For points "c" and "d" you are not overriding the method if you change the method signature, you are creating a new method that never gets invoked.

Why didn't you turn on the debug flag and you would see that the method was never invoked. Why add debug code if you never use it?

camickra at 2007-7-15 4:42:09 > top of Java-index,Desktop,Core GUI APIs...
# 6
Thanks so much! Finally got it working!!!
greenspacechunksa at 2007-7-15 4:42:09 > top of Java-index,Desktop,Core GUI APIs...