I'm on Windows using NetBeans IDE and lanterna. I try to create a SwingTerminal, but it won't show.
public static void main(String[] args) throws Exception {
SwingTerminal t = TerminalFacade.createSwingTerminal();
while (true) {
Thread.sleep(100);
}
}
I also tried displaying the JFrame, but I get null from SwingTerminal.getJFrame().
t.getJFrame().setVisible(true);
I also tried running the program from the command-line, thinking it might be an issue with NetBeans, but it didn't work either (cygwin). How can I make the SwingTerminal show?
I should've look at the Google Discussions first. Hacked together from a bunch of snippets:
public static void main(String[] args) {
// Create a Terminal and Screen.
SwingTerminal terminal = new SwingTerminal();
Screen screen = new Screen(terminal);
screen.startScreen();
// Add listener(s) for the Window. The JFrame won't shut
// down itself when Alt+F4 or the like is pressed or the
// Window is closed by pressing the X button.
terminal.getJFrame().addWindowListener(
new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent evt) {
screen.stopScreen();
}
}
);
}
Related
I'm making a fullscreen JavaFX program, but I want it to run as a screensaver in the background with other applications simultaneously running on top of it. Right now, it automatically creates a separate desktop just for the program. Any solutions?
Swing is definitely an option. Here is a place to start:
public class MyClass {
public void myAction () {
// your logic goes here
System.out.println("hello");
}
public static void main(String[] args) {
MyClass mc = new MyClass();
mc.myAction();
}
}
I am creating a small GUI java application that it will store some user credentials in a file.
If the file is missing or has the wrong properties then I want a pop to get brought up that will inform the user to register his credentials (so a new file can be created with the proper ones).
I have nailed down the logic of when the file is incorrect and/or missing but what I can't figure out (due to my inexperience with JFrame) is where exactly in the code to check if the user needs to enter his credentials so he can be prompted.
Let's say that the function showWarning() is the one that will check and display the popup if needed and this is my main JFrame function (this was generated from Netbeans mostly):
public static void main(String args[]) {
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new GUI().setVisible(true);
}
});
}
Do I put the showWarning() function inside the main function? If yes, do I put it right after new GUI().setVisible(true);? What is the proper way of doing this?
EDIT: I am stumbling to the same problem I did before. This is my showWarning() that I drafted quickly for testing purposes:
public void showWarning(){
File propertiesFile = new File("config.properties");
if (propertiesFile.exists() && propertiesExist(propertiesFile)) {
JOptionPane.showMessageDialog(rootPane, "Creds are ok");
} else {
JOptionPane.showMessageDialog(rootPane, "Creds are not ok");
}
}
The problem that I am having is that I can't make this method static in order to use it without an object because of the rootPane which is a non-static object. The problem that this caused is that I can't just write:
public static void main(String args[]) {
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new GUI().setVisible(true);
showWarning();
}
});
}
I can't use showWarning() like that since it's a non-static method.
Do I need to have the GUI object in a variable properly or is there a way to make the showWarning() a static method?
If you want the check to run right when the program starts, you would want to put your function call after the main JFrame gui is set visible. See edited code below. Of course, I'm using the ambiguous showWarning() function here, but you should talor that line of code to your need. If calling a function, then right the function, but if wanting to call a new popup jframe you will need to do more lines of code there.
public static void main(String args[]) {
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new GUI().setVisible(true);
LoginForm login = new LoginForm();
login.setVisible(true);
}
});
}
Now here you would want to change the variables accordingly. The LoginForm is a Jframe already created.
You probably want your popup dialog to be modal, so the program does not continue until the user has handled and fixed the problem. To do this, do not use a JFrame but a JDialog for your popup dialog and make it modal. Then you can simply put the showWarning() call everywhere you want. I think I would put it inside the main.
Use JDialog for creating the pop up.
And either you add the showWarning() method call in main like this :
public static void main(String args[]) {
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new GUI().setVisible(true);
showWarning();
}
});
}
or better you can invoke the method showWarning() when the user credentials have to be enetered in the file. If checked just before it, it would be optimal.
My users like having multiple JFrames; it allows them to resize the different components and place them wherever they want on the screen. However, I have a request to make all the child windows come to the front together... in other words, lets say they maximize another window in front of all the windows, and then use the task bar to click on just one of the JFrames. How can I set it so that they all come to the front? Note: it is also possible to close the child windows; if they are actually hidden, I do not want them to come to the front. I have a class ApplicationModel that keeps track of whether a window is hidden or not.
Things I've tried:
Using windowActivated() and focusGained() to try to bring them all to the front. This usually results in an infinite loop. The problem is that my eventing framework sends these requests off the Event Dispatch Thread, so any sort of blocking with an AtomicBoolean doesn't last long enough.
The main problem is not that I can't make them come to the front... I have made them come to the front. The problem is that they KEEP trying to come to the front, as bringing a window to the front throws the focusGained and windowActivated events, which creates an endless loop...
Making one window the master, and making the others a JDialog. Unfortunately, either the windows are modeless (and therefore don't come to front with the master window), or they are modal, (and therefore block the master window).
How can I fix either of these problems, or is there an entirely different third solution?
You can use a boolean field as a flag to prevent the infinite loop:
private boolean movingAllFramesToFront;
public void windowActivated(WindowEvent event) {
if (movingAllFramesToFront) {
return;
}
movingAllFramesToFront = true;
List<Frame> frames = getAllApplicationFrames();
for (Frame frame : frames) {
if (!applicationModel.isHidden(frame)) {
frame.toFront();
}
}
event.getWindow().toFront();
event.getWindow().requestFocus();
EventQueue.invokeLater(new Runnable() {
public void run() {
movingAllFramesToFront = false;
}
);
}
Another thing you can try is the new autoRequestFocus property introduced in Java 1.7. I have never tried using it, but here's my understanding of how it works:
public void windowActivated(WindowEvent event) {
final List<Frame> frames = getAllApplicationFrames();
for (Frame frame : frames) {
if (!applicationModel.isHidden(frame)) {
frame.setAutoRequestFocus(false);
frame.toFront();
}
}
EventQueue.invokeLater(new Runnable() {
public void run() {
for (Frame frame : frames) {
if (!applicationModel.isHidden(frame)) {
frame.setAutoRequestFocus(true);
}
}
}
);
}
I have an application with a lot of windows and had a problem similar to yours. My workaround is:
#Override
public void windowActivated(WindowEvent e) {
if (e.getOppositeWindow() == null) {
//front every window
}
}
First I created a class "SlveFrame" (Slve being the name of my app), a child of "JFrame".
public class SlveFrame extends JFrame implements WindowListener {
static ArrayList<SlveFrame> frames = new ArrayList<SlveFrame>();
public SlveFrame () {
addWindowListener(this); / /to make JFrame fire WindowListener's method
}
/ /... every method added from WindowListener
#Override
public void windowActivated(WindowEvent e) {
if (e.getOppositeWindow() == null) { // return null if window is not from my (or Your) work
for (SlveFrame frame : frames) { // if you have no idea what this is, look for "for each loop java" in google
frame.toFront();
}
}
}
/**
* The use of SlveFrame is almost the same as Jframe
*/
#Override
public void setVisible (boolean b) {
if (b)
frames.add(this);
else
frames.remove(this); // may raise an exception if you're not careful
super.setVisible(b); // or your window will simply not be visible.
}
#Override
public void dispose () {
frames.dispose(this) // may raise an exception you'll want to handle
}
}
The trick being that WindowEvent.getOppositeWIndow() returns a Jframe if the JFrame (or child class) is from your own program, meaning that if you switch to another program or app (such as eclipse, Firefox or a text editor) then back to any of your windows, then a call to getOppositeWindow() will return a 'null'. A simple if (e.getOppositeWindow()) makes it fairly easy to determine whether your window gain focus in condition that would require you to bring every window to the front, or rather to let everything be.
The overriding of setVisible (boolean b) and dispose () are optional but allow the dev to use it as a regular window.
I hope i could be of some help. Sincerly ~a lama
Given the following code:
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
ClientGUI gui = new ClientGUI();
gui.start();
}
});
}
everything works fine, I get a nice GUI window. OK.
Now, lets add an infinite loop after gui.start() :
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
ClientGUI gui = new ClientGUI();
gui.start();
while (true) {
}
}
});
}
and the output is a blank window that does not react to window exiting.
Can someone explain me what exactly happans here?
Can someone explane me what exactly happans here?
Sure. You're keeping the event dispatch thread tied up in an infinite loop, so it never gets to react to events such as "close window". Don't do that.
You should keep the event dispatch thread available for as much of the time as possible - don't perform any long-running tasks on it, including IO operations such as reading from files or the network.
See the "Concurrency in Swing" tutorial for more details.
hi i am new to Java i developed many forms but i am unable to connect those forms please any one tell me how can connect one form to another form for example after login screen my application form should be open
It sounds like you're coming from a Visual Basic background and you are trying to have some kind of procedure to display a login window, then a main program window.
There are many different ways to do this, but the two most common would be:
Display login dialog
Retrieve login information from closed dialog
Validate or exit/redisplay login
Display main window
and
Display login window
On 'OK' being pressed, validate or exit/display error
Hide self
Show main window
The first would be implemented something like this:
public static void main(String[] args) {
LoginDialog dlg = new LoginDialog();
dlg.setVisible(true);
LoginCredentials cred = dlg.getCredentials();
if ( ! valid(cred)) {
System.exit(1);
}
MainWindow wnd = new MainWindow(cred);
wnd.setVisible(true);
}
The second would look more like this:
public static void main(String[] args) {
LoginWindow app = new LoginWindow();
app.setVisible(true);
}
LoginWindow.actionPerformed(ActionEvent e) {
if ( ! validCredentials()) {
System.exit(1);
}
setVisible(false);
dispose();
MainWindow wnd = new MainWindow();
wnd.setVisible(true);
}
I recommend the first, so you can reuse the LoginDialog in other places, as it does not start the main window of this specific application itself.