Question

Hy guys!

I need a little assist on this because I cant really figure it out what I'm doing wrong. I'm working on an envelope writing program. The program have 4 JTextField. Name, City, Addres, Postal Code. I use keylistener on Name JTextField to recognize the Enter button, and add a new Name JTextField. (Basicly, it sets the 2nd Name JTextField Visible). So total you could have 4 Name JTextField and the others (city, address, postal code) total 7. Also has print jbutton, jtable for viewing already written envelopes waiting for printing so on. When I use g.drawString() I use a predefined x,y value which are changing dynamicly according to the Names field's text length measured by Font Metrics' StringWidth() method and calculating with the right side margin value.. whatever. My Main problem is here:

if (name.length() > address.length()) {

            g.drawString(name,
                (250 - nameCord_X) + 46, 214);       //this is working
            g.drawString(city, (250 - nameCord_X) + 46, 226);
            g.drawString(address, (250 - nameCord_X) + 46, 238);
            g.drawString(postal, (250 - nameCord_X + 46, 250);
        } else {
            if (address.length() > name.length()
                && name2.isEmpty())
            { // this is working
                g.drawString(name, (250 - addressCord_X) + 46, 214);
                g.drawString(city, (250 - addressCord_X) + 46, 226);
                g.drawString(address, (250 - addressCord_X) + 46, 238);
                g.drawString(postal, (250 - addressCord_X + 46, 250);

            }
}


                if (name2.length() > name.length()
                    && name2.length() > address.length()
                    && name2.isVisible())
                {// this is working

                    g.drawString(name, (250 - name2Cord_X) + 46, 202);
                    g.drawString(name2, (250 - name2Cord_X) + 46, 214);
                    g.drawString(city, (250 - name2Cord_X) + 46, 226);
                    g.drawString(address, (250 - name2Cord_X) + 46, 238);
                    g.drawString(postal, (250 - name2Cord_X) + 46, 250);

                }
      //This is the part that doesnt working. it prints out 5 lines
      // but the 2nd line is the same as first line,
      // and the real 2nd line value is printed over.
// For example Name: Lion Name2:Lion (altough I typed for
// example horse into name2 field, and the program draw
// the horse string over the 2nd line which contains Lion.)  
//Altough it prints out the all 5 row, it messing only with
//the second row :-/       
                if( (name.length() > name2.length()
                    && name.length() > address.length()
                    && name2.isVisible()))
                {
                    g.drawString(name, (250 - nameCord_X) + 46, 202);
                    g.drawString(name2, (250 - nameCord_X) + 46, 214);
                    g.drawString(city, (250 - nameCord_X) + 46, 226);
                    g.drawString(address, (250 - nameCord_X) + 46, 238);
                    g.drawString(postal, (250 - nameCord_X) + 46, 250);
                   }
Was it helpful?

Solution

You have some complicated if blocks there. Also it is not clear what the types of the variables are. It seems that they are String but e.g. you call isVisible() on name2.

I understand that you are using nameCord_X, addressCord_X, name2Cord_X for alignment purposes. You want all the strings to be left aligned and start the drawing at that point that would allow the largest string to correctly fit so it does not exceed the right side of your envelope. Is that correct?

If yes, then I suggest you first find the maximum of the above lengths. You should take each one into account only if it has a meaningful value. Then you can do the following:

int max = ... // maximum of the above values - only not empty values should be taken into account
int dy = 12;  // it seems you always leave 12 pixels between the lines
int y = ...;  // the value with the topmost y. It seems you use 202 when both names are present 214 when name2 is not given
int x = 250 - max + 46;

String[] labels = new String[] {name, name2, city, address, postal};
for (String label : labels) {
    if (label == null || label.length() == 0) {
        continue; // ignore null or empty values
    }
    g.drawString(name, x, y);
    y += dy;
}    

I hope it helps

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top