Creating Control Buttons with NetBeans IDE - The nucleus of JToggleButton
(Page 2 of 5 )
This is the continuation of the previous section. Further proceeding we have the following:
private void tbtnItalicActionPerformed(
java.awt.event.ActionEvent evt)
{
// TODO add your handling code here:
repaintTextBox();
}
private void tbtnBoldActionPerformed(
java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
repaintTextBox();
}
The above code is the main heart of our application. According to the code in the previous section, you can understand that when the toggle buttons are hit the JRE tries to execute either "tbtnBoldActionPerformed" or "tbtnItalicActionPerformed" (based on the button hit). The implementations for both of those methods are as above. According to the above code, any of those events would simply execute another method, "repaintTextBox."
"repaintTextBox" is my own method, which is defined below:
private void repaintTextBox() {
int style=0;
if (this.tbtnBold.isSelected()) {
style += java.awt.Font.BOLD;
}
if (this.tbtnItalic.isSelected()) {
style += java.awt.Font.ITALIC;
}
this.txtMsg.setFont(this.txtMsg.getFont().deriveFont(style));
}
The above would simply test which toggle button is switched on (by using "isSelected") and apply the respective font style (bold or italic).
Next: Working with JCheckBox >>
More Java Articles
More By Jagadish Chaterjee