Generating radom data

Hi all,

i'm a student in polytechnic in Singapore. i am doing a disc rental project using jcreator right now and is facing some difficulties.

i need to create a table inside a class with back ground, some lables, a 10 rows table and a generate button. and what this table gotta do is to generate 10 random disc information from my data base. info which include title, cost price etc..depending on the title. and i need to have a button which everytime i click, it will generate another 10 set of random disc info out.

java experts here, pls help me and advise me! :) speaking the truth, my java is kinda lousy.. haha.

regards,

Titus

[671 byte] By [Titus-tana] at [2007-11-27 11:35:22]
# 1

hi! welcome to the forum.

here's a guide on using jtable and swing components:

http://java.sun.com/docs/books/tutorial/uiswing/components/table.html

Yannixa at 2007-7-29 17:02:26 > top of Java-index,Desktop,Core GUI APIs...
# 2

Hi,

erm ok thanks for the web page. guess i found some new things that can help me to improve in my table.

but back to the qns, can someone teach me how to generate random data from my database into the table? thanks :)

Titus

Titus-tana at 2007-7-29 17:02:26 > top of Java-index,Desktop,Core GUI APIs...
# 3

Do a google search for "java Math.random()" and once armed with that information you could just generate 10 random numebrs within the index range of your disc table and pull those indexes out of the DB and display them

c0demonk3ya at 2007-7-29 17:02:26 > top of Java-index,Desktop,Core GUI APIs...
# 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

Titus-tana at 2007-7-29 17:02:26 > top of Java-index,Desktop,Core GUI APIs...
# 5

add import javax.swing.table.*;

to fix the errors stated above

ICE

icewalker2ga at 2007-7-29 17:02:26 > top of Java-index,Desktop,Core GUI APIs...
# 6

And, in the future, use code tags to make your code easier to read.

By the way, welcome to the forum!

jaxiana at 2007-7-29 17:02:26 > top of Java-index,Desktop,Core GUI APIs...
# 7

Hi all,

thanks a lot, finally my table is out.. haha. but erm regarding about the random generating funtion, i tried searching for the random.maths() but i don really understand it, and i dont know how to i change it into my situation here. is there any one out there can kindly assit me in how to generate random data into my table? thanks! :)

TItus

Titus-tana at 2007-7-29 17:02:26 > top of Java-index,Desktop,Core GUI APIs...
# 8

> Hi all,

>

> thanks a lot, finally my table is out.. haha. but erm

> regarding about the random generating funtion, i

> tried searching for the random.maths() but i don

> really understand it, and i dont know how to i change

> it into my situation here. is there any one out there

> can kindly assit me in how to generate random data

> into my table? thanks! :)

>

> TItus

here's a guide on using Math.random():

http://java.about.com/library/weekly/aa_random_num.htm

after you generate random number then add it to your table by using the

setValueAt() method in jtable.

Yannixa at 2007-7-29 17:02:26 > top of Java-index,Desktop,Core GUI APIs...
# 9

Hi all,

sorry to trouble you guys so much.

i got the code from the jtable guide from Java site. and i guess there is some error within a few lines of my code, cause the result that was out was kinda different from what the java site has shown.

My header is not really a 'header', it is seen no different from other column. in the java site, their header can be easily see as header.

the other thing is that i have remove some of the code which will make my table editble, but in the end, im still able to edit.

and last things is that this code here:

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];

}

from the web site seem to made the header in the center, and the size of the column big enough for the data inside to be seen. but for me, it has no effect at all. pls help me. thanks:)

TItus

Titus-tana at 2007-7-29 17:02:26 > top of Java-index,Desktop,Core GUI APIs...
# 10

> from the web site seem to made the header in the

> center, and the size of the column big enough for the

> data inside to be seen. but for me, it has no effect

> at all. pls help me. thanks:)

if you download the source code from the jtable tutorial you can see a method

private void initColumnSizes(JTable table)

that is why size of the column is big enough for the data

can you post a compilable code?

Yannix

Yannixa at 2007-7-29 17:02:26 > top of Java-index,Desktop,Core GUI APIs...