1

I am creating a game, and I am doing a character selection screen which has a JTextField for entering the user name, the screen also has a JButton "Create Character", which, when pressed will parse the JTextField and if any problems(space in name, begin with space, etc..) it will put some text to a JLabel next to the JTextField.

I hook up the JButton to an actionPerformed method, which calls a function that parses the data. My problem is that every time I press the button, a new label is being placed on top of the original JLabel.

I create the JLabel like this:

public static JLabel label;
label = new JLabel();
// some properties

And I call the parsing method like so:

parseCharacterSelection(nameInput, label);

where nameInput is the JTextField. My question is, why is there a new Label being created every time I press the JButton

public void parseCharacterCreation(JTextField input, JLabel label) {

    // Variable that hold whether or not
    // the User Name is acceptable

    player_name = input.getText();

    // Make some initial checks on the
    // input string
    //----------------------------------
    if( player_name.isEmpty() ) {
        label.setText("You need to pick a username");
    } else if( player_name.startsWith(" ") ) {
        label.setText("Name Cannot start with a space");
    } else {
        pass = true;
    }

    // Attempt to write to file
    // Catch some errors
    //----------------------------------
    if(pass == true) {
        try {
            if( player_name.contains(" ") ) {
                player_name = player_name.replaceAll("\\s", "_");
                player_file = new File("src/Resources/characters/" + player_name + ".properties");
            }

            if(!player_file.exists()) {
                player_file.createNewFile();
            }

            FileWriter      fw = new FileWriter(player_file.getAbsoluteFile());
            BufferedWriter  bw = new BufferedWriter(fw);

            bw.write(player_name);
            bw.close();

        } catch( IOException e ) {
            e.printStackTrace();
        } finally {
            System.out.println("DONE");
        }
    }
}

// ScreenCharacter Class

public class ScreenCharacter extends JPanel {

public static JButton       create;
public static JTextField    nameInput;
public static JLabel        errorLabel;

public ScreenCharacter() {

    this.setLayout(null);
    this.add(createUserName());
    this.add(createMainArea());
}

private static JPanel createUserName() {

    MatteBorder matteBorder = new MatteBorder(1, 0, 1, 0, Color.decode("#222222"));
    EmptyBorder emptyBorder = new EmptyBorder(10, 75, 10, 10);
    CompoundBorder border   = new CompoundBorder(matteBorder, emptyBorder);

    JPanel userNameArea     = new JPanel();
    userNameArea.setLayout(new FlowLayout(FlowLayout.LEFT, 0, 0));
    userNameArea.setBounds(0, 35, Engine.screenX, 50);
    userNameArea.setBorder(border);
    userNameArea.setBackground(new Color(255, 255, 255, 70));

    JLabel nameLabel        = new JLabel();
    nameLabel.setText("Enter Your Username: ");

    nameInput = new JTextField();
    nameInput.setForeground(Color.black);
    nameInput.setBorder(BorderFactory.createEmptyBorder(0, 5, 0, 0));
    nameInput.setPreferredSize(new Dimension(200, 25));

    errorLabel = new JLabel();
    errorLabel.setText("This is a test label");
    errorLabel.setBorder(BorderFactory.createEmptyBorder(0, 20, 0, 0));
    errorLabel.setPreferredSize(new Dimension(200, 25));
    errorLabel.setAlignmentX(JLabel.LEFT_ALIGNMENT);

    userNameArea.add(nameLabel);
    userNameArea.add(nameInput);
    userNameArea.add(errorLabel);


    return userNameArea;
}

private static JPanel createMainArea() {

    JPanel resetCreatePanel = new JPanel(new GridLayout(1, 2));
    resetCreatePanel.setPreferredSize(new Dimension(300, 30));
    resetCreatePanel.setBackground(Color.black);

    create  = new JButton("Create Character");
    create.setBorder(BorderFactory.createLineBorder(Color.decode("#171717")));
    create.setFont(new Font("Dialog", 1, 11));
    create.setBackground(Color.black);
    create.setForeground(Color.white);
    create.setFocusPainted(false);
    create.setContentAreaFilled(false);
    create.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent arg0) {
            if(!errorLabel.getText().isEmpty()) {
                errorLabel.setText("");
            } else {
                Engine.p_parser.parseCharacterCreation(nameInput, errorLabel);
            }
        }
    });

    resetCreatePanel.add(create);


}

}

// Engine

public class Engine {
    public static PlayerParser p_parser;
    public Engine() {
        p_parser = new PlayerParser();

    }
}
user2444217
  • 581
  • 2
  • 7
  • 16
  • 1
    We'd have to see the code that's doing the parse to say why it's doing something unexpected during the parse, wouldn't we? – Nathaniel Waisbrot Jun 08 '13 at 20:02
  • @NathanielWaisbrot yes we would :) – user2444217 Jun 08 '13 at 20:07
  • OK `parseCharacterSelection` looks fine. The two lines you post about creating JLabel aren't valid; can you post the actual context for `label = new JLabel()`? – Nathaniel Waisbrot Jun 08 '13 at 20:13
  • you say it happens when you click the button. can you show the code that runs after the button is clicked ? code of actionPerformed method and every other method that is called from that code (if therere are) –  Jun 08 '13 at 20:14
  • @NathanielWaisbrot alright, there it is – user2444217 Jun 08 '13 at 20:19
  • @user2444217 you really have the `JLabel` declaration on the line immediately preceding where you initialize it? I can't compile that code. – Nathaniel Waisbrot Jun 08 '13 at 20:29
  • @NathanielWaisbrot so, it's part of a class, and the class has a method which creates the JLabel. I wanted to minimize the code I put here, so I cut some parts out which I thought wouldn't affect the problem. – user2444217 Jun 08 '13 at 20:36
  • 2
    Your problem is that you say you keep creating new JLabels. So the code where you call `new JLabel()` is the most important part of your questions. – Nathaniel Waisbrot Jun 08 '13 at 20:37
  • @NathanielWaisbrot I made another edit, that has the complete code for the Jlabel – user2444217 Jun 08 '13 at 20:55

1 Answers1

2

Thanks everyone for the help. The "problem" was that the JPanel that the components were sitting on had a semi-transparent background. And JComponents with transparency get messed up somehow, which made it appear like there are multiple labels being created. But it works fine without transparency.

user2444217
  • 581
  • 2
  • 7
  • 16