I am working on a project that tests Java programs using JUnit5. And in this case, I want to verify that when a button is clicked, it will really open an input dialog.
private void updateTextButtonActionPerformed(java.awt.event.ActionEvent evt) {
String newString = JOptionPane.showInputDialog("Enter a string.");
}
The only way I can think of that the input dialog function has really been implemented is when I can simulate on my test that during button click, an input dialog has been opened.
So the test will be successful if an input dialog has been opened on button click, while failure if not.
UPDATE:
The Mock function of JOptionPane:
#Test
public void shouldShowInputDialogOnUpdateTextButtonClick() {
try (MockedStatic<JOptionPane> jOptionPane = mockStatic(JOptionPane.class)) {
// TODO: Check whether 'showInputDialog' has been called. But how?
}
updateTextButton = (JButton) TestUtils.getChildNamed(activity1, "updateTextButton");
updateTextButton.doClick(); // Performs updateTextButtonActionPerformed() function
}
Related
If you have ever used Window.alert("msg"); API in GWT to show popup, I am not sure but the call to this API pauses the code execution until a user action is taken (cliking the ok button), Simillar to that i have created a custom popup, when it is shown i don't want the code to execute further till any user input in received on the popup, How can i pause the code execution further?
Assume :-
//Some Code
MY Popup (Here i want to wait till a user action is received.)
//Some code
I read somewhere to use Synchronized key word but that didn't work either,Do you have answer to this. How GWT compiler sees "Synchronized" keyword does it ignores the keyword?
Create something like a ConfirmCallBack that you fire when the "OK" button (or whatever) is clicked in the popuppanel.
//method in your own popup class
public static void confirm(String message, ConfirmCallBack confirmCallBack)
{
Button confirmButton = new Button(confirmButtonText, event ->
{
confirmCallBack.callback(true);
//hide popup
});
}
Than also have the ConfirmCallBack interface like
public interface ConfirmCallBack
{
void callback(boolean result);
}
Then call your own popup like
MyPopup.confirm("Hello world", result ->
{
if (result)
{
//my code to be executed after clicking the ok button
}
}
I would really appreciate your help;
I'm using java (netbeans ide), i'm working with filechooser, when i choose a directory, i need to display it's path on a jtextfield. However nothing appears until the program is over (untill all the files of the directory are parsed and treated), I would like it to appear as soon as the program starts.
Please help me out, here is my code:
JFileChooser fch = new JFileChooser("C:\\");
fch.addChoosableFileFilter(filter);
fch.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
int ret = fch.showOpenDialog(null);
int apr=0;
if (ret==JFileChooser.APPROVE_OPTION)
{
apr=1;
jTextField1.setText(fch.getSelectedFile().toString());
}
else jTextField1.setText("Nothing clicked!!!");
.......... the rest of the code .........
when I don't click the msg appears, yet when i do, the path won't apprear till after the program is finished
The code of JFileChooser... probably resides in an ActionListener. This is handled on the sole event handling thread. So do an invokeLater.
#Override
public void actionPerformed(ActionEvent event) {
...
EventQueue.invokeLater(new Runnable() { // Added
... rest of the code
}); // Added
}
Here I think "rest of the code" might be causing the delay, but you might try differently.
I want to make a confirm function in java just like a alert in javascript.
Example javascript code:
var a = prompt("");
In java i have a function that returns a string with a dialog box input;
public static boolean log ;
public String value;
public String androidPrompt(){
log = true;
showMyDialog();
while(log){
}
return value;
}
public void showMyDialog(){
log= false;
value = //inputed value from dialog;
}
But my application don't respond. What should i do. I want to pause
androidPrompt() while showMyDialog() is not done. and when showMyDialog()
is done androidPrompt() function will resume and return the value
Example javascript code: var a = prompt("");
Android does not support that sort of blocking UI call. Instead, use listeners to notify something within your app when the user has accepted the dialog (e.g., pressed the OK button).
What is the best practice for subscribing to events from another JFrame? For example, I have a "settings" form, and when the user presses okay on the settings form, I want the main form to know about this so it can retrieve the settings.
Thanks.
Here is my ideal interface:
public void showSettingsButton_Click() {
frmSettings sForm = new sForm(this._currentSettings);
//sForm.btnOkay.Click = okayButtonClicked; // What to do here?
sForm.setVisible(true);
}
public void okayButtonClicked(frmSettings sForm) {
this._currentSettings = sForm.getSettings();
}
Someone publishes an Event, that something has changed, here the settings. A subscriber that registered for this specifig event, gets notified about it and can do his work, here get the settings. This is called publisher/subscriber.
For this you can use Eventbus or implementing something smaller on your own.
One approach is to have only a single JFrame. All the other 'free floating top level containers' could be modal dialogs. Access the the main GUI will be blocked until the current dialog is dismissed, and the code in the main frame can check the settings of the dialog after it is dismissed.
For anyone interested, here is what I ended up going with. I'm not sure if it's the best way, but it is working for my purposes.
// Method called when the "Show Settings" button is pressed from the main JFrame
private void showSettingsButton_Click() {
// Create new settings form and populate with my settings
frmSettings sForm = new frmSettings(this.mySettings);
// Get the "Save" button and register for its click event...
JButton btnSave = sForm.getSaveButton();
btnSave.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent evt) {
SaveSettings(sForm);
}
});
// Show the settings form
sForm.setVisible(true);
}
// Method called whenever the save button is clicked on the settings form
private void SaveSettings(frmSettings sForm) {
// Get the new settings and assign them to the local member
Settings newSettings = sForm.getSettings();
this.mySettings = newSettings;
}
And if, like me, you are coming from a .NET perspective, here is the C# version:
private void showSettingsButton_Click(object sender, EventArgs e)
{
frmSettings sForm = new frmSettings(this.mySettings);
sForm.btnSave += new EventHandler(SaveSettings);
sForm.Show();
}
private void SaveSettings(object sender, EventArgs e)
{
frmSettings sForm = (frmSettings)sender; // This isn't the exact cast you need..
Settings newSettings = sForm.Settings;
this.mySettings = newSettings;
}
hi i have a full screen program which i dont want people to close unless they have a password i have this code at the moment
public void windowClosing(WindowEvent arg0)
{
System.out.println("HERE");
String inputValue = JOptionPane.showInputDialog("Please input the closeword");
if (inputValue != "closeplz")
{
}
}
in the if statement i want it to stop the method so that the program doesent close. any help would be greatly aprecheated thanks ste
You have to call
setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
on (or within) the JFrame instance. Then the frame will not close unless you do it manually, though windowClosing() will still be called. Inside it, you can then conditionally call
System.exit(1);
which will end the application. Be sure to do any necessary cleanup first.
Check out Closing an Applicaton for a simple class to help you with this. You would need to provide the custom close action that prompts the user for the password.
Using your simple example the code would be:
Action ca = new AbstractAction()
{
public void actionPerformed(ActionEvent e)
{
JFrame frame = (JFrame)e.getSource();
String inputValue = JOptionPane.showInputDialog("Please input the closeword");
if (! inputValue.equals("closeplz"))
{
frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
}
}
};
CloseListener cl = new CloseListener(ca);