corn jew

Greenhorn
+ Follow
since Sep 06, 2003
Merit badge: grant badges
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by corn jew

Hi all
I am playing around with JTable using the example from sun's TableDemo.The problem is that i have read through the tutorial ,but don't understand how the table updates it's data.I have written a method in the custom model class which extends AbstractTableModel to change the data when i call it from a button which calls that method,but nothing happens.I have also
tried to call tableData() method from that action ,but after clicking the
removeButton it gives me a blank table.Can anyone please explain to me
how these tables work with an example.


/*
* TableDemo.java is a 1.4 application that requires no other files.
*/

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JButton;
import javax.swing.table.AbstractTableModel;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.BorderLayout;
import java.awt.*;
import java.awt.event.*;
import javax.swing.AbstractButton;

/**
* TableDemo is just like SimpleTableDemo, except that it
* uses a custom TableModel.
*/
public class TableDemo extends JPanel implements ActionListener {
private boolean DEBUG = false;
MyTableModel mtm ;//= new MyTableModel();
JTable table = new JTable();
JButton rowButton;
JButton removeButton;

public TableDemo() {
super(new BorderLayout());
//super(new GridLayout(1,0));
mtm = new MyTableModel();
addButtons();
tableData();
}

public void tableData(){
System.out.println("start of changeTableData");
//sets the size of the table to match the model which it contains
table.setModel(this.mtm);
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,BorderLayout.CENTER);
System.out.println("end of changeTableData");

}

public void addButtons(){
System.out.println("add buttons ");

JPanel jp = new JPanel(new FlowLayout(FlowLayout.RIGHT));
rowButton = new JButton("select row");
rowButton.addActionListener(this);
removeButton = new JButton("remove row");
removeButton.addActionListener(this);
jp.add(rowButton);
jp.add(removeButton);
add(jp,BorderLayout.SOUTH);

}

public void actionPerformed(ActionEvent ae){
if(ae.getSource() == rowButton){
int row = table.getSelectedRow();
System.out.println("row selected = "+row);
int column = table.getSelectedColumn();
System.out.println("column selected = "+column);
String value = (String)table.getValueAt(row,column);
System.out.println("cell selected = "+value);

}
else{
Object[][] data = {
{"Sharon", "Zakhour",
"Speed reading", new Integer(20), new Boolean(true),new Integer(8),"banana"},
{"Philip", "Milne",
"Pool", new Integer(10), new Boolean(false),new Integer(9),"litchi"},

};

System.out.println("remove button clicked");
mtm = new MyTableModel();
mtm.setData(data);

}

}
////////////////////////////////////////////////////////////////////////////
class MyTableModel extends AbstractTableModel {
private String[] columnNames =
{"First Name","Last Name","Sport","# of Years","Vegetarian",
"code","fruit"};

private Object[][] data = {
{"Gregory", "Adams",
"Snowboarding", new Integer(5), new Boolean(false),new Integer(5),"pear"},
{"Cornelius", "Jewell",
"Rowing", new Integer(3), new Boolean(true),new Integer(6),"apple"},
{"Kathy", "Walrath",
"Knitting", new Integer(2), new Boolean(false),new Integer(7),"orange"},
{"Sharon", "Zakhour",
"Speed reading", new Integer(20), new Boolean(true),new Integer(8),"banana"},
{"Philip", "Milne",
"Pool", new Integer(10), new Boolean(false),new Integer(9),"litchi"},

};

//to change table data
public void setData(Object[][] data)
{
this.data = data;
fireTableDataChanged();
}

//methods that you must implement
public int getColumnCount() {//these methods are called by JTable
return columnNames.length;
}
//methods that you must implement
public int getRowCount() {//these methods are called by JTable

return data.length;
}

public String getColumnName(int col) {
return columnNames[col];
}
//methods that you must implement table call this method to display data
public Object getValueAt(int row, int col) {//these methods are called by JTable
return data[row][col];
}

/*
* JTable uses this method to determine the default renderer/
* editor for each cell. If we didn't implement this method,
* then the last column would contain text ("true"/"false"),
* rather than a check box.
*/
public Class getColumnClass(int c) {
return getValueAt(0, c).getClass();
}

/*
* Don't need to implement this method unless your table's
* editable.
*/
public boolean isCellEditable(int row, int col) {
//Note that the data/cell address is constant,
//no matter where the cell appears onscreen.
if (col < 2) {
return false;
} else {
return true;
}
}

/*
* Don't need to implement this method unless your table's
* data can change.
*/
public void setValueAt(Object 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);//for cell changes


if (DEBUG) {
System.out.println("New value of data:");
printDebugData();
}
}

private void 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("--------------------------");
}
}

/**
* Create the GUI and show it. For thread safety,
* this method should be invoked from the
* event-dispatching thread.
*/
public static void createAndShowGUI() {
//Make sure we have nice window decorations.
JFrame.setDefaultLookAndFeelDecorated(true);

//Create and set up the window.
JFrame frame = new JFrame("TableDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//Create and set up the content pane.
TableDemo newContentPane = new TableDemo();
//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();
}
});
}
}

Thanks for all your help.

Cheers.
18 years ago
Hi all
I want to make use of several input boxes where a user can enter any amount of decimal numbers say from 1 to 50, use that info, shuffle it and display the numbers.I found only TextField class,but it does not give you an option to restrict values entered to two values only.
Can someone perhaps help or is there a way to do it.
Example user put in numbers 1,2,10,15,16,20 and shuffle that numbers to give you a different set ie 15,1,20,etc.
Thanks for any help
Corn
19 years ago