Java Basic Problems

JTable Cell Edit for validation: How to assign two Different JTextField in two different JTable Column Cells.

Two JTextField in two different Table Column cellThis post is also related to JTable cell validation, suppose our JTable has 3 columns.  First column will take String or character type input and the second column or Column 2 will take integer type input from users. So, if we want to implement this scenario in our software we can solve it by assigning or using two different JTextField  for the cells of  Column 1 and Column 2 in JTable. In our example we used textBox1 (JTextField) in Column 1, which will  validate string or char type input from users and  for Column 2  I have assigned    textBox (JTextField) to validate int type input from the users.  The given solution is just a basic you can implement it as your own way.  Just Copy the following code and run it.


Continue reading “JTable Cell Edit for validation: How to assign two Different JTextField in two different JTable Column Cells.”

Java Basic Problems

Java: I don’t want to allow Plus (+) or Minus (-) sign as a first entry of my JTextField.

In my previous example I have shown about How to add TextField (JTextField)in java.

Here I will try to show how to add validation in JTextField.

Suppose,I am trying to prepare a Calculator and I don’t want my user to give invalid input at the  first keypress entry,  for example, Plus (+) or Minus (-).

But, they can give any numeric entry at the first keyPress.So, I  need to put validation in my JTextField so that Plus (+) or Minus () sign will not be allowed at the first keypress /key entry, but  they are allowed after any numeric or digit entry.

In the given example, if users press (+) or () sign at first, our program will show error message but it will allow users to take numeric entry at first. After first entry, (+) or minus (-) will be allowed.


Code Example:

import java.awt.*;

import java.awt.event.*;

import java.awt.event.KeyEvent;

import javax.swing.*;

import java.util.*;

public class Calc extends JFrame


   JTextField ques, answer;

   private Toolkit toolkit;

   char ch;

   int plusOccurs=0;

        public Calc()


       JPanel panel =new JPanel();




          setTitle(“Easy Calculator”);



                    Dimension size=toolkit.getScreenSize();

                    setLocation(size.width/2-getWidth()/2, size.height/2-getHeight()/2);

                    ques=new JTextField();

                    answer=new JTextField();

        JButton calculate=new JButton(“Calculate”);


                   ques.setBounds(10,10,220,30);     // Positioning of TextField(JTextField)

          answer.setBounds(10,100,220,30); //Positioning  of TextField(JTextField)


          panel.add(ques );    // Adding TextField(JTextField) in JPanel

          panel.add(answer );  // Adding TextField(JTextField) in JPanel

                    ques.addKeyListener(new KeyAdapter()


                    public void keyTyped(KeyEvent e)



               if (ch==’+’ && ques.getText().length()==0)



   System.out.println(“Sorry, You have entered :”+ch+”:at first,Please Enter Numeric value, then try”);




                    else if (ch==’-‘&& ques.getText().length()==0)



                                         System.out.println(“Sorry, You have entered :”+ch+”:at first,Please Enter Numeric value at first”);