Results 1 to 4 of 4

Thread: Random numbers

  1. #1
    zel2zel is offline Member
    Join Date
    Mar 2011
    Posts
    1
    Rep Power
    0

    Default 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"
    }
    ));

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is online now Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,964
    Rep Power
    8

    Default

    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!

  3. #3
    NRitH is offline Senior Member
    Join Date
    Feb 2011
    Posts
    118
    Rep Power
    0

    Default

    Quote Originally Posted by zel2zel View Post
    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"
    }
    ));
    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.
      }
    }

  4. #4
    KevinWorkman's Avatar
    KevinWorkman is online now Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,964
    Rep Power
    8

    Default

    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

  1. Replies: 8
    Last Post: 04-19-2009, 05:50 PM
  2. Help with random numbers
    By checkmylongboarding in forum New To Java
    Replies: 2
    Last Post: 01-12-2009, 05:47 AM
  3. Random numbers
    By jithan in forum Advanced Java
    Replies: 3
    Last Post: 06-14-2008, 02:04 PM
  4. random numbers without random class`
    By carlos123 in forum New To Java
    Replies: 4
    Last Post: 01-17-2008, 10:44 PM
  5. random numbers
    By carlos123 in forum New To Java
    Replies: 1
    Last Post: 12-22-2007, 02:56 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •