Advertisement
YauhenMardan

Untitled

Feb 26th, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.99 KB | None | 0 0
  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import javax.swing.*;
  4. import javax.swing.table.*;
  5. class JtableCellValidation extends JFrame
  6. {
  7. private JPanel topPanel;
  8. private JTable table;
  9. private JScrollPane scrollPane;
  10. private String[] columnNames=new String[3];
  11. private String[][] dataValues=new String[3][3];
  12. JTextField textBox=new JTextField();
  13. public JtableCellValidation()
  14. {
  15. setTitle(JTable Cell Validation”);
  16. setSize(300,300);
  17. setDefaultCloseOperation (EXIT_ON_CLOSE);
  18. topPanel=new JPanel();
  19. topPanel.setLayout(new BorderLayout());
  20. getContentPane().add(topPanel);
  21. columnNames=new String[] {“Column 1″ ,”Column 2”, “Column 3};
  22. dataValues=new String[][]
  23.                    {
  24.                    {1″,”2″,”3},
  25.                    {4″,”5″,”6},
  26.                    {7″,”8″,”9}
  27.                    };
  28. TableModel  model=new myTableModel();
  29. table=new JTable();
  30. table.setRowHeight(50);
  31. table.setModel(model);
  32. TableColumn soprtColumn=table.getColumnModel().getColumn(1);
  33. soprtColumn.setCellEditor(new DefaultCellEditor (textBox));
  34. table.setCellSelectionEnabled(true);
  35. scrollPane=new JScrollPane(table);
  36. topPanel.add(scrollPane,BorderLayout.CENTER);
  37. textBox.addKeyListener(new KeyAdapter()
  38. {
  39. public void keyTyped(KeyEvent e)
  40. {
  41. if(!Character.isDigit(e.getKeyChar()) && e.getKeyChar() !=KeyEvent.VK_BACK_SPACE)
  42. {
  43. textBox.setEditable(false);
  44. textBox.setBackground(Color.WHITE);
  45. JOptionPane.showMessageDialog(null,”String Type Entry Not Allowed”);
  46. }
  47. else
  48. {
  49. textBox.setEditable(true);
  50. }
  51. }
  52. });
  53. table.addMouseListener(new java.awt.event.MouseAdapter()  
  54. {
  55. public void mouseClicked(java.awt.event.MouseEvent e)
  56. {
  57. }
  58. });
  59. }
  60. public class myTableModel extends DefaultTableModel
  61. {
  62. myTableModel()
  63. {
  64. super(dataValues,columnNames);
  65. }
  66. public boolean isCellEditable(int row,int cols)
  67. {
  68. return true;
  69. }
  70. }
  71. public static void main(String args[])
  72.   {
  73.   JtableCellValidation x=new JtableCellValidation();
  74.   x.setVisible(true);
  75.   }  
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement