I have tried many different tutorials and none have worked this is what I have. Any help?
UIManager.put("OptionPane.font", new FontUIResource(new Font("Press Start 2P", Font.PLAIN, 11)));
if (questionNumber == questions.size()) {
triviagui.questionFrame.setVisible(false);
JOptionPane.showMessageDialog(null, "Your score for this level was : " + levelScore + " out of 10. \n Your total score is " + triviagui.totalScore, "Scores", JOptionPane.INFORMATION_MESSAGE, pokeballIcon);
}
this is how I change my font in a JLabel, so maybe it is any help?
message = new JLabel(textMessage);
// create bigger text (to-times-bigger)
Font f = message.getFont();
message.setFont(new Font(f.getName(), Font.PLAIN, f.getSize()*2));
// put text in middle of vertical space
message.setVerticalTextPosition(JLabel.CENTER);
You just take the font from your label, and reset the font as you like.
Maybe you can do the same with your JDialog?
I found a working answer here: formatting text in jdialog box
this could be a method called by the actionListener of a button:
public void openPopUp(){
String t = "<html>The quick <font color=#A62A2A>brown</font> fox.";
JOptionPane.showMessageDialog(null, t);
}
Gives you this result:
Related
I have the following line of code
JOptionPane.showMessageDialog(rootPane, "Código incorreto\nPor favor verifique", "Atenção", JOptionPane.ERROR_MESSAGE);
that show this message here
How to center this 2 lines of text in the center of the box?
JLabel label = new JLabel("<html><center>Código incorreto<br>Por favor verifique");
label.setHorizontalAlignment(SwingConstants.CENTER);
JOptionPane.showMessageDialog(null, label, "Atenção", JOptionPane.ERROR_MESSAGE);
Try this one. Preview:
You may get it working with following code:
String message = "<html><body><div width='100px' align='center'>Código incorreto<br>Por favor verifique</div></body></html>";
JLabel messageLabel = new JLabel(message);
JOptionPane.showConfirmDialog(null, messageLabel, "Atenção", JOptionPane.DEFAULT_OPTION);
Is there a way to change the JOptionPane.showMessageDialog font? I'm trying to write code for a message dialog and I need to change the font if it's possible.
JOptionPane jopt = new JOptionPane();
String result;
result = "your message";
JLabel resLabel = new JLabel(result);
resLabel.setFont(new Font("Monospaced", Font.BOLD, 50));
jopt.showMessageDialog( null, resLabel, "Results", JOptionPane.PLAIN_MESSAGE );
Create a new label with your custom font and set the jlabel as a component in joptionpane.
I have a TreeEditor and some text in its first column. I need to append some text to it using a label or string. In addition, I want to change the color of the appended string to green. I tried to do it with this code, but it isn't working:
Label label = new Label(this.tree ,SWT.RIGHT);
label.setFont(new Font(getSite().getShell().getDisplay(), ("Hello"), 18,
SWT.BOLD));
label.setText("hello:));
label.pack();
treeItem.setText(1, hi + " " + label.getText());
How could I accomplish this?
you might want to look at org.eclipse.jface.viewers.DecoratingStyledCellLabelProvider
Currently it looks so
What to do so that it looks so?
Below is my code:
JFrame f = new JFrame();
JTextPane textPane = new JTextPane();
JTextField component = new JTextField(" ");
component.setMaximumSize(component.getPreferredSize());
textPane.setSelectionStart(textPane.getDocument().getLength());
textPane.setSelectionEnd(textPane.getDocument().getLength());
textPane.insertComponent(component);
try {
textPane.getDocument().insertString(textPane.getDocument().getLength(), "text",
new SimpleAttributeSet());
} catch (BadLocationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
f.add(new JScrollPane(textPane));
f.setSize(200, 100);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
The single question which is near to this topic I found: JTextPane insert component, faulty vertical alignment
But there is no answer how to change the alignment. But it must be possible according to the discussion there.
You can use this http://java-sl.com/tip_center_vertically.html
It should work with JComponents as well.
You can also override LabelView's getPreferredSpan() adding some space to the bottom.
Alternatively you can try to override RowView inner class in ParagraphView
http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b14/javax/swing/text/ParagraphView.java#ParagraphView.Row
That points to inner class Row extends BoxView
You should replace it with own one. Try to override
public float getAlignment(int axis)
to return CENTER (0.5). If this does not help override layoutMinorAxis(0 to return proper offsets (shifted).
Define a style for your document with a JLabel and set the vertical aligment on it:
Style s = doc.addStyle("icUf", regular);
ImageIcon icUf = createImageIcon("uf.png", "Unidad Funcional");
if (icUf != null) {
JLabel jl = new JLabel(icUf);
jl.setVerticalAlignment(JLabel.CENTER);
StyleConstants.setComponent(s, jl);
}
Insert the label:
doc.insertString(doc.getLength(), " ", doc.getStyle("icUf"));
and the text:
doc.insertString(doc.getLength(), " text ", doc.getStyle("bold"));
Based on the answer above (which didn't work for me, but helped me find this), I used:
Style s = doc.addStyle("icUf", regular);
ImageIcon icUf = createImageIcon("uf.png", "Unidad Funcional");
if (icUf != null) {
// create label with icon AND text
JLabel jl = new JLabel("some text",icUf, SwingConstants.LEFT);
StyleConstants.setComponent(s, jl);
}
doc.insertString(doc.getLength(), " ", doc.getStyle("icUf"))
This properly aligned the text 'some text' and the icon.
My Java GUI application needs to quickly show some text to the end-user, so the JOptionPane utility methods seem like a good fit. Moreover, the text must be selectable (for copy-and-paste) and it could be somewhat long (~100 words) so it must fit nicely into the window (no text off screen); ideally it should all be displayed at once so the user can read it without needing to interact, so scrollbars are undesirable.
I thought putting the text into a JTextArea and using that for the message in JOptionPane.showMessageDialog would be easy but it appears to truncate the text!
public static void main(String[] args) {
JTextArea textArea = new JTextArea();
textArea.setText(getText()); // A string of ~100 words "Lorem ipsum...\nFin."
textArea.setColumns(50);
textArea.setOpaque(false);
textArea.setEditable(false);
textArea.setLineWrap(true);
textArea.setWrapStyleWord(true);
JOptionPane.showMessageDialog(null, textArea, "Truncated!", JOptionPane.WARNING_MESSAGE);
}
How can I get the text to fit entirely into the option pane without scrollbars and selectable for copy/paste?
import java.awt.*;
import javax.swing.*;
public class TextAreaPreferredHeight2
{
public static void main(String[] args)
{
String text = "one two three four five six seven eight nine ten ";
JTextArea textArea = new JTextArea(text);
textArea.setColumns(30);
textArea.setLineWrap( true );
textArea.setWrapStyleWord( true );
textArea.append(text);
textArea.append(text);
textArea.append(text);
textArea.append(text);
textArea.append(text);
textArea.setSize(textArea.getPreferredSize().width, 1);
JOptionPane.showMessageDialog(
null, textArea, "Not Truncated!", JOptionPane.WARNING_MESSAGE);
}
}
If you need to display a string of an unknown length, you can set number of rows "on the fly":
public static void showMessageDialogFormatted(String msg, String title, int messageType, int columnWidth) {
JTextArea textArea = new JTextArea(msg);
textArea.setColumns(columnWidth);
textArea.setRows(msg.length() / columnWidth + 1);
textArea.setLineWrap(true);
textArea.setEditable(false);
textArea.setWrapStyleWord(true);
JOptionPane.showMessageDialog(null, textArea, title, messageType);
}
You've got the right idea. Just adjust the rows of your textarea.
textArea.setRows(10); // or value that seems acceptable to you...
This seemed to fix the issue for me, using 100 words of lorem ipsum.
Try this:
JTextArea textArea = new JTextArea();
textArea.setText(getText());
textArea.setSize(limit, Short.MAX_VALUE); // limit = width in pixels, e.g. 500
textArea.setWrapStyleWord(true);
textArea.setLineWrap(true);