Update the JLabel's label text during the event - Swing - java

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.

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.

"tab" key event as "enter" key event

I have a JTextField which takes a number. I need to use "tab" key event same like "enter" key event.
JLabel lblPieChart = new JLabel("Maximum pie chart label length:");
GridBagConstraints gbc = new GridBagConstraints();
basePanel.add(lblPieChart, gbc);
// PieChart Label textbox
SwiftApplication APP = SwiftApplication.getInstance();
int nMaxLabel = APP.getMaxPieLabel();
mMaxLabelLength = new JTextField();
mMaxLabelLength.setPreferredSize(new Dimension(200, 24));
mMaxLabelLength.setText(new Integer(nMaxLabel).toString());
GridBagConstraints gbc2 = new GridBagConstraints();
basePanel.add(mMaxLabelLength, gbc2);
mMaxLabelLength.addActionListener(this);
In above code, as soon as I press enter after entering a number, the ActionListener comes into play and does the calculation using the number. I need to do the same thing, if I press "tab". Currently, if I press tab, it goes to next text field without any change.

Java add text to textArea and textField

I am trying to make a JTextArea and a JTextField that has pre-set text in it already so when the user clicks in the box, they can edit it so something else.
Here's what I have for my text field:
private JTextField gridSizeField = new JTextField(6);
and my text area:
private JTextArea status = new JTextArea(20, 20);
private JScrollPane statusScroller = new JScrollPane(status);
You can use one of the overloaded constructors that accepts a String as an argument.
JTextField(String text, int columns)
JTextField gridSizeField = new JTextField("Your Text", 15);
Same for JTextArea
JTextArea(String text, int rows, int cols)
JTextArea status = new JTextArea("Your status", 20, 20);
You should have referred to the JavaDocs first
More on JTextArea and JTextField
In order to have text already set inside the JTextArea, it is a parameter of the JTextArea constructor. For example, to have the string "Text already in JTextArea", you just have to do
private JTextArea status = new JTextArea("Text already in JTextArea", 20, 20);
private JTextField field = new JTextField("Text already in JTextField", 6);
Hope it helps!

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