### MessageDialog JFrame and JButton

B4J HowTo Project - Non-UI application showing HelloWorld with a JFrame in a BoxLayout with TextArea, Button, Label
@see http://docs.oracle.com/javase/tutorial/uiswing/
@run: From the commandline: java -jar helloworld.jar
`Sub Process_Globals
Private joInlineJava As JavaObject = Me
End Sub`
`Sub AppStart (Args() As String)
Log("App Startet " & DateTime.Date(DateTime.Now) & " " & DateTime.Time(DateTime.Now))
'Message using a TextArea
Dim CR As String = CRLF & CRLF
Dim msg As String = $"This is a message at ${CR}$DateTime{DateTime.Now}"$
joInlineJava.RunMethod("MessageDialog", Array(100,100,200,200,False, msg, "2015 by Robert W.B. Linn"))
'StartMessageLoop
End Sub`
`Sub buttonOKEvent(msg As String)
Log(msg)
ExitApplication
End Sub`
'Show a swing message dialog in a B4J nonui app
If JAVA
import javax.swing.JFrame;
import javax.swing.BoxLayout;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JEditorPane;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.Color;
public static void MessageDialog(int x, int y, int w, int h, boolean stayontop, String msg, String status) {
//Define the frame
JFrame frame = new JFrame("Message");
//Set frame type to utility
frame.setType(javax.swing.JFrame.Type.UTILITY);
frame.setAlwaysOnTop (stayontop);
//Set frame type to undecorated as another option --- NOT USED
//frame.setUndecorated(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Add listener to the title bar close button top right [x]
frame.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.out.println("Closing Window via Systemclose.");
//ba.raiseEvent(this, "buttonokevent", "You have clicked system close [x]");
System.exit(0);
}
});
//Create a vertical panel with vertical boxlayout
JPanel vPanel = new JPanel();
vPanel.setLayout(new BoxLayout(vPanel, BoxLayout.Y_AXIS));
//Add a textarea displaying the message
JTextArea textareaMessage = new JTextArea(msg);
textareaMessage.setToolTipText("The message.");
//Create a scrollpane for the textarea
JScrollPane scrollPaneMessage = new JScrollPane(textareaMessage);
scrollPaneMessage.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
scrollPaneMessage.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
vPanel.add(scrollPaneMessage);
//Create a new panel with a gridlayout for a button and a label
JPanel southPanel = new JPanel();
southPanel.setLayout(new GridLayout(0,1));
JButton buttonOK = new JButton("OK!");
buttonOK.setBackground(Color.GREEN);
buttonOK.setToolTipText("Click to close this application.");
buttonOK.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
//System.out.println("buttonOK clicked.");
//IMPORTANT: use lowercase for raising events!
ba.raiseEvent(this, "buttonokevent", "You have clicked button OK.");
}
});
southPanel.add(buttonOK, BorderLayout.SOUTH);
southPanel.setMaximumSize( southPanel.getPreferredSize() );
vPanel.add(southPanel, BorderLayout.SOUTH);
frame.getContentPane().add(vPanel);
//
JLabel labelStatus = new JLabel(status, JLabel.CENTER);
labelStatus.setToolTipText("Double Click to close the Widget.");
//Add mouse listener to the statuslabel
//double click exists the ExitApplication. This can used for a rasperry pi when the app has undecorated style (right mouse click)
labelStatus.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
if (e.getClickCount() == 2) {
System.out.println("Closing the application.");
//ba.raiseEvent(this, "updatesettings", frame.getX(), frame.getY(), frame.getWidth(), frame.getHeight());
System.exit(0);
}
}
});
frame.getContentPane().add(labelStatus, BorderLayout.SOUTH);
//Set the location
//frame.setLocationRelativeTo(null); NOT USED
frame.setLocation(x, y);
frame.setSize(w, h);
//frame.pack();
frame.setVisible(true);
}
End If

Changed: 2018-04-14 19:34, Category: B4J UI Applications on ARM Boards, File: T6C163M1207.htm, ID: 1207