Java GUI sample

This is a Java GUI sample. Regarding the Java GUI, there are some samples that are completely described in the main class, and I remember having difficulty finding a practical usage, so I implemented a group of GUI dialogs separately from the main class. Share here.

1.swing.JOptionPane InfoDialog 無題.png

This is a general-purpose dialog.

main.java


public class Main {
    public static void main(String[] args) {
        InfoDialog.showDialog("Please select a file(.xlsx)。");
       }
}

InfoDialog.java


import javax.swing.JOptionPane;

class InfoDialog {

	public static void showDialog(String msg) {
		JOptionPane.showMessageDialog(null, msg,"Take it",JOptionPane.INFORMATION_MESSAGE);
	}
}

When using this method, specify the character string you want to display in the dialog for the argument String. Here, "Please select a file (.xlsx)." I think the 2nd-4th arguments of the showMessageDialog method are understandable, but the first one takes parentComponent as an argument. I couldn't figure out what the ~~ parentComponent was, but ~~ basic null should be fine.

If you want to select "Yes" or "No", use the showConfirmDialog method instead of showMessageDialog.

ErrorDialog Just change the 4th argument of ShowMessageDialog from INFORMATION_MESSAGE, to ERROR_MESSAGE, and nothing else will change. 無題2.png

main.java


public class Main {
    public static void main(String[] args) {
        InfoDialog.ErrorDialog(".xlsx is unspecified. I'm done.");
       }
}

ErrorDialog.java


import javax.swing.JOptionPane;

class ErrorDialog {

	public static void showDialog(String msg) {
		JOptionPane.showMessageDialog(null, msg,"Take it",JOptionPane.ERROR_MESSAGE);
	}
}

2.awt.FileDialog

A dialog that lets the user select a file.

無題3.png

Main.java


public class Main {
	public static void main(String[] args) {
		new WindowTest();
        File file = new File(WindowTest.dir + WindowTest.fileName);
       }
}

WindowTest.java


import java.awt.FileDialog;
import java.awt.Frame;
import java.awt.event.WindowListener;

class WindowTest extends Frame implements WindowListener {
	static String dir;
	static String fileName;

	WindowTest() {
		FileDialog fileDialog = new FileDialog(this);
		fileDialog.setVisible(true);
		this.dir = fileDialog.getDirectory();
		this.fileName = fileDialog.getFile();

		if (fileName == null) {
			ErrorDialog.showDialog(".xlsx is unspecified. I'm done.");
			System.exit(0);
		}
	}

	public void windowActivated(java.awt.event.WindowEvent e) {
	}
	public void windowClosed(java.awt.event.WindowEvent e) {
	}
	public void windowClosing(java.awt.event.WindowEvent e) {
		System.exit(1);
	}
	public void windowDeactivated(java.awt.event.WindowEvent e) {
	}
	public void windowDeiconified(java.awt.event.WindowEvent e) {
	}
	public void windowIconified(java.awt.event.WindowEvent e) {
	}
	public void windowOpened(java.awt.event.WindowEvent e) {
	}
}

that's all.

Recommended Posts

Java GUI sample
[Java] Generics sample
Java sample code 02
Java sample code 03
Selenium sample (Java)
Java sample code 04
Java sample code 01
[Java] Holiday judgment sample
[Java] logback slf4j sample
Digital signature sample code (JAVA)
Java
Java standard log output sample
Java
Selenium Sample Reservation Form (Java)
Java 9 new features and sample code
A simple sample callback in Java
BloomFilter description and implementation sample (JAVA)
[Java] Date period duplication check sample
EXCEL file update sample with JAVA
Sample vending machine made in Java
Sample code using Minio from Java
Java learning (0)
Studying Java ―― 3
[Java] array
Java protected
[Java] Annotation
[Java] Module
Java array
Studying Java ―― 9
Java scratch scratch
Java tips, tips
Java methods
Java method
java (constructor)
Java array
[Java] ArrayDeque
java (override)
java (method)
Java string
java (array)
Java serialization
java beginner 4
JAVA paid
Studying Java ―― 4
Java (set)
java shellsort
[Java] compareTo
Studying Java -5
java reflexes
java (interface)
Java memorandum
☾ Java / Collection
Java array
Studying Java ―― 1
[Java] Polymorphism
Studying Java # 0
Java review
Java features
[Java] Inheritance
FastScanner Java
Java features