Swing - JTextField Adding a text field to the main window (with tooltips and all)
Prerequisites - before this lecture You should have seen: The lecture on JFrame The lecture on JButton Including having seen and run the examples on the above, found at https://github.com/progund/java-extra-lectures/tree/master/swing/componen ts-examples
Color codes in RGB Colors in a computer can be described as a 24 bit number with three parts RED (first 8 bits) 1111 1111???????????????? GREEN (next 8 bits)???????? 1111 1111???????? BLUE (last 8 bits)???????????????? 1111 1111 All colors can be produced by mixing R.G.B.: Black: 000000 16 White FFFFFF 16 Purple FF00FF 16 (red mixed with blue) Mix: Purple: 1111 1111 0000 0000 1111 1111 Numbers are given in hexadecimal (00 - FF are 16 bits) 0x0 is 0000 (in four bits) 0xF is 1111, 0xFF is 1111 1111
Let s make a GUI where you enter a color code
Let s make a GUI where you enter a color code If you enter FF00FF (maximum red, no green, maximum blue) you should get purple: You select the entered color by hitting [Enter] or clicking the button. The label gets the specified color (or black if it couldn t be interpreted).
Swing class rough layout public class JTextFieldExample { private JFrame window; // more components... public WindowExample() { initcomponents(); layoutcomponents(); addlisteners(); private void initcomponents() { // initialize the components... // more methods (layoutcomponents(), addlisteners()...)
Let s make a GUI where you enter a color code New components (as instance variables as usual): JLabel (just text with the prompt text Color in hex: JTextField (the text input box) ActionListener - we ll use the same action on both the textfield and button private JFrame window; // main window as before private JButton colorbutton; // the button private JLabel prompt; // the text Color in hex private ActionListener colorchanger; // The action to take private JTextField colortextfield; // where to enter text
Let s make a GUI where you enter a color code Constructor as before (the class is called TextFieldExample): public TextFieldExample() { initcomponents(); layoutcomponents(); addlisteners();
Let s make a GUI where you enter a color code private void initcomponents() { initwindow(); // the JFrame... colorbutton = new JButton("Change color"); prompt = new JLabel("Color in hex:"); // text to show colortextfield = new JTextField("000000"); // intial text colortextfield.selectall(); // select the text colortextfield.requestfocus(); // put focus in the textfield colortextfield.settooltiptext("<html><ul>" + "<li>red: FF0000</li>" + "<li>green: 00FF00</li>" + "<li>blue: 0000FF</li>" + "</ul></html>");
Let s make a GUI where you enter a color code A tooltip is a small help text which pops up when you hover the mouse over a component. Swing components with text can be styled using HTML: colortextfield.settooltiptext("<html><ul>" + "<li>red: FF0000</li>" + "<li>green: 00FF00</li>" + "<li>blue: 0000FF</li>" + "</ul></html>");
Let s make a GUI where you enter a color code
Let s make a GUI where you enter a color code layoutcomponents becomes: private void layoutcomponents() { window.setlayout(new FlowLayout()); window.add(prompt); window.add(colortextfield); window.add(colorbutton);
Creating the ActionListener colorchanger = e -> { Color newcolor = null; try { newcolor = Color.decode("0x" + colortextfield.gettext()); catch (NumberFormatException ignore) { // only valid ints are good if (newcolor!= null) { prompt.setforeground(newcolor); else { prompt.setforeground(black); colortextfield.settext("??????"); colortextfield.requestfocus(); colortextfield.selectall(); ;
Creating the ActionListener colorchanger = e -> { Color newcolor = null; try { //prepend with 0x and get text from textfield newcolor = Color.decode( "0x" + colortextfield.gettext()); catch (NumberFormatException ignore) { // only valid ints are good if (newcolor!= null) { prompt.setforeground(newcolor); else { prompt.setforeground(black); colortextfield.settext("??????"); colortextfield.requestfocus(); colortextfield.selectall(); ;
Creating the ActionListener - complete private void addlisteners() { colorchanger = e -> { Color newcolor = null; try { newcolor = Color.decode("0x" + colortextfield.gettext()); catch (NumberFormatException ignore) { if (newcolor!= null) { prompt.setforeground(newcolor); else { // invalid number was entered prompt.setforeground(black); // reset color to black colortextfield.settext("??????"); // show the user you didn t understand the number colortextfield.requestfocus(); // make it easy to overwrite the question marks colortextfield.selectall(); ; colortextfield.addactionlistener(colorchanger); // add to both textfield colorbutton.addactionlistener(colorchanger); // and button
Source code - A novel in 25 volumes import javax.swing.*; import java.awt.flowlayout; import java.awt.event.*; import static java.awt.color.*; import java.awt.color; public class TextFieldExample { private JFrame window; private JButton colorbutton; private JLabel prompt; private ActionListener colorchanger; private JTextField colortextfield;
Source code - A novel in 25 volumes public TextFieldExample() { initcomponents(); layoutcomponents(); addlisteners(); private void initwindow() { window = new JFrame("JTextField example"); window.setdefaultcloseoperation(jframe.exit_on_close); window.setsize(800, 600);
Source code - A novel in 25 volumes private void initcomponents() { initwindow(); // JFrame initialization moved to separate method to save space here colorbutton = new JButton("Change color"); prompt = new JLabel("Color in hex:"); colortextfield = new JTextField("000000"); colortextfield.selectall(); colortextfield.requestfocus(); colortextfield.settooltiptext("<html><ul><li>red: FF0000</li><li>Green: 00FF00</li><li>Blue: 0000FF</li></ul></html>"); // line-wrap - one single line private void layoutcomponents() { window.setlayout(new FlowLayout()); window.add(prompt); window.add(colortextfield); window.add(colorbutton);
Source code - A novel in 25 volumes private void addlisteners() { colorchanger = e -> { Color newcolor = null; try { newcolor = Color.decode("0x" + colortextfield.gettext()); catch (NumberFormatException ignore) { if (newcolor!= null) { prompt.setforeground(newcolor); else { prompt.setforeground(black); colortextfield.settext("??????"); colortextfield.requestfocus(); colortextfield.selectall(); ; colortextfield.addactionlistener(colorchanger); colorbutton.addactionlistener(colorchanger);
Source code - A novel in 25 volumes public void show() { window.setvisible(true); // end-of-class-declaration
Source code - Main class public class TextFieldExampleRunner { public static void main(string[] args) { try { javax.swing.swingutilities.invokelater(new Runnable() { public void run() { TextFieldExample textex = new TextFieldExample(); textex.show(); ); catch(exception e) { System.err.println("Exception in main: " + e.getmessage());
Further reading https://docs.oracle.com/javase/tutorial/uiswing/components/textfield.html https://docs.oracle.com/javase/tutorial/uiswing/components/tooltip.html https://docs.oracle.com/javase/tutorial/uiswing/components/label.html https://docs.oracle.com/javase/8/docs/api/java/awt/color.html