JLabel setText problem, help please


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 7 of 7

Thread: JLabel setText problem, help please

  1. #1
    Join Date
    Jan 2005
    Posts
    45

    JLabel setText problem, help please

    Here are my two pieces of code

    import java.io.*;
    import java.util.*;
    import javax.swing.*;
    import java.awt.*;

    public class RatRace {

    public static void main(String[] args) throws IOException {

    JFileChooser file = new JFileChooser();

    file.setDialogTitle("Where is your Rat Race map ?");

    if (file.showDialog(null,"OK") == JFileChooser.APPROVE_OPTION) {

    File f = file.getSelectedFile();
    BufferedReader read = new BufferedReader(new FileReader(f));

    //determine the dimensions of the character array
    String lineRead = "";
    int rows = 0;
    int columns = 0;
    while ((lineRead = read.readLine())!=null) {
    columns = lineRead.length();
    rows++;
    }

    //put the characters in the map file into a 2d array of characters
    String temp = "";
    BufferedReader br = new BufferedReader(new FileReader(f));
    char[][] myMap = new char[rows][columns];
    for (int r = 0; r < rows; r++) {
    while ((temp=br.readLine())!= null) {
    for (int c = 0; c < columns ; c++) {
    myMap[r][c] = temp.charAt(c);
    }
    }
    }

    MapWindow window = new MapWindow(myMap);

    }
    }
    }



    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.io.*;

    public class MapWindow extends JFrame{

    public MapWindow(char[][] map){

    JFrame window = new JFrame("Rat Race");
    window.getContentPane().setLayout(new GridLayout(map.length, map[0].length));
    JLabel[][]label = new JLabel[map.length][map[0].length];
    for (int r = 0; r < map.length; r++) {
    for (int c = 0; c < map[0].length ; c++) {
    label[r][c] = new JLabel();
    label[r][c].setText(Character.toString(map[r][c]));
    window.getContentPane().add(label[r][c]);
    }
    }
    window.pack();
    window.show();
    }

    }


    Im using Dr Java. When I call the main method in RatRace, by

    java RatRace

    a window will show up, prompting me for a map file with the content depicting a maze. After I choose that file, Iwant its content to appear in
    a JFrame window. That is, I want to have sth like this in
    the JFrame window called "Rat Race"

    #######################
    ###....@..#........####
    #####.....#..........##
    #.........#####....#..#
    #####.............##..#
    #..@#.....@...#......@#
    ##..#......####.......#
    #......J...#......##..#
    ####.......@.....###..#
    #@.........###...#....#
    #...###.....#....######
    ##....#....####......@#
    ###.@....#..........#.#
    #....#####..@.....###.#
    #...##########........#
    #.#.@.......##.......@#
    #............@........#
    #..................S..#
    #@........#######.....#
    #.........#######.@...#
    #.........####........#
    #......@..............#
    #######################


    but why do I get this



    Does anyone know why? Thanks for help.

  2. #2
    Join Date
    Oct 2004
    Posts
    311
    First of a little tip:
    Code:
     while ((temp=br.readLine())!= null) {
    for (int c = 0; c < columns ; c++) {
    myMap[r][c] = temp.charAt(c);
    }
    }
    can be done much more efficient by changing it to:"
    Code:
     while ((temp=br.readLine())!= null) {
    myMap[r] = temp.toCharArray();
    }
    (At least, I think that's the method, but you get the idea.)

    second, you could do a printout of the lines you read from the file, and next a printout of th Strings you create to put into the JLabels.

    This way, you can check where things go wrong.
    just start outputting the lot to System.out

  3. #3
    Join Date
    Jan 2005
    Posts
    45
    Thanks for your reply. You are right, a print out will show things clearer. I have some problems with

    for (int c = 0; c < columns ; c++) {
    myMap[r][c] = temp.charAt(c);
    System.out.println(myMap[r][c]); // just added this
    }

    When I run java RatRace, heres the output

    Welcome to DrJava.
    > java RatRace

    #######################

    #

    #

    #

    #

    #

    #

    #

    #

    #

    #

    #

    #

    #

    #

    #

    #

    #

    #

    #

    #

    #

    #

    #

    ###....@..#........####

    #

    #

    #

    .

    .

    .

    .

    @

    .

    .

    #

    .

    .

    .

    .

    .

    .

    .

    .

    #

    #

    #

    #

    #####.....#..........##

    #

    #

    #

    #

    #

    .

    .

    .

    .

    .

    #

    .

    .

    .

    .

    .

    .

    .

    .

    .

    .

    #

    #

    #.........#####....#..#

    #

    .

    .

    .

    .

    .

    .

    .

    .

    .

    #

    #

    #

    #

    #

    .

    .

    .

    .

    #

    .

    .

    #

    #####.............##..#

    #

    #

    #

    #

    #

    .

    .

    .

    .

    .

    .

    .

    .

    .

    .

    .

    .

    .

    #

    #

    .

    .

    #

    #..@#.....@...#......@#

    #

    .

    .

    @

    #

    .

    .

    .

    .

    .

    @

    .

    .

    .

    #

    .

    .

    .

    .

    .

    .

    @

    #

    ##..#......####.......#

    #

    #

    .

    .

    #

    .

    .

    .

    .

    .

    .

    #

    #

    #

    #

    .

    .

    .

    .

    .

    .

    .

    #

    #......J...#......##..#

    #

    .

    .

    .

    .

    .

    .

    J

    .

    .

    .

    #

    .

    .

    .

    .

    .

    .

    #

    #

    .

    .

    #

    ####.......@.....###..#

    #

    #

    #

    #

    .

    .

    .

    .

    .

    .

    .

    @

    .

    .

    .

    .

    .

    #

    #

    #

    .

    .

    #

    #@.........###...#....#

    #

    @

    .

    .

    .

    .

    .

    .

    .

    .

    .

    #

    #

    #

    .

    .

    .

    #

    .

    .

    .

    .

    #

    #...###.....#....######

    #

    .

    .

    .

    #

    #

    #

    .

    .

    .

    .

    .

    #

    .

    .

    .

    .

    #

    #

    #

    #

    #

    #

    ##....#....####......@#

    #

    #

    .

    .

    .

    .

    #

    .

    .

    .

    .

    #

    #

    #

    #

    .

    .

    .

    .

    .

    .

    @

    #

    ###.@....#..........#.#

    #

    #

    #

    .

    @

    .

    .

    .

    .

    #

    .

    .

    .

    .

    .

    .

    .

    .

    .

    .

    #

    .

    #

    #....#####..@.....###.#

    #

    .

    .

    .

    .

    #

    #

    #

    #

    #

    .

    .

    @

    .

    .

    .

    .

    .

    #

    #

    #

    .

    #

    #...##########........#

    #

    .

    .

    .

    #

    #

    #

    #

    #

    #

    #

    #

    #

    #

    .

    .

    .

    .

    .

    .

    .

    .

    #

    #.#.@.......##.......@#

    #

    .

    #

    .

    @

    .

    .

    .

    .

    .

    .

    .

    #

    #

    .

    .

    .

    .

    .

    .

    .

    @

    #

    #............@........#

    #

    .

    .

    .

    .

    .

    .

    .

    .

    .

    .

    .

    .

    @

    .

    .

    .

    .

    .

    .

    .

    .

    #

    #..................S..#

    #

    .

    .

    .

    .

    .

    .

    .

    .

    .

    .

    .

    .

    .

    .

    .

    .

    .

    .

    S

    .

    .

    #

    #@........#######.....#

    #

    @

    .

    .

    .

    .

    .

    .

    .

    .

    #

    #

    #

    #

    #

    #

    #

    .

    .

    .

    .

    .

    #

    #.........#######.@...#

    #

    .

    .

    .

    .

    .

    .

    .

    .

    .

    #

    #

    #

    #

    #

    #

    #

    .

    @

    .

    .

    .

    #

    #.........####........#

    #

    .

    .

    .

    .

    .

    .

    .

    .

    .

    #

    #

    #

    #

    .

    .

    .

    .

    .

    .

    .

    .

    #

    #......@..............#

    #

    .

    .

    .

    .

    .

    .

    @

    .

    .

    .

    .

    .

    .

    .

    .

    .

    .

    .

    .

    .

    .

    #

    #######################

    #

    #

    #

    #

    #

    #

    #

    #

    #

    #

    #

    #

    #

    #

    #

    #

    #

    #

    #

    #

    #

    #

    #

    >

    Any idea how to fix?

  4. #4
    Join Date
    Oct 2004
    Posts
    311
    hhmmzz.. very strange, seems that most of the time charAt(c) returns one character, which is as it should be. and sometimes it returns an entire string of characters....

    try changing it from
    Code:
     for (int r = 0; r < rows; r++) {
    while ((temp=br.readLine())!= null) {
    for (int c = 0; c < columns ; c++) {
    myMap[r][c] = temp.charAt(c);
    }
    }
    to
    Code:
    int line = 0;
    while ((temp=br.readLine())!= null) {
    for (int c = 0; c < temp.length ; c++) {
    myMap[line][c] = temp.charAt(c);
    }
    line++;
    }
    This takes the file as l;eading instead of the array.

    I'm off to work, but will read again when there.

  5. #5
    Join Date
    Jan 2005
    Posts
    45
    Sorry but It It didnt work. Heres the error

    java.lang.ArrayIndexOutOfBoundsException: 23
    at RatRace.main(RatRace.java:35)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)

    It seems so weird to me. I think my idear is correct. Set a grid layout to a JFrame window, bring in a map file which contains of characters, put those characters in to an array. Pass that array to the constructor of the MapWindow. Cant explain why it doesnt work.

  6. #6
    Join Date
    Oct 2004
    Posts
    311
    Why not put the content of the file straight into the labels?
    instead of first pttingiin an array?

  7. #7
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560
    This works. I have modified your code it a bit; used an ArrayList of bytearrays to hold the lines instead of reading the file twice (and the FileDialog since this one remembers the last map visisted).

    One more thing, using labels to draw is not the optimal solution, but you're probably aware of that.

    import java.io.*;
    import java.util.*;
    import javax.swing.*;
    import java.awt.*;

    public class RatRace {

    public static void main(String[] args) throws IOException {
    FileDialog fd=new FileDialog(new JFrame(),"Where is your ratrace map ?",FileDialog.LOAD);
    fd.setVisible(true);
    String fileName=fd.getFile();
    if (fd==null) return; // user bailed out
    String filePath=fd.getDirectory()+fileName;
    BufferedReader read = new BufferedReader(new FileReader(filePath));
    //get the mapfile's lines into an arraylist
    String lineRead = "";
    ArrayList myMap=new ArrayList();
    int maxWidth=Integer.MIN_VALUE;
    int lineCount=0;
    while ((lineRead = read.readLine())!=null) {
    lineCount++;
    byte[] b=lineRead.trim().getBytes();
    if (maxWidth > 0 && b.length != maxWidth) {
    JOptionPane.showMessageDialog(
    null,"A RatRace Maze must be a rectangle, \r\nline: "+
    lineCount+" in "+filePath+" differs in length ("+
    maxWidth+" != "+b.length+")", "you goofed",JOptionPane.ERROR_MESSAGE);
    read.close();
    return;
    }
    maxWidth=(maxWidth < b.length) ? b.length : maxWidth;
    myMap.add(b);
    }
    read.close();
    MapWindow window = new MapWindow(myMap, maxWidth);
    }
    }

    class MapWindow extends JFrame {
    public MapWindow(ArrayList map, int mapWidth){
    JFrame window = new JFrame("Rat Race");
    window.getContentPane().setLayout(new GridLayout(map.size(), mapWidth));
    JLabel[][]label = new JLabel[map.size()][mapWidth];
    for (int r = 0; r < map.size(); r++) {
    for (int c = 0; c < mapWidth ; c++) {
    label[r][c] = new JLabel();
    byte b=((byte[])map.get(r))[c];
    label[r][c].setText(Character.toString((char)b));
    window.getContentPane().add(label[r][c]);
    }
    }
    window.pack();
    window.show();
    }
    }
    Attached Files Attached Files
    eschew obfuscation

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