Justify text inside JOptionPane - java

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);

Related

Java - Validation of TextField

(SOLVED) Issue 1: I am trying to add a simple verification to my 2 TextFields by checking its values. However, with this code below, I think whats happening is that the try/catch is called as the program starts up (which I tested with the System.out.println() code), therefore always resulting in an error. How can I make it such that this is called only after button 'Finish' is pressed?
(UNSOLVED) Issue 2: Following on from my first issue, how can I make it such that if either my if or my try/catch returns an 'error', then pressing the 'Finish' button doesn't end the code?
Code:
Dialog<Pair<String, Integer>> dialog = new Dialog();
dialog.setTitle("Add new values");
dialog.setHeaderText("Please input name and number");
ButtonType finishButton = new ButtonType("Finish", ButtonData.OK_DONE);
dialog.getDialogPane().getButtonTypes().addAll(finishButton, ButtonType.CANCEL);
GridPane grid = new GridPane();
grid.setHgap(10);
grid.setVgap(10);
TextField name = new TextField();
name.setPromptText("Name");
TextField size = new TextField();
number.setPromptText("Number");
grid.add(new Label("Name:"), 0, 1);
grid.add(eventName, 1, 1);
grid.add(new Label("Number:"), 0, 3);
grid.add(eventSize, 1, 3);
dialog.getDialogPane().setContent(grid);
//verification code below
if (eventName.getText() == null || eventName.getText() == "") {
grid.add(new Label("Name is required!"), 0, 0);
}
try {
int size = Integer.parseInt(eventSize.getText());
} catch (NumberFormatException e) {
grid.add(new Label("Size is required!"), 0, 1);
System.out.println("Test failed");
}
This is the code I am trying to learn off from: Here
Firstly, you must compare Strings using the .equals() method. I believe, but am not 100% certain, that the check for null is unnecessary. So, change:
if (eventName.getText() == null || eventName.getText() == "")
to
if (eventName.getText().equals(""))
I am unfamiliar with the Dialog class. However, when I need to implement something like this I like to use JDialog, and put it in a while loop:
JPanel p = new JPanel(new GridLayout(2,2));
JTextField nameField = new JTextField(5);
JTextField numberField = new JTextField(5);
JLabel nameLabel = new JLabel("Name");
JLabel numberLabel = new JLabel("Number");
p.add(nameLabel);
p.add(nameField);
p.add(numberLabel);
p.add(numberField);
while(true){
int result = JOptionPane.showConfirmDialog(null, p, "Please enter Name and Number.", JOptionPane.OK_CANCEL_OPTION);
if(result == JOptionPane.OK_OPTION){
if(nameField.getText().equals("")){
JOptionPane.showConfirmDialog(null, "Invalid input!");
}
else break;
}
}
This code should guide you on how you might be able to check for different inputs, and validate them accordingly. See JOptionPane for more details on the different dialogs you can open.
Hope this helps you.
Dont know if this will help but i made a button that sounds like what your trying to do
//taking input from pop up box
JTextField InputPosX = new JTextField(5);
JTextField InputNegX = new JTextField(5);
JTextField InputY = new JTextField(5);
JPanel ChangeAxisPanel = new JPanel();
ChangeAxisPanel.add(new JLabel("Max X:"));
ChangeAxisPanel.add(InputPosX);
ChangeAxisPanel.add(Box.createHorizontalStrut(15)); // a spacer
ChangeAxisPanel.add(new JLabel("Min X:"));
ChangeAxisPanel.add(InputNegX);
ChangeAxisPanel.add(Box.createHorizontalStrut(15)); // a spacer
ChangeAxisPanel.add(new JLabel("Y:"));
ChangeAxisPanel.add(InputY);
int result = JOptionPane.showConfirmDialog(null, ChangeAxisPanel,
"Please Enter X and Y Values", JOptionPane.OK_CANCEL_OPTION);
//if ok is pressed
if (result == JOptionPane.OK_OPTION) {
if(!(InputPosX.getText().isEmpty())){
defaultPosX=Integer.parseInt(InputPosX.getText());
}
if(!(InputNegX.getText().isEmpty())){
defaultNegX=Integer.parseInt(InputNegX.getText());
}
if(!(InputY.getText().isEmpty())){
defaultY=Integer.parseInt(InputY.getText());
}
}
}
});
most of this was gathered from
Here its a good link for gui input windows. also if you are looking for a simpler method you may want to look into jbutton's you can use it to call this window
Jbutton
anyways hope this helped

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 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:

JOptionPane customize input

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...

Categories