Change Font of JOptionPane showMessageDialog Boxes - java

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.

Related

Is there a way to change JOptionPane.showMessageDialog font?

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.

Java GUI - JOptionPane/JDialog customization issue

So I'm trying to make a simple dialog where the user can input some information... My problem is that I'm trying to make the whole background white; I got MOST of it, but there's a gray line behind the buttons that I don't know how to fix (make white as well). How can I fix it? :(
What it looks like:
What I want:
Code:
JPanel all = new JPanel();
all.setLayout(new BorderLayout());
all.add(names, BorderLayout.NORTH);
all.add(academic, BorderLayout.CENTER);
all.setBackground(Color.WHITE);
all.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20)); //int top, int left, int bottom, int right
Object [] options = {"SAVE", "EXIT"};
JOptionPane jop = new JOptionPane(all, JOptionPane.PLAIN_MESSAGE , JOptionPane.YES_NO_OPTION, null, options, null);
final JDialog dialog = jop.createDialog(null, "Username Information");
jop.setBackground(Color.WHITE);
dialog.setBackground(Color.WHITE);
dialog.setLocation(585, 300);
dialog.setVisible(true);
String choice = (String) jop.getValue();

how to set font weight in Java for Swing components

I want to set different font weights for components on my JFrame dialog. How do I do this?
In the below Java statement
setFont(new Font("Dialog", Font.BOLD, 12));
when I use Font.BOLD it is too bold and when I use Font.Plain it is too plain. I want something in-between.
welle is partially correct. You can use TextAttributes to obtain a font:
Map<TextAttribute, Object> attributes = new HashMap<>();
attributes.put(TextAttribute.FAMILY, Font.DIALOG);
attributes.put(TextAttribute.WEIGHT, TextAttribute.WEIGHT_SEMIBOLD);
attributes.put(TextAttribute.SIZE, 12);
label.setFont(Font.getFont(attributes));
A better approach is to derive your font from the font installed on the Swing component by the look-and-feel:
Font font = label.getFont();
font = font.deriveFont(
Collections.singletonMap(
TextAttribute.WEIGHT, TextAttribute.WEIGHT_SEMIBOLD));
label.setFont(font);
That will preserve the font's family and size, which users may have set in their desktop preferences for readability reasons.
maybe i wrong but i think class Font has only Bold ,plain but you can change after that in number
setFont(new Font("Dialog", Font.BOLD, 12));
setFont(new Font("Dialog", Font.plain, 27));
but in class java.awt.font.TextAttribute
you have WEIGHT_BOLD and WEIGHT_SEMIBOLD ...
The solution is to load by name the variant of the font, for example:
Font font = new Font("Segoe UI Semibold", Font.PLAIN, 12);

How to change font with showconfirmdialog?

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:

Jdatechooser text alignement to RIGHT

I need to align the text in Jdatechooser text field. It always aligns the text(the selected date) to LEFT but i need at RIGHT side.
I have tried this but its not working,
StartJCal.setAlignmentX(RIGHT_ALIGNMENT);
The setTextAlignment method is not available for Jdatechooser.
StartJCal = new JDateChooser();
StartJCal.setDateFormatString("yyyyMMdd");
StartJCal.setFont(new Font("Dialog", Font.PLAIN, 11));
StartJCal.setSize(new Dimension(105, 0));
This is the piece of code am using.How can i align the text please help
StartJCal = new JDateChooser();
JTextFieldDateEditor dateEditor = (JTextFieldDateEditor)StartJCal.getComponent(1);
dateEditor.setHorizontalAlignment(JTextField.RIGHT);
StartJCal.setDateFormatString("yyyyMMdd");
StartJCal.setFont(new Font("Dialog", Font.PLAIN, 11));
StartJCal.setSize(new Dimension(105, 0));
try this. it will solve your problem
Similar but not passing through getComponent()
StartJCal = new JDateChooser();
JTextFieldDateEditor dateEditor = (JTextFieldDateEditor)StartJCal.getDateEditor();
dateEditor.setHorizontalAlignment(JTextField.RIGHT);
StartJCal.setDateFormatString("yyyyMMdd");
StartJCal.setFont(new Font("Dialog", Font.PLAIN, 11));
StartJCal.setSize(new Dimension(105, 0));

Categories