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.
Related
I have been trying to change the font of the boxes within showMessageDialog, but I am unsure how to do so. Here's the code I have so far:
Object[] choice = {"Previous", "Next"};
value = JOptionPane.showOptionDialog(
null,
"<html><body><b style='font-family: Sitka Display; font-size: 14px'; <p style='width: 400px;'>" + text,
"Header Test",
JOptionPane.YES_OPTION,
JOptionPane.PLAIN_MESSAGE,
null,
choice,
choice[0]);
Output
Thanks in advance.
You only need to create a JLabel to achieve this.
JLabel label = new JLabel("...");
label.setFont(new Font("Verdana", Font.PLAIN, 14));
And pass it to showOptionDialog as a parameter.
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);
Basically I want change the JLabel's Label text during on-click the button
'Generate PDF Record Book'
From the previous example says:
label.setText("new value");
when I do that, the label value doesn't change at all, please give me some directions, thanks
initialize();
JLabel lblNewLabel = new JLabel("513 k bytes");
lblNewLabel.setBounds(407, 713, 151, 14);
frmViperManufacturingRecord.getContentPane().add(lblNewLabel);
On button Generate PDF Record Book click
JButton btnGeneratePdfHeader = new JButton("Generate PDF Record Book");
btnGeneratePdfHeader.setMnemonic('G');
btnGeneratePdfHeader.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
final JLabel lblNewLabel = new JLabel("513 k bytes");
//java.io.File file = new java.io.File(strdfile);
//lblNewLabel.setSize(file.length());
//System.out.println(file.length());
String fileSize = file.length() + " k bytes";
System.out.println("I am here");
lblNewLabel.setText("new value");
}
});
You are creating a new JLabel when pressing the button and then set the text of that label to "new value"
final JLabel lblNewLabel = new JLabel("513 k bytes");
lblNewLabel.setText("new value");
rather than changing the text of the label on your UI. You will need to call setText("new value") on a reference to the label you've already added to the UI instead. For instance, that label would neeed to be a field in your UI class, eg final JLabel fileSizeLabel and you would set that labels text by calling
fileSizeLabel.setText("new value");
inside the buttons action listener.
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:
All I want to do is have a JOptionPane inputDialog with a JTextArea instead of a JTextField.
I tried putting the JTextArea inside of the Message parameter like so
Object[] inputText = new Object[]{new JLabel("Enter Graph Information"),
newJTextArea("",20,10)};
graphInfo=(String)JOptionPane.showInputDialog(null,
inputText,
"Create Graph",
JOptionPane.PLAIN_MESSAGE,
null,
null,
"");
But it still has the text field at the bottom and I cannot get the text from the JTextArea.
Is there any way to either remove the original text field and get the text from the jtextarea or replace the text field with the text area completely? I'm trying to avoid having to make a custom dialog if possible and this "seems" like something that should be easy to do?
You're on the right lines; you just need to use showConfirmDialog instead of showMessageDialog, which allows you to pass any Component as your "message" and have it displayed within the JDialog. You can then capture the contents of the JTextArea if the user clicks OK; e.g.
int okCxl = JOptionPane.showConfirmDialog(SwingUtilities.getWindowAncestor(this),
textArea,
"Enter Data",
JOptionPane.OK_CANCEL_OPTION)
if (okCxl == JOptionPane.OK_OPTION) {
String text = textArea.getText();
// Process text.
}
If you want to show a JLabel in conjunction with your JTextArea you can create and pass in a JPanel containing both Components; e.g.
JTextArea textArea = ...
JPanel pnl = new JPanel(new BorderLayout());
pnl.add(new JLabel("Please enter some data:"), BorderLayout.NORTH);
pnl.add(textArea, BorderLayout.CENTER);
JOptionPane.show...