Skip to content

Instantly share code, notes, and snippets.

@ICHx
Created August 26, 2021 10:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ICHx/273617e9cc78e12758b2da03d7bf1314 to your computer and use it in GitHub Desktop.
Save ICHx/273617e9cc78e12758b2da03d7bf1314 to your computer and use it in GitHub Desktop.
Notepad evolved, refactored to Kotlin, on JDK11+
package javaproj
import java.awt.event.ActionEvent
import java.awt.event.ActionListener
import java.awt.event.WindowEvent
import java.io.*
import javax.swing.*
import kotlin.system.exitProcess
import javax.swing.JOptionPane.showMessageDialog as alert
/**
* ! Java Swing | Create a simple text editor
* https://www.geeksforgeeks.org/java-swing-create-a-simple-text-editor/
* Refactored to Kotlin using Idea and own hands, removed some loops
*/
class Notepad internal constructor() : JFrame(), ActionListener {
// Text component
private var t: JTextPane // * https://stackoverflow.com/questions/11302982/jtextarea-consumes-a-lot-of-memory
// Frame
private var frame: JFrame = JFrame("editor v2")
companion object {
// Main class
@JvmStatic
fun main(args: Array<String>) {
System.setProperty("awt.useSystemAAFontSettings", "off")
System.setProperty("swing.aatext", "true")
Notepad()
}
}
// Constructor
init {
// Create a frame
try {
// I hate metal look and feel
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel")
} catch (ignored: Exception) {
}
// Text component
t = JTextPane()
//scroll
// Create a menubar
val mb = JMenuBar()
// Create amenu for menu
val m1 = JMenu("File")
// Create menu items
val mi1 = JMenuItem("New")
val mi2 = JMenuItem("Open")
val mi3 = JMenuItem("Save")
val mi9 = JMenuItem("Print")
// Add action listener
mi1.addActionListener(this)
mi2.addActionListener(this)
mi3.addActionListener(this)
mi9.addActionListener(this)
m1.add(mi1)
m1.add(mi2)
m1.add(mi3)
m1.add(mi9)
// Create amenu for menu
val m2 = JMenu("Edit")
// Create menu items
val mi4 = JMenuItem("cut")
val mi5 = JMenuItem("copy")
val mi6 = JMenuItem("paste")
// Add action listener
mi4.addActionListener(this)
mi5.addActionListener(this)
mi6.addActionListener(this)
m2.add(mi4)
m2.add(mi5)
m2.add(mi6)
val mc = JMenuItem("close")
mc.addActionListener(this)
mb.add(m1)
mb.add(m2)
mb.add(mc)
frame.jMenuBar = mb
frame.add(JScrollPane(t)) // enabling scrolling
frame.setSize(500, 500)
frame.defaultCloseOperation = EXIT_ON_CLOSE
frame.isVisible = true
}
//! If a button is pressed
override fun actionPerformed(e: ActionEvent) {
try {
when (e.actionCommand) {
"cut" -> t.cut()
"copy" -> t.copy()
"paste" -> t.paste()
"Save" -> {
Thread {
// Create an object of JFileChooser class
val j = JFileChooser("f:")
// Invoke the showsSaveDialog function to show the save dialog
val r = j.showSaveDialog(null)
if (r == JFileChooser.APPROVE_OPTION) {
// Set the label to the path of the selected directory
val fi = File(j.selectedFile.absolutePath)
try {
// Create a file writer
val wr = FileWriter(fi, false)
// Create buffered writer to write
val w = BufferedWriter(wr)
// Write
w.write(t.text)
w.flush()
w.close()
} catch (evt: java.lang.Exception) {
alert(frame, evt.message)
evt.printStackTrace()
}
} else alert(frame, "the user cancelled the operation")
}.start()
}
"Print" -> {
// print the file
t.print()
}
"Open" -> {
// Create an object of JFileChooser class
val j = JFileChooser("f:")
// Invoke the showsOpenDialog function to show the save dialog
val r = j.showOpenDialog(null)
// If the user selects a file
if (r == JFileChooser.APPROVE_OPTION) {
// Set the label to the path of the selected directory
val fi = File(j.selectedFile.absolutePath)
// File reader
val fr = FileReader(fi)
val br = BufferedReader(fr)
//! Take the input from the file
val lines: String = br.readLines()
.joinToString("\n")
// Set the text
t.text = lines
System.gc()
} else alert(frame, "the user cancelled the operation")
}
"New" -> {
t.text = ""
System.gc()
}
"close" -> {
// frame.isVisible = false
// exitProcess(0)
frame.dispatchEvent(WindowEvent(frame,WindowEvent.WINDOW_CLOSING))
}
} //end of when
} catch (evt: java.lang.Exception) {
alert(frame, evt.message)
evt.printStackTrace()
}
} //end of listener
} //end of class
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment