67 154
Assignments Done
99,4%
Successfully Done
In November 2018

Answer to Question #63989 in Java | JSP | JSF for hamod

Question #63989
write a complete java class that contains only one jtextfield .write only needed code to allow user to input octal number inside this jtextfield .also need to change the jtextfield's foreground color to red when an invalid digit is entered
Expert's answer

public class NewJFrame extends javax.swing.JFrame {

public NewJFrame() {
initComponents();
}

@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {

panel = new javax.swing.JPanel();
textField = new javax.swing.JTextField();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

panel.setBackground(new java.awt.Color(204, 255, 204));

textField.setFont(new java.awt.Font("Times New Roman", 0, 18)); // NOI18N
textField.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyReleased(java.awt.event.KeyEvent evt) {
textFieldKeyReleased(evt);
}
});

javax.swing.GroupLayout panelLayout = new javax.swing.GroupLayout(panel);
panel.setLayout(panelLayout);
panelLayout.setHorizontalGroup(
panelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(panelLayout.createSequentialGroup()
.addContainerGap()
.addComponent(textField, javax.swing.GroupLayout.DEFAULT_SIZE, 439, Short.MAX_VALUE)
.addContainerGap())
);
panelLayout.setVerticalGroup(
panelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(panelLayout.createSequentialGroup()
.addGap(122, 122, 122)
.addComponent(textField, javax.swing.GroupLayout.PREFERRED_SIZE, 42, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(136, Short.MAX_VALUE))
);

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(panel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(panel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
);

pack();
}// </editor-fold>//GEN-END:initComponents

private void textFieldKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_textFieldKeyReleased
char[] charArray = textField.getText().toCharArray();
boolean detect = true;
for (char ch : charArray) {
switch (ch) {
case '0': break;
case '1': break;
case '2': break;
case '3': break;
case '4': break;
case '5': break;
case '6': break;
case '7': break;
default: detect = false;
}
}
if (detect) panel.setBackground(new java.awt.Color(204, 255, 204));
else panel.setBackground(java.awt.Color.RED);
}//GEN-LAST:event_textFieldKeyReleased

/**
* @param args the command line arguments
*/
public static void main(String args[]) {
//java.awt.EventQueue.invokeLater(() -> {
NewJFrame frame = new NewJFrame();
frame.setResizable(false);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
//});
}

// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JPanel panel;
private javax.swing.JTextField textField;
// End of variables declaration//GEN-END:variables
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be first!

Leave a comment

Ask Your question

Submit
Privacy policy Terms and Conditions