How to add multiple lines in Label JavaFX - java

I have a problem that I don't really know how to add a multiple lines into Label in JavaFX.
For example:
Label label = new Label();
for(int i= 0; i<10; i++){
label.setText(Integer.toString(i));
}
So when the loop finishes, the label just only shows the final value which is 9.
So any solutions that can show all the numbers 1 - 9 with the break lines( such as '\n') between them.
This problem that happens when i want to show the Bill of my project that contain many dishes.
Thank you for your help.

You need to append and not set the text over and over again
AND you need the new line character '\n'
my suggestion would be like using a variable to append the information and when you are done with that step, then set the label.text
Example:
StringBuilder msg = new StringBuilder():
Label label = new Label();
for (int i = 0; i < 10; i++) {
msg.append(Integer.toString(i));
msg.append(",\n"); //this is the new line you need
}
label.setText(msg.toString());

Related

removing multiple buttons with the same name

Im adding buttons for a game but when removing the button in a loop it will only get rid of one button even though i added them in the same way
for(int i=0 - 1; i < 4 ; i++) {
panelButtonsub.remove(buttonBlankItems);
}
panelButtonsub.setLayout (new GridLayout (intLayout,intLayout));
revalidate();
repaint();
If you want to remove all the buttons in the panel you can use:
panel.removeAll();
If you want to remove the first 4 buttons in the panel you can use:
for (int i = 0; i < 4, i++)
panel.remove(0);
If you are trying to remove a certain type of component from the panel then you need to start at the end to remove the components:
int components = panel.getComponentCount();
for (int i = components - 1; i >= 0; i --)
{
Component c = panel.getComponent(i);
if (c instance of BlankButton)
panel.remove(i);
}
Where BlankButton is the component you created to represent the extra space by using panel.add( new BlankButton(...) ).
If you are trying to do something else then you need to clarify your question.
You must have distinct instances for each buttonBlankItems button. I guess that you are adding the same button 5 times and then you are trying to remove them.

Java Default Highlighter

Im using the DefaultHightlighter.DefaultHightlighterPainter to highlight text within a java text pane.
I want to remove all highlights (there could be more than one string highlighted) and want it to return the locations of the strings where the highlight has been removed, so obviously I cant use
pseudoCodeTextPane.getHighlighter().removeHighlight(highlight);
Can anyone help?
Thanks
How about something like
Highlighter.Highlight[] highlights = pseudoCodeTextPane.getHighlighter().getHighlights();
int[] startOffsets = new int[highlights.length];
int[] endOffsets = new int[highlights.length];
for (int i = 0; i < highlights.length; ++i) {
startOffsets[i] = highlights[i].getStartOffset();
endOffsets[i] = highlights[i].getEndOffset();
}
pseudoCodeTextPane.getHighlighter().removeAllHighlights();
// now do whatever processing you want to do with the highlight locations
If you remove all highlights (I suppose with removeAllHighlights) you can getHighlights before that and use the information you receive there.

Java (GUI) adding JButton multiple times?

Im learning Java and Im creating a memory type game where you have to find two equal cards.
I have created a Window etc etc but my problem is adding multiple JButtons to it. (my cards are JButtons with icons). I have commented my code where my problem is.
//Get the images.
private File bildmapp = new File("bildmapp");
private File[] bilder = bildmapp.listFiles();
//My own class extending JButton
Kort[] k = new Kort[bilder.length];
for(int i = 0; i < bilder.length; i++){
k[i] = new Kort(new ImageIcon(bilder[i].getPath()));
}
//Later in my code:
int sum = rows * columns;
Kort[] temp = new Kort[sum];
//My function to randomize.
Verktyg.slumpOrdning(k);
//***********************//
//Trying to fill a array from K (which contains all cards) so my temp contains SUM cards and SUM/2 pairs
for(int i = 0; i < sum/2; i++){
temp[i] = k[i];
temp[i+sum/2] = k[i];
}
//Problem is that i only get SUM/2 (half of the cards) cards, not the 16 (8 pairs) i would like to add in this case
//SYNLIGT = VISIBLE.
for(int i = 0; i < sum; i++){
temp[i].setStatus(Kort.Status.SYNLIGT);
j.add(temp[i]);
}
Your code ends up adding each Kort object to the container twice, since the array temp contains two references to each Kort. When you add a Kort a second time, it moves to the second location. A Component can only appear in one place at a time.
You may not add the same widget twice. You need two separate buttons (but you may use the same icon on both).
You have to create sum JButton objects not sum/2; otherwise 2 buttons are the same and therefore only displayed once.

how to create TextFields dynamically in j2me?

we are developing Mobile application in j2me.In my application, we are using TextField and some other controls in Form.Here, my problem is i want to dynamically create TextField based on User's Credentials.For Example, If Manager is entered,then i want to create certain TextField(based on Manager Selection) for getting input from the Manager.Otherwise,i just want to create TextField that are less than the Manager TextField.
How to Create TextFields Dynamically...
For example like this...
int userSelection=10;
for(int i=0;i<userSelection;i++)
TextField text=new TextField("Some Name",null);
here, our problem is,
I want to create TextField With Different Name...
Please guide me to get out of this issue...
Create the TextField array and refer from array index.
TextField[] textFields = new TextField[10];
for (int i = 0; i < textFields.length; i++) {
textFields[0] = new TextField(label, text, maxSize, constraint);
}
after you use correct parameters to construct TextField, code might look like
import javax.microedition.lcdui.TextField;
import java.util.Vector;
// ...
Vector newTextFields(int userSelection) {
// neither List nor generics in midp sorry
final int MAX_SIZE = 42;
final Vector list = new Vector();
for(int i=0; i < userSelection; i++) {
list.addElement(new TextField("Name #" + i, null,
MAX_SIZE, TextField.ANY);
}
return list;
}
// ...

JOptionPane in Java

Does anyone know why tab (\t) does not work with JOptionPane.showMessageDialog?
My code is as follows:
String addText = "NAME\t\tADDRESS\t\tTEL.No\tEMAIL\n";
for (int i = 0; i < addressBookSize; i++) {
addText = addText+entry[i].viewAllInfo();
}
System.out.print(addText);
JOptionPane.showMessageDialog(null, addText);
Are there other ways to align text in JOptionPane?
Put your tabbed text into JTextArea
String addText = "NAME\t\tADDRESS\t\tTEL.No\tEMAIL\n";
for (int i = 0; i < addressBookSize; i++) {
addText = addText+entry[i].viewAllInfo();
}
System.out.print(addText);
JOptionPane.showMessageDialog(null, new JTextArea(addText));
Looking at your data again, I'd probably display it in a JTable, and then if desired, would display this in a JOptionPane or in a GUI. If you need simpler, then display it in a JTextArea whose font has been set to monospaced, and use String.format(...) or something similar to allow your Strings to be displayed in a table.

Categories

Resources