Results 1 to 4 of 4
Thread: Random numbers
- 03-09-2011, 05:25 PM #1
Member
- Join Date
- Mar 2011
- Posts
- 1
- Rep Power
- 0
Random numbers
how to implement the code for displaying random numbers to JTable
i know a different way to implement it but with the Netbeans's default generated code for the table, i have no idea how
jTable1.setModel(new javax.swing.table.DefaultTableModel(
new Object [24][],
new String [] {
"PROCESS", "AT", "BT", "TYPE"
}
));
- 03-09-2011, 05:41 PM #2
The best advice I can give you is to ditch the code generator and read this tutorial: How to Use Tables (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Swing Components)
How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
- 03-09-2011, 06:25 PM #3
Senior Member
- Join Date
- Feb 2011
- Posts
- 118
- Rep Power
- 0
It's easier to roll your own.
Java Code:import java.math.Random; import javax.swing.table.AbstractTableModel; public void RandomTableModel extends AbstractTableModel { private int _rows; private int _cols; private Random _random; public RandomTableModel(int numRows, int numCols) { _rows = numRows; _cols = numCols; _rand = new Random(System.currentTimeMillis()); } public int getRowCount() { return _rows; } public int getColumnCount() { return _col; } public Object getObjectAt(int row, int col) { return new Integer(_rand.nextInt(100)); // for random numbers from 0 to 99, incl. } }
- 03-09-2011, 06:39 PM #4
A few small issues, especially as your post is almost spoonfeeding:
There is no getObjectAt() function. That should probably be getValueAt().
Aren't you returning a new random number each time the model is called? I'm pretty sure that's not what the OP wants- he probably wants to initialize them to random values, then have them not change.
Are you an old-school programmer? Starting variables with an underscore goes against the standard naming conventions.How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
Similar Threads
-
How do I generate random numbers in a certain range using the random class?
By frasifrasi in forum New To JavaReplies: 8Last Post: 04-19-2009, 05:50 PM -
Help with random numbers
By checkmylongboarding in forum New To JavaReplies: 2Last Post: 01-12-2009, 05:47 AM -
Random numbers
By jithan in forum Advanced JavaReplies: 3Last Post: 06-14-2008, 02:04 PM -
random numbers without random class`
By carlos123 in forum New To JavaReplies: 4Last Post: 01-17-2008, 10:44 PM -
random numbers
By carlos123 in forum New To JavaReplies: 1Last Post: 12-22-2007, 02:56 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks