Building a Java text editor
JColorChooser
dialog.
Since you don't need to set any of the properties for JColorChooser
in the designer, there's no need to add it to the UI in the designer. You can just call it directly from a menu item's actionPerformed()
event handler as follows:
TextEditFrame.java
.
jMenuItem6
) which has "Foreground Color" in its actionCommand
property.
actionPerformed()
event to create the following event handler:
void jMenuItem6_actionPerformed(ActionEvent e) { }
//Handle the "Foreground Color" menu item Color color = JColorChooser.showDialog(this,"Foreground Color",jTextArea1.getForeground()); if (color != null) { jTextArea1.setForeground(color); } //repaints menu after item is selected this.repaint();
jMenuItem7
), which should have "Background Color" in its actionCommand
property. Create an actionPerformed()
event for it as you did for jMenuItem6
.
actionPerformed()
event for jMenuItem7
:
// Handle the "Background Color" menu item Color color = JColorChooser.showDialog(this,"Background Color",jTextArea1.getBackground()); if (color != null) { jTextArea1.setBackground(color); } //repaints menu after item is selected this.repaint();