Class GTerm
public final class GTerm
- This Version:
- 2021.03.01
GTerm allows you to create a GUI application using System.out.println-like statements.
Version History:
2020.02.29: Original version
2020.03.06: Added \n support for print and println. Added clear.
2020.03.10: Added textField, textArea, setTextInField, getTextInField, addButton support, repaint in clear.
2020.03.15: Does not exit on close; added close, addPasswordField, addList, addElementToList, removeElementFromList, getSelectedElementFromList
2020.03.21: Fixed non-removal in removeElementFromList, addList and addTextArea now has auto-scrollbars
2020.06.01: Lists of String type
2020.07.06: getElementFromList returns value of consistent type. Added getSelectedElementIndexFromList, setFilePath
2020.08.25: Lists replaced with tables (\t formats data in to columns. Supports optional header row.). Added addTable, addRowToTable, clearRowsOfTable, getSelectedRowIndexFromTable, getSelectedRowFromTable. Exceptions thrown from invoked supplied methods are more readable. Added addImageIcon(BufferedImage), showErrorDialog(String), showWarningDialog(String). Title of showMessageDialog(String) says "information" instead of "message" (to match icon). addPasswordField, addTextArea and addTextField now return the text field index.
2021.03.01: Images added in a terminal fashion. addTable uses background set by setBackgroundColor.
- Author:
- Gayan Wijesinghe
-
Constructor Summary
Constructors Constructor Description GTerm(int width, int height)
GTerm constructor taking a width and a height suggestion for the window -
Method Summary
Modifier and Type Method Description void
addButton(String buttonText, Object handlingObject, String handlingMethodName)
Adds a button.void
addImageIcon(String imageFilename)
Adds an image at current x,y coordinates.int
addPasswordField(String defaultPassword, int widthInPixels)
Adds a single-line password text field.int
addRowToTable(int indexOfTable, String rowData)
Adds a row to an existing table.int
addTable(int width, int height, String columnNames)
Adds a list-like table.int
addTextArea(String defaultText, int widthInPixels, int heightInPixels)
Adds a multi-line, word-wrapping-capable, auto-scrolling text area.int
addTextField(String defaultText, int widthInPixels)
Adds a single-line text field.void
clear()
Clears the GTerm like clearing a terminal.void
clearRowsOfTable(int indexOfTable)
Clears added rows of an existing table.void
close()
Closes this GTerm window but does not necessarily stop the program from running.java.awt.Color
getColorFromDialog(String message)
Shows a colour chooser.String
getFilePath()
Displays a file open dialog.int
getIndexOfSelectedRowFromTable(int indexOfTable)
Retrieve the currently chosen row's index within an existing table.String
getInputString(String message)
Allows the user to enter a String.String
getPasswordFromDialog(String message)
Shows a password entry dialog box and returns the password.String
getSelectedRowFromTable(int indexOfTable)
Retrieve the currently chosen row String from an existing table.String
getTextFromEntry(int textFieldIndex)
Access the text entered in a text field or a text area.void
print(String message)
Similar to System.out.print.void
println(String message)
Similar to System.out.println.void
setBackgroundColor(int r, int g, int b)
Change the RGB background colour of the window.void
setBackgroundColor(java.awt.Color backgroundColor)
Change the background colour of the window.String
setFilePath()
Displays a file save dialog.void
setFont(String fontName, int fontStyle, int fontSize)
Change the font size, style and size used for text that will be added next.void
setFontColor(int r, int g, int b)
Change the font colour used for text that will be added next.void
setFontColor(java.awt.Color fontColor)
Change the font colour used for text that will be added next.void
setFontName(String fontName)
Change the font name used for text that will be added next.void
setFontSize(int fontSize)
Change the font size used for text that will be added next.void
setFontStyle(int fontStyle)
Change the font style for text that will be added next.void
setTabSize(int tabSize)
Number of spaces used to replace a tab character by methods such as print and println.void
setTextInEntry(int textFieldIndex, String text)
Modify the text entered in a text field or a text area.void
setXY(int x, int y)
Allows the change of placement within the window.void
showErrorDialog(String message)
Shows the supplied message to the user using an error message dialog box.void
showHelp()
Prints the version, credits and help file location.void
showMessageDialog(String message)
Shows the supplied message to the user using an information message dialog box.void
showWarningDialog(String message)
Shows the supplied message to the user using a warning message dialog box.
-
Constructor Details
-
GTerm
public GTerm(int width, int height)GTerm constructor taking a width and a height suggestion for the window- Parameters:
width
- Suggested width of content area as an intheight
- Suggested height of content area as an int
-
-
Method Details
-
showHelp
public void showHelp()Prints the version, credits and help file location. -
getPasswordFromDialog
public String getPasswordFromDialog(String message)Shows a password entry dialog box and returns the password. Note: Password stored as a String with convenience, not security, in mind. Has no connection with addPasswordField method.- Parameters:
message
- The message that will be shown to the user.- Returns:
- Returns the entered password or null if cancelled.
-
getColorFromDialog
public java.awt.Color getColorFromDialog(String message)Shows a colour chooser. Refer to documentation of javax.swing.JColorChooser's showDialog method for details.- Parameters:
message
- The message that will be shown to the user.- Returns:
- Returns the chosen colour or null if cancelled.
-
addTable
public int addTable(int width, int height, String columnNames)Adds a list-like table. Also see addRowToTable, getSelectedRowFromTable, getSelectedRowIndexFromTable and clearRowsOfTable methods.- Parameters:
width
- Width in pixelsheight
- Height in pixelscolumnNames
- Tab-separated column names for header row. Use null for no header row.- Returns:
- Returns the index of the table (use this when adding to, retrieving from, etc. this table.
-
addRowToTable
public int addRowToTable(int indexOfTable, String rowData)Adds a row to an existing table. Use addTable method to add a table first.- Parameters:
indexOfTable
- Index of an existing table (see addTable)rowData
- Data for this row, optionally separated by '\t'. Automatically adds columns as needed.- Returns:
- Returns the index of the row (e.g. can be used with getSelectedRowFromTable method, etc.)
-
clearRowsOfTable
public void clearRowsOfTable(int indexOfTable)Clears added rows of an existing table. To remove a table or any other added component, use the clear method.- Parameters:
indexOfTable
- Index of an existing table (see addTable)
-
getIndexOfSelectedRowFromTable
public int getIndexOfSelectedRowFromTable(int indexOfTable)Retrieve the currently chosen row's index within an existing table.- Parameters:
indexOfTable
- Index of an existing table (see addTable)- Returns:
- Returns the index of the selected row or -1 if no row selected (e.g. can be used with getSelectedRowFromTable method, etc.)
-
getSelectedRowFromTable
public String getSelectedRowFromTable(int indexOfTable)Retrieve the currently chosen row String from an existing table.- Parameters:
indexOfTable
- Index of an existing table (see addTable)- Returns:
- Returns a String or -1 if no row selected
-
close
public void close()Closes this GTerm window but does not necessarily stop the program from running. Use the clear method if necessary to redraw (with removed, re-arranged components). -
setTextInEntry
public void setTextInEntry(int textFieldIndex, String text)Modify the text entered in a text field or a text area. Programmer must keep track of number of text areas currently visible. Indices reset to zero upon calls to the clear() method.- Parameters:
textFieldIndex
- Index number of text field/area currently showing.text
- Text to be added
-
getTextFromEntry
public String getTextFromEntry(int textFieldIndex)Access the text entered in a text field or a text area. Programmer must keep track of number of text areas currently visible. Indices reset to zero upon calls to the clear() method.- Parameters:
textFieldIndex
- Index number of text field/area currently showing.- Returns:
- The text that is currently in the specified text element.
-
addPasswordField
public int addPasswordField(String defaultPassword, int widthInPixels)Adds a single-line password text field. (See also addTextField) Access its contents using getTextFromEntry method. Modify its contents using setTextInEntry method. Note: Password stored as a String with convenience, not security, in mind.- Parameters:
defaultPassword
- Default text. Give an emptry String to keep blank.widthInPixels
- Width in pixels- Returns:
- Returns the index of this text component (to be used with getTextFromEntry, etc.)
-
addTextField
public int addTextField(String defaultText, int widthInPixels)Adds a single-line text field. (See also addTextArea and addPasswordField). Access its contents using getTextFromEntry method. Modify its contents using setTextInEntry method.- Parameters:
defaultText
- Default text. Give an emptry String to keep blank.widthInPixels
- Width in pixels- Returns:
- Returns the index of this text component (to be used with getTextFromEntry, etc.)
-
addTextArea
public int addTextArea(String defaultText, int widthInPixels, int heightInPixels)Adds a multi-line, word-wrapping-capable, auto-scrolling text area. (See also addTextField) Access its contents using getTextFromEntry method. Modify its contents using setTextInEntry method.- Parameters:
defaultText
- Default text. Give an emptry String to keep blank.widthInPixels
- Width in pixelsheightInPixels
- Height in pixels- Returns:
- Returns the index of this text component (to be used with getTextFromEntry, etc.)
-
addButton
public void addButton(String buttonText, Object handlingObject, String handlingMethodName)Adds a button.- Parameters:
buttonText
- Button text.handlingObject
- The object containing the method mentioned in handlingMethodName.handlingMethodName
- The method that will be called when the button is pressed.
-
clear
public void clear()Clears the GTerm like clearing a terminal. To close with GTerm window, use the close method. -
setFontName
public void setFontName(String fontName)Change the font name used for text that will be added next. Does not change fonts of previously added text.- Parameters:
fontName
- A String describing a logical Font. Default is Font.MONOSPACED. See API for more https://docs.oracle.com/javase/10/docs/api/java/awt/Font.html
-
setFontSize
public void setFontSize(int fontSize)Change the font size used for text that will be added next. Does not change size of previously added text.- Parameters:
fontSize
- An int size. Default is 12.
-
setFontStyle
public void setFontStyle(int fontStyle)Change the font style for text that will be added next. Does not change the style of previously added text.Examples: setFontStyle(Font.BOLD), setFontStyle(Font.ITALIC), setFontStyle(Font.PLAIN), setFontStyle(Font.BOLD | Font.ITALIC)
- Parameters:
fontStyle
- One of the above statements (int)
-
setBackgroundColor
public void setBackgroundColor(java.awt.Color backgroundColor)Change the background colour of the window. Default is Color.BLACK.- Parameters:
backgroundColor
- For available colours, refer to https://docs.oracle.com/javase/7/docs/api/java/awt/Color.html
-
setBackgroundColor
public void setBackgroundColor(int r, int g, int b)Change the RGB background colour of the window. Default is 0,0,0.- Parameters:
r
- An int between 0-255 indicating the red valueg
- An int between 0-255 indicating the green valueb
- An int between 0-255 indicating the blue value
-
setFont
public void setFont(String fontName, int fontStyle, int fontSize)Change the font size, style and size used for text that will be added next. Does not change previously added text.- Parameters:
fontName
- See documentation of setFontName method for detailsfontStyle
- See documentation of setFontStyle method for detailsfontSize
- See documentation of setFontSize method for details
-
setFontColor
public void setFontColor(java.awt.Color fontColor)Change the font colour used for text that will be added next. Does not change colours of previously added text.- Parameters:
fontColor
- For available colours, refer to https://docs.oracle.com/javase/7/docs/api/java/awt/Color.html
-
setFontColor
public void setFontColor(int r, int g, int b)Change the font colour used for text that will be added next. Does not change colours of previously added text.- Parameters:
r
- An int between 0-255 indicating the red valueg
- An int between 0-255 indicating the green valueb
- An int between 0-255 indicating the blue value
-
setTabSize
public void setTabSize(int tabSize)Number of spaces used to replace a tab character by methods such as print and println. Does not affect tables. Does not affect previously added text.- Parameters:
tabSize
- An int between 0-255 indicating the blue value
-
showMessageDialog
public void showMessageDialog(String message)Shows the supplied message to the user using an information message dialog box.- Parameters:
message
- The message String. If you want to display multiple variables, you will need to concatenate first.
-
showErrorDialog
public void showErrorDialog(String message)Shows the supplied message to the user using an error message dialog box.- Parameters:
message
- The message String. If you want to display multiple variables, you will need to concatenate first.
-
showWarningDialog
public void showWarningDialog(String message)Shows the supplied message to the user using a warning message dialog box.- Parameters:
message
- The message String. If you want to display multiple variables, you will need to concatenate first.
-
getInputString
public String getInputString(String message)Allows the user to enter a String. Shows an input dialog box with the message String supplied as a prompt.- Parameters:
message
- The message String. If you want to display multiple variables, you will need to concatenate first.- Returns:
- Returns a String. If you need an int or double use Integer.parseInt or Double.parseDouble to convert the returned String.
-
getFilePath
public String getFilePath()Displays a file open dialog.- Returns:
- Returns a String of the absolute path of the chosen file.
-
setFilePath
public String setFilePath()Displays a file save dialog.- Returns:
- Returns a String of the absolute path of the chosen/entered file.
-
addImageIcon
public void addImageIcon(String imageFilename)Adds an image at current x,y coordinates. Supports GIF, JPEG and PNG. Can be used with getFilePath method as gt.addImageIcon(gt.getFilePath()) Also see setXY method. As the purpose of this method is to simply "add" the image, if the image is too big, you will need to resize it using an image editor and save it before adding.- Parameters:
imageFilename
- Path to a image file.
-
addImageIcon
public void addImageIcon(java.awt.image.BufferedImage image)Adds a BufferedImage object at current x,y coordinates. Also see setXY method.- Parameters:
image
- Existing BufferedImage object.
-
print
public void print(String message)Similar to System.out.print. Text is added at current X,Y coordinates. Also see setXY method.- Parameters:
message
- String message to display.
-
println
public void println(String message)Similar to System.out.println. Text is added at current X,Y coordinates then cursor moves to "next line". Also see setXY method.- Parameters:
message
- String message to display.
-
setXY
public void setXY(int x, int y)Allows the change of placement within the window. Can be used with print, println, addImageIcon, etc. methods.- Parameters:
x
- An int x value within the windowy
- An int y value within the window
-
getMouseX
public int getMouseX()- Returns:
- Returns the x coordinate of the mouse within the window. -1 if outside window.
-
getMouseY
public int getMouseY()- Returns:
- Returns the y coordinate of the mouse within the window. -1 if outside window.
-
getKey
public String getKey()- Returns:
- Returns the character of the key that was most recently pressed.
-
keyReleased
public void keyReleased(java.awt.event.KeyEvent e)Do not use. Internal method.- Specified by:
keyReleased
in interfacejava.awt.event.KeyListener
-
keyTyped
public void keyTyped(java.awt.event.KeyEvent e)Do not use. Internal method.- Specified by:
keyTyped
in interfacejava.awt.event.KeyListener
-
keyPressed
public void keyPressed(java.awt.event.KeyEvent e)Do not use. Internal method.- Specified by:
keyPressed
in interfacejava.awt.event.KeyListener
-
mouseClicked
public void mouseClicked(java.awt.event.MouseEvent e)Do not use. Internal method.- Specified by:
mouseClicked
in interfacejava.awt.event.MouseListener
-
mousePressed
public void mousePressed(java.awt.event.MouseEvent e)Do not use. Internal method.- Specified by:
mousePressed
in interfacejava.awt.event.MouseListener
-
mouseReleased
public void mouseReleased(java.awt.event.MouseEvent e)Do not use. Internal method.- Specified by:
mouseReleased
in interfacejava.awt.event.MouseListener
-
mouseEntered
public void mouseEntered(java.awt.event.MouseEvent e)Do not use. Internal method.- Specified by:
mouseEntered
in interfacejava.awt.event.MouseListener
-
mouseExited
public void mouseExited(java.awt.event.MouseEvent e)Do not use. Internal method.- Specified by:
mouseExited
in interfacejava.awt.event.MouseListener
-