0

So I want to add some text to a window.

I added the text in a ArraList like this:

ArrayList<String> Text = new ArrayList<String>();
Text.add("text1");
Text.add("text2");
...
Text.add("text*n*");

I now want to add these items into a JFrame. Now, I am pretty new to programming, so there is probably a better solution than this. But here is what I tried (I am using a for loop, because I think this is also the easiest way for me to manage the bounds of the labels:

for(int i = 0; i<Text.size();i++){
    JLabel jl = new JLabel(names.get(i));

    jl.setBounds(50,100+20*i,200,50);
    this.add(jl);

}

But only the last element in the ArrayList is added to the JFrame (text*n*). Not all of them. How can I get every element in the arraylist to show in the jframe? Maybe I shouldn't use JFrame?

1
  • Why are you using names.get(i) rather than Text.get(i)? Commented Feb 2, 2014 at 16:48

5 Answers 5

3

It sounds like you want to use a JList, not a grid of JLabel.

i.e.,

DefaultListModel<String> listModel = new DefaultListModel<String>();
JList<String> myJList = new JList<String>(listModel);

// assuming you have an array of String or ArrayList<String> called texts
for (String text : texts) {
  listModel.addElement(text);
}

JScrollPane listScrollPane = new JScrollPane(myJList);

// then add the listScrollPane to your GUI

Also:

  • Please learn and follow Java naming rules. Variable names should begin with a lower case letter, so "text" not Text.
  • And you should know that every time someone uses a null layout and setBounds(...) a puppy dies. Please don't be cruel to puppies, and don't create rigid hard to maintain and upgrade GUI's. Learn and use the Swing layout managers. You won't regret this, and neither will the puppies.
Sign up to request clarification or add additional context in comments.

Comments

2

You need a layout, otherwise they are added on top of each other. Try adding everything to a JPanel and only add the panel to the frame at the end.

JFrame frame = new JFrame("title");
JPanel panel = new JPanel();
// Y_AXIS means each component added will be added vertically
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));

for (String str : Text) {
   JLabel j1 = new JLabel(str);
   panel.add(j1);
}

frame.add(panel);
frame.pack();
frame.setVisible(true);

2 Comments

Thank you. One final question. Everything is added to the top left corner. How can I choose were to put it?
Depends where/how you want to puts things. Without knowing this I can't really help much. There are a bunch of other layout managers that you can choose: layouts
0

Try with Replace names.get(i) as Text.get(i)

OR Try,

for(String str : Text){
    JLabel jl = new JLabel(str);
}

Comments

0

Try this:

String output = "";
for(int i = 0; i<Text.size();i++)
{
    output += Text.get[i] + "\n";

}
JLabel jl = new JLabel(output);

This will create a string named output that will add the text from each index, followed by creating a new line. Then at the end, the full string will be added to the JLabel. The only downside to this method is that one label will contain ALL of the text.

Comments

0
JLabel jl = new JLabel(names.get(i));

Here you always construct a new object for j1, and after the loop you just have the latest object.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.