TextGrids in Java (help required)


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 13 of 13

Thread: TextGrids in Java (help required)

  1. #1
    Join Date
    May 2005
    Posts
    22

    TextGrids in Java (help required)

    I am currently working on a project in which I need to have a textGrid displaying a series of numbers.
    I downloaded two textGrid classes that seem unable to allow me to enter text into them, or move around the grids using the keyboard.
    I asked a friend of mine who mentioned I should attempt to write my own class, but I have no idea of how to do it properly so that values display correctly.
    If anybody is able to either write me a demonstration class or explain how I could go about this endevour, it would be greatly appreciated.
    Thanks

  2. #2
    Join Date
    Dec 2004
    Location
    San Bernardino County, California
    Posts
    1,468
    what does the documentation you received with the textGrid classes say about creating an instance of textGrid or invoiking the methods of the class?

  3. #3
    Join Date
    May 2005
    Posts
    22
    That's the problem. I don't have any documentation with them
    I can get the textgrid to display in the applet, but it doesn't do what it should i.e it just shows a series of boxes according to my specifications.

    I realise that I can possibly clarify what I want better:
    I'm looking for some kind of code that will allow me to display a series to editable textareas in this fashion:
    _____________
    |6|5|3|4|4|0|4|
    |7|4|8|9|9|5|6| etc.

    I need to be able to move between the individual cells with the keyboard arrow keys and each cell can accomodate a maximum of 2 characters.

  4. #4
    Join Date
    Dec 2004
    Location
    San Bernardino County, California
    Posts
    1,468
    So it sounds that you are invoking the textGrid class and can change the rows and columns.

    Did you get the code for these classes?

    A shot in the dark - there is a method which probably takes row, column, and text or string as arguments. Something such as "writeGrid()" or "addToGrid()" - can you find a method such as this?

  5. #5
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560

    This could be used

    I need to be able to move between the individual cells with the keyboard arrow keys and each cell can accomodate a maximum of 2 characters.
    .. at least it covers the specs qoted, it also uses a mouselistener:
    Attached Files Attached Files
    eschew obfuscation

  6. #6
    Join Date
    May 2005
    Posts
    22
    Thanks a lot sjalle, this class is just what I need.
    I have one or two problems that I can't quite figure out though.

    How do I specify the number of rows and columns, is it in a method that I missed somewhere?
    And how would I interface the textgrid with a non-applet program? I can't seem to get it to display in a frame or pane. The pane.add() method doesn't seem to take effect.
    Also, do the different cells have coordinates that can be displayed and used as variables when the individual cells are accessed?


    Again, thanks a lot.
    Last edited by cirrus; 05-23-2005 at 04:17 PM.

  7. #7
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560

    This prog is a hybrid...

    .. it is/runs as - an application when started from the console. If you
    embed it in a web page it runs as an applet. I usually do it like this
    when I make applets, its easier to trap bugs and run various tests.

    Well.
    How do I specify the number of rows and columns, is it in a method that I missed somewhere
    Its done in the getParameter method in TextGrid, I have restructured it here
    so its more clear what happends, - this method is invoked in the init method
    like:
    Code:
    rowCount = Integer.parseInt(this.getParameter("row_count", "10"));
    colCount = Integer.parseInt(this.getParameter("col_count", "10"));
    and here it is (restructured & commented)
    Code:
    //Get a parameter value
      public String getParameter(String key, String def) {
        if (isStandalone) {
          return System.getProperty(key, def); // APPLICATION MODE(will return def)
        } else { // APPLET MODE
          if (getParameter(key) != null) {
            return getParameter(key); // parameter was included in applet tag
          } else {
            return def; // parameter not in applet tag, use default
          }
        }   
      }
    So you set these values in the applet tag (named row_count & col_count)
    and/or as the default ("10") in the calls from the init method.
    If you check out the static methods System.setProperty /System.getProperty
    you will see yet another way

    -----------------
    And how would I interface the textgrid with a non-applet program? I can't seem to get it to display in a frame or pane. The pane.add() method doesn't seem to take effect
    You see it all in the applets main() method. This is never invoked when
    running in a browser;
    So, being a hybrid it already is an application, but, in its application mode it treats the applet like a Panel. I'm not sure what kind
    of pane you are trying to add it to (this is non-swing code).
    ------------------
    do the different cells have coordinates that can be displayed and used as variables when the individual cells are accessed?
    The GridCell is a Rectangle extension, so it has inherited the public
    variables: x,y,width & height. These are the pixel position of the cell's
    topLeft and its pixel width & height (see the call to the rectangle method
    setBounds in the constructor of GridCell).

    GridCell is a java.awt.Rectangle (++)

    The private variables rowNo & colNo are the grid-coordinates of the cell.
    You can code set & get methods for those.
    If you want to display their values you can make yourself another smaller
    font and (in the draw method of GridCell) draw the values in one of the
    cell's corners. This will involve a little xy tweaking that I leave up to you
    Last edited by sjalle; 05-24-2005 at 05:56 AM.
    eschew obfuscation

  8. #8
    Join Date
    May 2005
    Posts
    22
    Thanks again.

    The problem though is that my app is swing based. How could I restructure the code to implement the textgrid as a component in the applet?

  9. #9
    Join Date
    May 2005
    Location
    Aberdeen
    Posts
    2

    Post Adding TextGrid as a component within an Applet

    You have to add the TextGrid to a JPanel and then add the panel to the applet.
    Farooq

  10. #10
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560
    No, do not mix awt & swing components. Rename all components in my source to
    J<name> components (JApplet, JFrame), and change to the
    <name>.getContentPane().add(<component>)
    way of adding components , - where it is required.
    eschew obfuscation

  11. #11
    Join Date
    May 2005
    Posts
    22
    Ah, right. Should've seen that.
    Been fiddling with class and it works perfectly.
    thanks a lot!

  12. #12
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560

    Dynamic grid

    This version uses arrowkeys + CTRL to add/remove rows/columns in the
    textGrid
    Attached Files Attached Files
    eschew obfuscation

  13. #13
    Join Date
    May 2005
    Posts
    22
    Thanks a lot for all your help!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center