Note that there are some explanatory texts on larger screens.

plurals
  1. POhave to access switchcase statement from another class using jtextfield
    primarykey
    data
    text
    <p>In this below code section, I created two classes. First class is for designing in that I kept two jtextfields one is used to get String value another one is used to get numeric value. Second class is describing a function of string and numeric for to display the values in jtextfield. I can't to access string parameter in class a textfields.</p> <p>If I will use this code it's accessing both the functions in each jtextfield. Please give a solution for this problem.</p> <pre><code>import java.awt.event.*; import javax.swing.*; public class valid implements KeyListener { perform1 per1=new perform1(); String num1="Alpha"; String num2="Numeric"; String num3="Alphanumeric"; JFrame fr=new JFrame(); JPanel jp=new JPanel(); JLabel label1=new JLabel("STUDENT NAME"); JLabel label2=new JLabel("REG NO"); JTextField text1=new JTextField(15); JTextField text2=new JTextField(15); public valid(){ text1.addKeyListener(this); text2.addKeyListener(this); jp.add(label1); jp.add(text1); jp.add(label2); jp.add(text2); fr.add(jp); fr.setSize(450, 330); fr.setVisible(true); } public void keyTyped(KeyEvent e) { } public void keyPressed(KeyEvent e) { } public void keyReleased(KeyEvent e) { per1.dis(text1, e, num1); per1.dis(text2, e, num2); } public static void main(String ar[]) { new valid(); } } class perform1{ public void dis(JTextField PTxtCtrl,KeyEvent PKeyvalue,String PTempStr){ JLabel error=new JLabel(); String TmpCurntStr; String TmpConvStr; value val = value.valueOf(PTempStr); switch(val){ case Alpha: if(PKeyvalue.getKeyChar()&gt;='a'&amp;&amp; PKeyvalue.getKeyChar()&lt;='z'){ TmpCurntStr=PTxtCtrl.getText(); TmpConvStr=TmpCurntStr.toUpperCase(); PTxtCtrl.setText(TmpConvStr); }else{ JOptionPane.showMessageDialog(error,"Enter only alpha"); } break; case Numeric: if(PKeyvalue.getKeyChar()&gt;='0'&amp;&amp; PKeyvalue.getKeyChar()&lt;='9'){ TmpCurntStr=PTxtCtrl.getText(); System.out.println("hai"); }else{ JOptionPane.showMessageDialog(error,"Enter only numeric"); } break; case Alphanumeric: break; } } enum value{ Alpha, Numeric, Alphanumeric } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload