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.
Related
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());
I have a QuizApplication which will show up the results upon user finished the test.
I have following piece of code which returns quiz result as string:
public String getResult() {
int score = 0;
String[] choices;
String scoreB = "";
for (int i = 0; i < test.length; i++) {
if (test[i].getCorrect() == test[i].getAns()) {
score++;
} else {
choices = test[i].getChoice();
scoreB += "Question " + (i + 1) + " : " + test[i].getQn() + "\n<html></br>choices[test[i].getCorrect() - 1] + "\n\n";
}
} // end of for loop
return result;
}
And then I want to show the result in the form of MessageDialog, so I used:
JOptionPane.showMessageDialog(null, newQuiz.getResult()
But, I want to add a vertical scrollbar onto the JOptionPane.showMessageDialog. How can I do it?
One of the parameters to showMessageDialog is an Object. If this Object is a Component, it will be added to the dialog's view.
I'm not sure why you want to add JScrollBar to the dialog, but you will need to construct a Component containing all the elements you want to display and pass it to the dialog via the message parameter.
Check out How to use Dialogs for details
As a concrete example of #MadProgrammer's suggestion, this example wraps a JTextArea in a JScrollPane and passes the latter to JOptionPane.showMessageDialog(). Also consider this alternative using HTML/CSS.
So... I'm making a Jeopardy game, and I'm experimenting with better ways of making and adding the buttons so that it looks better in the source code. So to start off, I have a CategoryClass that takes the parameters of a List of QuestionButtons (basically a JButton), the category name and so on. The only trouble I'm having is adding the buttons to a JPanel. Here's the code:
public void addCategoryButtons(JPanel pane) {
JLabel head = this.getCategoryHeading();
pane.add(head);
List<QuestionButton> buttons = this.getCategoryButtons();
for (int i = 0; i < buttons.size(); i++) {
pane.add(buttons.get(i).getButton());
}
}
And here's what I get. Note: the "Test" is the name of the category
As you can see, It shows the last Category I add instead of all of them. Any suggestions?
You need to set some layout manager, for example:
pane.setLayout(new GridLayout(buttons.size(), 1));
for (int i = 0; i < buttons.size(); i++)
{
pane.add(buttons.get(i).getButton());
}
You might want to use a Layout Manager for this task.
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.
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;
}
// ...