Customized Message Box - Vision

Hi
I would like to have a message box with no ok button.
And also i would like to customize it to look like a toastr.
Is there any options available in ignition?

Yes. You can make a custom JOptionPane
Example:

from javax.swing import JOptionPane
JOptionPane.showOptionDialog(None, "What Choices do we have?", "Not Okay!", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, None, ["Not Okay!", "Really Not Okay!"], "Confirm")

image

Edit:
Additional information Here:

Is there any way to have a messagbox with no option to be selected?

Yes. Just leave the choice list blank

from javax.swing import JOptionPane
JOptionPane.showOptionDialog(None, "There are no options!!!", "What are the options???", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, None, [], "Confirm")

image

Is it possible not to have the close option and close the message box within few secs

Is it possible not to have the close option and close the message box within few secs

I imagine so, but I’ve never attempted it.

By the time you get to no buttons and no close option, you have yourself a popup. :man_shrugging:

1 Like

@JordanCClark

What you’re saying makes perfect sense, but can you blame me? Who could resist the opportunity to make goofy custom message boxes?

1 Like

Okay, I attempted it using some of the guidance from @zxcslo.

This solution is tested and works:

def disposeDialog():
    def closeWindow():
		from javax.swing import SwingUtilities
		parent = event.source
		parent = SwingUtilities.getRoot(parent)
		getWindows = parent.getWindows()
		for disposableWindow in getWindows:
			try:
				if disposableWindow.title == "Options":
					disposableWindow.setVisible(False)
			except:
				pass
    system.util.invokeLater(closeWindow,3000)
    from javax.swing import JOptionPane
    JOptionPane.showOptionDialog(None, "Problem message", "Options", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, None, [], "")        
system.util.invokeAsynchronous(disposeDialog)

It opens a custom dialog box and closes it automatically after three seconds.

The key information is the title. So in the if statement: if disposableWindow.title == "Options":

The title "Options" comes from the third argument of the JOptionPane.showOptionDialog() command.

Edit: Updated deprecated .hide() method to .setVisible(False)

Hi,
Thanks for the suggestion.
I am using the button as a template.
So I have included the given script in custom method.



Is it right to use like this?

Yes. It will work as expected. However, a couple of things will need to be changed.
1. In your actionPerformed script, change self.disposeDialog() to event.source.disposeDialog()
2. in your disposeDialog custom method, the upper definition will still be required, and everything will have a minimum of one tab spacing. Also parent = event.source will need to be changed to parent = self
All together, the custom method script will look like this:

def disposeDialog(self):
	"""
	Arguments:
		self: A reference to the component instance this method is invoked on. This argument
		  is automatic and should not be specified when invoking this method.
	"""
	def disposeDialog():
	    def closeWindow():
			from javax.swing import SwingUtilities
			parent = self
			parent = SwingUtilities.getRoot(parent)
			getWindows = parent.getWindows()
			for disposableWindow in getWindows:
				try:
					if disposableWindow.title == "Options":
						disposableWindow.setVisible(False)
				except:
					pass
	    system.util.invokeLater(closeWindow,3000)
	    from javax.swing import JOptionPane
	    JOptionPane.showOptionDialog(None, "Problem message", "Options", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, None, [], "")        
	system.util.invokeAsynchronous(disposeDialog)

Edit: Updated deprecated .hide() method to .setVisible(False)

Another thing of note, I used the .hide() method from the JComponent class to close the window, but this has been deprecated. I should have used the more current .setVisible(False), so if you’ve already implemented the code I originally posted, you will probably want to change this accordingly.

Hey Justin,

Thank you so much… It works!!!.
Is it possible to change background color, size??

1 Like

Yes, but you will have to add a JPanel to the dialog box. Here is an example using your custom method:

def disposeDialog(self):
	"""
	Arguments:
		self: A reference to the component instance this method is invoked on. This argument
		  is automatic and should not be specified when invoking this method.
	"""
	def disposeDialog():
		def closeWindow():
			from javax.swing import SwingUtilities
			parent = self
			parent = SwingUtilities.getRoot(parent)
			getWindows = parent.getWindows()
			for disposableWindow in getWindows:
				try:
					if disposableWindow.title == "Options":
						disposableWindow.setVisible(False)
				except:
					pass
		system.util.invokeLater(closeWindow,3000)
		from javax.swing import JOptionPane
		from javax.swing import JPanel
		from javax.swing import JLabel
		from java.awt import Dimension
		from java.awt import BorderLayout
		from java.awt import Color
		from java.awt import Font
		text = JLabel("Problem message", JLabel.CENTER)
		text.setForeground(Color.white)
		text.setFont(Font("Serif", Font.BOLD, 48))
		panel = JPanel()
		panel.setPreferredSize(Dimension(500,500))
		panel.setLayout(BorderLayout())
		panel.add(text,BorderLayout.CENTER)
		panel.setBackground(Color.black)
		pane = JOptionPane
		pane.showOptionDialog(None, panel, "Options", JOptionPane.YES_NO_OPTION, JOptionPane.PLAIN_MESSAGE, None, [], "")				
	system.util.invokeAsynchronous(disposeDialog)

The proceeding code produces the following result:

1 Like

It shouldn’t be too difficult to tweak the parameters in that code to meet your usage needs.

Thank you so much Justin

1 Like

Hey Justin,

Is this possible to have this code in the project library, so that i can use it over the prject whenever i wanna display a popup for eg say."Loading".
When I try to implement as i said before, i am getting error in 'parent'