I’ve a Swing GUI with 50 or so Jlabels:
{
jLabel1 = new JLabel();
getContentPane().add(jLabel1, "0, 0");
jLabel1.setText("AAPL 1453.54 2334.34 3234.32");
jLabel1.setForeground(new java.awt.Color(0,249,0));
}
I want to be able to change the txt and colour without having to write a large bunch of conditional statements. What’s the best way to do this?
I was thinking to write a method overWrite(String text, int hPos, int vPos){} to just add a new label in the place.
There might be a much better way to do this, I’m trying to make a crude stock display that changes stock prices and colour depending on an increase or decrease.
Okey, the solution below is ugly so I just came up with another solution… You can set a name of each of your labels so that you can get a specific label. And you do not need any other structure to hold the information.
You can then iterate (just like the other solution) over the components and get the specific one like this:
Old solution: This is ugly but works.. 🙂
Output: