How to create JLabels with for loop
I want to be able to create JLabels, all different names, with a for loop
ex:
static JLabel[] totalAYearLabelYr = new JLabel[termYears];
So I got 30 JLabels named totalAYearLabelYr, which I'm not sure you can have 30 JLabels named the same thing.
I wanna name them all
totalAYearLabelYr1
totalAYearLabelYr2
totalAYearLabelYr3
totalAYearLabelYr4........as much as the termYears is, etc.
I wanna then setBounds to each Label
for (int i = 0; i < mc.getTermYears(); i++)
for (int v = 295; v <= 1; v += 25)
totalAYearLabelYr[i].setBounds(10, v, 375, 20);
Then add it to the pane (I'm not using a LayoutManager, just setBounds() and pane.add())
for (int i = 0; i < mc.getTermYears(); i++)
pane.add(totalAYearLabelYr[i]);
I'm just assuming errors are because all the JLabels are named the same totalAYearLabelYr name.
I don't want to name them individually like
totalAYearLabelYr1
totalAYearLabelYr2
totalAYearLabelYr3
waaaaayyyy too much code for other things I wanna do, arrays as everyone knows cuts your code to only 2 lines minimum, instead of say 50 lines.
musikluver4
Junior Poster in Training
81 posts since Mar 2010
Reputation Points: 10
Solved Threads: 0
Have you set the JLabels to any value?
Based on the code snippets you've posted, you have instantiated the array, but not the elements in the array.
Try,
for (int i = 0; i < mc.getTermYears(); i++)
for (int v = 295; v <= 1; v += 25)
{
totalAYearLabelYr[i].setBounds(10, v, 375, 20);
totalAYearLabelYr[i].setText("Whatever you want them to be");
}
As far as the naming, you don't have 30 JLabels named the same thing, you have 1 JLabel array. Each individual JLabel has a unique array index, so you should be fine.
It would be easier for me to help you if you posted more of the code. It's kind of hard to figure out exactly what the problem is when you can only see part of the code.
Mattox
Junior Poster in Training
57 posts since Jan 2011
Reputation Points: 10
Solved Threads: 15
I found out it wasn't anything I thought it was. It was something it's trying to grab that has nothing to do with the Labels I'm trying to add.
for (yearNumber = 1; yearNumber <= mc.getTermYears(); yearNumber++)
totalAYearLabel[yearNumber - 1].setText("Year #" + (yearNumber) + "'s Year Total is: " + df0.format(mc.getTotalCost()[yearNumber - 1]));
This line of code, the very last thing, the getTotalCost() is actually the problem. I need to fix that. The error is a NullPointerException, it's just not initialized in my other java file that's all.
musikluver4
Junior Poster in Training
81 posts since Mar 2010
Reputation Points: 10
Solved Threads: 0
musikluver4
Junior Poster in Training
81 posts since Mar 2010
Reputation Points: 10
Solved Threads: 0
Mattox
Junior Poster in Training
57 posts since Jan 2011
Reputation Points: 10
Solved Threads: 15
I made my program small enough to just use the JLabel I'm having trouble with. I did what you guys said, but the Labels are still not being shown...it's just blank with a yellow JPanel.
import java.awt.Color;
import java.awt.Container;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.text.DecimalFormat;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class MortgageTotalsTemp extends JFrame
{
static DecimalFormat df0 = new DecimalFormat("###,###.00");
static MCalcs107 mc = new MCalcs107();
static JLabel[] totalAYearLabel;
JPanel window = new JPanel();
public MortgageTotalsTemp()
{
super("Mortgage Loan Totals");
this.setSize(800,825);
this.setLocation(363,190);
this.setResizable(false);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
setupTotalsLayout(getContentPane());
this.setVisible(true);
this.pack(); // added for better fit of the window
}
public static void main(String[] args)
{
JFrame totalsFrame = new MortgageTotalsTemp();
totalsFrame.setSize(800, 825);
}
public void setupTotalsLayout(Container pane)
{
totalAYearLabel = new JLabel[30];
for (int i = 0; i < 30; i++)
{
totalAYearLabel[i] = new JLabel();
for (int v = 30; v <= 1; v += 25)
{
totalAYearLabel[i].setBounds(10, v, 250, 20);
}
}
window.setBounds(0, 0, 800, 700);
for (int i = 0; i < 30; i++)
pane.add(totalAYearLabel[i]);
pane.add(window);
window.setBackground(new Color(238,221,130));
for (int i = 0; i < 30; i++)
totalAYearLabel[i].setText("Year #'s Total is: ");
}
}
Someone please tell me why even a simple program for just the JLabel array doesn't even do anything?? All I see is yellow, no text at all!
musikluver4
Junior Poster in Training
81 posts since Mar 2010
Reputation Points: 10
Solved Threads: 0
your code probably doesn't works because I think that never call
totalAYearLabel[i].setBounds(10, v, 250, 20);
mKorbel
Veteran Poster
1,141 posts since Feb 2011
Reputation Points: 480
Solved Threads: 224
You are correct, I tried doing the first JLabel outside of the for loop
totalAYearLabel = new JLabel[30];
for (int i = 0; i < 30; i++)
totalAYearLabel[i] = new JLabel();
totalAYearLabel[0].setBounds(10, 30, 250, 20);
for (int i = 1; i < 30; i++)
for (int v = 55; v <= 1; v += 25)
totalAYearLabel[i].setBounds(10, v, 250, 20);
And the only one I saw was the one that wasn't in the for loop........why doesn't the ' v ' work?
musikluver4
Junior Poster in Training
81 posts since Mar 2010
Reputation Points: 10
Solved Threads: 0
So how do I make JLabel array, all of them 25 vertical pixels apart? I changed it to
totalAYearLabel = new JLabel[mc.getTermYears()];
for (int i = 0; i < mc.getTermYears(); i++)
{
totalAYearLabel[i] = new JLabel();
for (int v = 30; v <= 700; v += 25)
{
totalAYearLabel[i].setBounds(10, v, 250, 20);
}
}
Say all the way up to 700, but of course, it's only telling one of them to do it, so at the end, there's only one JLabel, and it's at the bottom of the JFrame. I'm assuming I need another for loop but I can't figure out what i should do
musikluver4
Junior Poster in Training
81 posts since Mar 2010
Reputation Points: 10
Solved Threads: 0
what't wrong with this one
import java.awt.*;
import javax.swing.*;
import javax.swing.border.LineBorder;
public class GridLayoutDemo extends JFrame {
private static final long serialVersionUID = 1L;
private JPanel panel;
private JPanel panelN;
private JPanel panelS;
private JPanel panelE;
private JPanel panelW;
private void addComponentsToPane() {
for (int i = 0; i < 30; i++) {
JLabel lbl = new JLabel();
lbl.setText("JLabel No. " + i);
if (i % 2 == 0) {
lbl.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
lbl.setForeground(Color.red);
lbl.setBorder(new LineBorder(Color.black, 1));
} else {
lbl.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
lbl.setForeground(Color.blue);
}
panel.add(lbl);
}
}
private GridLayoutDemo() {
panel = new JPanel();
panel.setPreferredSize(new Dimension(600, 400));
panel.setLayout(new GridLayout(6, 5, 30, 30));
addComponentsToPane();
panelN = new JPanel();
panelS = new JPanel();
panelE = new JPanel();
panelW = new JPanel();
JFrame frame = new JFrame("GridLayoutDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(panelN, BorderLayout.NORTH);
frame.add(panelS, BorderLayout.SOUTH);
frame.add(panelE, BorderLayout.EAST);
frame.add(panelW, BorderLayout.WEST);
frame.add(panel, BorderLayout.CENTER);
frame.setLocation(200, 150);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
GridLayoutDemo gbd = new GridLayoutDemo();
}
});
}
}
mKorbel
Veteran Poster
1,141 posts since Feb 2011
Reputation Points: 480
Solved Threads: 224
Thanks everyone for all the help. I finally got it. The two programs above were excellent examples to help me. My code I chose to make it work is this:
totalAYearLabel = new JLabel[mc.getTermYears()];
for (int i = 0, v = 320; i < mc.getTermYears() && v < 1055; i++, v+=25)
{
totalAYearLabel[i] = new JLabel();
if (i <= ((mc.getTermYears() / 2) - 1)) //exactly half of the termYears - arrays start at 0, hence - 1
{
if (v % 2 == 0)
totalAYearLabel[i].setBounds(10, v, 250, 20);
else
totalAYearLabel[i].setBounds(10, v, 250, 20);
if (i == ((mc.getTermYears() / 2) - 1))
v = 295;
}
else
{
if (v % 2 == 0)
totalAYearLabel[i].setBounds(280, v, 250, 20);
else
totalAYearLabel[i].setBounds(280, v, 250, 20);
}
pane.add(totalAYearLabel[i]);
}
musikluver4
Junior Poster in Training
81 posts since Mar 2010
Reputation Points: 10
Solved Threads: 0