0

I'm trying to add my toString() method of Person class to a GUI:

public GUI_Persons() {
    setBounds(100, 100, 450, 300);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    contentPane.setLayout(null);

    JTextPane textPane = new JTextPane();
    textPane.setBounds(0, 38, 434, 223);

    for(Person person : Access.getAllPersons()) {
        textPane.setText(textPane.getText() + person.toString() + "\n");
    }

    contentPane.add(textPane);

    JLabel lblAllPersons = new JLabel("All persons information");
    lblAllPersons.setBounds(10, 11, 188, 16);
    contentPane.add(lblAllPersons);
}

Unfortunaley, only a blank text field is shown. Does somebody have an idea why the text won't be set to the JTextPane component? I also use a method run, because this frame shows up if a user hits the "view persons" button in another frame:

public static void run() {
    try {
        GUI_Persons frame = new GUI_Persons();
        frame.setVisible(true);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

This is the toString() method:

public String toString()
{
    return "ID: " + id + ", Type: " + this.getClass().getSimpleName() +", Name: " + name + ", Forename: " + forename + ", Adress: " + adr.toString();
}

Thanks for all information in advance.

svb
  • 69
  • 7
  • 1
    1) Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). 2) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). – Andrew Thompson May 14 '17 at 16:45
  • 1
    .. 3) *"in another frame"* See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/q/9554636/418556) – Andrew Thompson May 14 '17 at 16:46
  • `for(Person person : Access.getAllPersons()) { textPane.setText(textPane.getText() + person.toString() + "\n"); }` Only the last `Person` will ever appear. Can you figure out why? – Andrew Thompson May 14 '17 at 16:48
  • Please post an image of what you get form running that code. – user1803551 May 15 '17 at 00:21

1 Answers1

0

Hi svb your code works fine, if I change the loop that uses Person, class that I don't have and I put it in a real class :

 public class GUI_Persons extends JFrame {
    public GUI_Persons() {
        setBounds(100, 100, 450, 300);
        JPanel contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        JTextPane textPane = new JTextPane();
        textPane.setBounds(0, 38, 434, 223);

        for (int i = 0; i < 100; i++) {
            textPane.setText(textPane.getText() + personToString(i) + "\n");
        }

        contentPane.add(textPane);

        JLabel lblAllPersons = new JLabel("All persons information");
        lblAllPersons.setBounds(10, 11, 188, 16);
        contentPane.add(lblAllPersons);
    }

    public static String personToString(int i) {
        int id = i;
        String name = "name" + i;
        String forename = "forename" + i;
        String adr = "AAA";
        return "ID: " + id + ", Type: " + ", Name: " + name + ", Forename: "
                + forename + ", Adress: " + adr.toString();
    }
    public static void main(String[] args) {

        try {
            GUI_Persons frame = new GUI_Persons();
            frame.setVisible(true);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

you can try by yourself , so not a swing problem... Very strange... just verify the other parts of your code, or please provide something executable.

navy1978
  • 1,411
  • 1
  • 15
  • 35