Advertisement
Guest User

Untitled

a guest
Mar 21st, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.28 KB | None | 0 0
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package tabeldinamic;
  7. import java.awt.*;
  8. import java.awt.GridLayout;
  9. import java.awt.event.*;
  10. import java.util.ArrayList;
  11. import javax.swing.*;
  12. import javax.swing.table.*;
  13. public class TabelDinamic extends JFrame{
  14. private JTable tabel;
  15. private String [][] date;
  16. private String[] numeColoane;
  17. private ArrayList <JButton> butoane;
  18. private TableColumnModel mc;
  19. private DefaultTableModel mt;
  20. private int nrLinii, nrCol;
  21. public TabelDinamic(){
  22. super("Operatii cu tabel");
  23. nrLinii=10;
  24. nrCol=5;
  25. date = new String [10][5];
  26. for(int i=0;i<nrLinii;i++)
  27. for(int j=0;j<nrCol;j++)
  28. date[i][j]=(i+j)+"";
  29. numeColoane = new String [5];
  30. for(int i=0;i<5;i++)
  31. numeColoane[i]="Coloana"+(i+1);
  32. tabel = new JTable(date,numeColoane);
  33. butoane = new ArrayList<>();
  34. butoane.add(new JButton("Adauga o linie"));
  35. butoane.add(new JButton("Sterge o linie"));
  36. butoane.add(new JButton("Adauga o coloana"));
  37. butoane.add(new JButton("Adauga o coloana"));
  38. AscultatorButoane ab = new AscultatorButoane();
  39. JPanel p = new JPanel(new GridLayout(1,butoane.size(),10,10));
  40. for(int i=0;i<butoane.size();i++)
  41. {butoane.get(i).addActionListener(ab);
  42. p.add(butoane.get(i));
  43. }
  44. JScrollPane jp=new JScrollPane(tabel);
  45. mt = new DefaultTableModel(10,5);
  46. for(int i=0;i<nrLinii;i++)
  47. for(int j=0;j<nrCol;j++)
  48. mt.setValueAt(date[i][j],i,j);
  49. mc=tabel.getColumnModel();
  50. tabel.setModel(mt);
  51. add(jp);
  52. add(p,BorderLayout.SOUTH);
  53. mt.setColumnIdentifiers(numeColoane);
  54. tabel.setColumnSelectionAllowed(true);
  55. //AscultatorTabel at=new AscultatorTabel();
  56. //tabel.addMouseListener(at);
  57. AscultatorFocus af=new AscultatorFocus();
  58. tabel.addFocusListener(af);
  59. }
  60. private class AscultatorButoane implements ActionListener{
  61. private String[] date1;
  62. public void actionPerformed (ActionEvent e){
  63. if(e.getSource()==butoane.get(0)){
  64. date1=new String[5];
  65. mt.addRow(date1);
  66. nrLinii++;
  67. }
  68. else if(e.getSource()==butoane.get(1)){
  69. nrLinii--;
  70. mt.removeRow(nrLinii);
  71. }
  72. else if(e.getSource()==butoane.get(2)){
  73. nrCol++;
  74. mt.addColumn("Coloana"+nrCol);
  75. }
  76. else if(e.getSource()==butoane.get(3)){
  77. nrCol--;
  78. TableColumn tc=mc.getColumn(tabel.getSelectedColumn());
  79. mc.removeColumn(tc);
  80. }
  81. }
  82. }
  83. private class AscultatorFocus extends FocusAdapter{
  84. private int i,j;
  85. private String sir;
  86. public void focusLost(FocusEvent e){
  87. i=tabel.getSelectedRow();
  88. j=tabel.getSelectedColumn();
  89. sir=String.valueOf(mt.getValueAt(i,j));
  90. try{
  91. Integer.parseInt(sir);
  92.  
  93. }catch(NumberFormatException ex){
  94. mt.setValueAt(0, i,j);
  95. JOptionPane.showMessageDialog(TabelDinamic.this,"Tv si tastezi: numere intregi");
  96. }
  97. }
  98. }
  99.  
  100.  
  101.  
  102. public static void main(String[] args) {
  103. JFrame f = new TabelDinamic();
  104. f.setSize(400,200);
  105. f.setVisible(true);
  106. f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  107. }
  108.  
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement