# 4
Hi all,
after getting some files from the java web, i managed to make the code out. but in the end i have 3 error which by my weak knowleadge of java, i cant solve it. its about some abstract problem. here is my program:
import java.sql.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
import java.io.*;
import java.util.*;
/**
* TableDemo is just like SimpleTableDemo, except that it
* uses a custom TableModel.
*/
public class SearchByRandom extends JPanel {
ImageIcon background;
Image backgroundImage;
private JLabel jLabel1 = null;
private JButton SearchRandom = null;
private boolean DEBUG = false;
public SearchByRandom() {
super(new GridLayout(1,0));
background = new ImageIcon("BackGrd.jpg");
backgroundImage = background.getImage(); // for background image
setLayout(null);
add(jLabel1(), null);
add(getSearchByRandom(), null);
JTable table = new JTable(new MyTableModel()); //error2
table.setPreferredScrollableViewportSize(new Dimension(500, 70));
//table.setFillsViewportHeight(true);
//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 MyTableModel extends AbstractTableModel { //error1
private String[] columnNames = {"Disc Title",
"Disc ID",
"Status",
"Type",
"Discription",
"Language",
"Rating",
"No. of Disc",
"Cost Price",
};
private Object[][] data = {
{"Transformers", "M0001", "Available", "Movie", "robots", "English", 6, 2, "$6.00"},
{"Transformers", "M0001", "Available", "Movie", "robots", "English", 6, 2, "$6.00"},
{"Transformers", "M0001", "Available", "Movie", "robots", "English", 6, 2, "$6.00"},
{"Transformers", "M0001", "Available", "Movie", "robots", "English", 6, 2, "$6.00"},
{"Transformers", "M0001", "Available", "Movie", "robots", "English", 6, 2, "$6.00"},
{"Transformers", "M0001", "Available", "Movie", "robots", "English", 6, 2, "$6.00"},
{"Transformers", "M0001", "Available", "Movie", "robots", "English", 6, 2, "$6.00"},
};
//all the above info are the same becasuse im just testing the code.
public int getColumnCount() {
return columnNames.length;
}
public int getRowCount() {
return data.length;
}
public String getColumnName(int col) {
return columnNames[col];
}
public Object getValueAt(int row, int col) {
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); //error3
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[j]);
}
System.out.println();
}
System.out.println("--");
}
}
private JLabel jLabel1(){
if (jLabel1 == null) {
jLabel1 = new JLabel("Search By Random");
jLabel1.setBounds(new java.awt.Rectangle(100, 140, 500, 105));
jLabel1.setOpaque(false);
jLabel1.setFont(new java.awt.Font("Comic Sans MS", 0, 30));
//jLabel1.setForeground(new java.awt.Color(255, 255, 255));
}
return jLabel1;
}
private JButton getSearchByRandom() {
if (SearchRandom == null) {
SearchRandom = new JButton("Click here to generate another random list!");
SearchRandom.setBounds(new java.awt.Rectangle(100, 400, 280, 30));
SearchRandom.setOpaque(false);
SearchRandom.setBorder(null);
SearchRandom.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
toSearchByRandom(e);
}
}
);
}
return SearchRandom;
}
public void toSearchByRandom(ActionEvent e){
JFrame frame = new JFrame("Search By Random");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setContentPane(new SearchRandom());
frame.setResizable (false);
frame.setSize(800, 600);
frame.setLocation(50, 50);
frame.setVisible(true);
}
public void paintComponent(Graphics g) {
super.paintComponent(g); //paint background
//Draw image at its natural size first.
g.drawImage(backgroundImage, 0, 0,800,600, this);
}
}
and my errors are:
--Configuration: <Default>--
C:\Users\Titus\Documents\School\IT2292 OOADPJ\Project\Program\SearchByRandom.java:45: cannot find symbol
symbol : class AbstractTableModel
location: class SearchByRandom
class MyTableModel extends AbstractTableModel {
^
C:\Users\Titus\Documents\School\IT2292 OOADPJ\Project\Program\SearchByRandom.java:33: cannot find symbol
symbol : constructor JTable(SearchByRandom.MyTableModel)
location: class javax.swing.JTable
JTable table = new JTable(new MyTableModel());
^
C:\Users\Titus\Documents\School\IT2292 OOADPJ\Project\Program\SearchByRandom.java:119: cannot find symbol
symbol : method fireTableCellUpdated(int,int)
location: class SearchByRandom.MyTableModel
fireTableCellUpdated(row, col);
^
3 errors
Pls advise. thanks you!!
Titus