Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package tabeldinamic;
- import java.awt.*;
- import java.awt.GridLayout;
- import java.awt.event.*;
- import java.util.ArrayList;
- import javax.swing.*;
- import javax.swing.table.*;
- public class TabelDinamic extends JFrame{
- private JTable tabel;
- private String [][] date;
- private String[] numeColoane;
- private ArrayList <JButton> butoane;
- private TableColumnModel mc;
- private DefaultTableModel mt;
- private int nrLinii, nrCol;
- public TabelDinamic(){
- super("Operatii cu tabel");
- nrLinii=10;
- nrCol=5;
- date = new String [10][5];
- for(int i=0;i<nrLinii;i++)
- for(int j=0;j<nrCol;j++)
- date[i][j]=(i+j)+"";
- numeColoane = new String [5];
- for(int i=0;i<5;i++)
- numeColoane[i]="Coloana"+(i+1);
- tabel = new JTable(date,numeColoane);
- butoane = new ArrayList<>();
- butoane.add(new JButton("Adauga o linie"));
- butoane.add(new JButton("Sterge o linie"));
- butoane.add(new JButton("Adauga o coloana"));
- butoane.add(new JButton("Adauga o coloana"));
- AscultatorButoane ab = new AscultatorButoane();
- JPanel p = new JPanel(new GridLayout(1,butoane.size(),10,10));
- for(int i=0;i<butoane.size();i++)
- {butoane.get(i).addActionListener(ab);
- p.add(butoane.get(i));
- }
- JScrollPane jp=new JScrollPane(tabel);
- mt = new DefaultTableModel(10,5);
- for(int i=0;i<nrLinii;i++)
- for(int j=0;j<nrCol;j++)
- mt.setValueAt(date[i][j],i,j);
- mc=tabel.getColumnModel();
- tabel.setModel(mt);
- add(jp);
- add(p,BorderLayout.SOUTH);
- mt.setColumnIdentifiers(numeColoane);
- tabel.setColumnSelectionAllowed(true);
- //AscultatorTabel at=new AscultatorTabel();
- //tabel.addMouseListener(at);
- AscultatorFocus af=new AscultatorFocus();
- tabel.addFocusListener(af);
- }
- private class AscultatorButoane implements ActionListener{
- private String[] date1;
- public void actionPerformed (ActionEvent e){
- if(e.getSource()==butoane.get(0)){
- date1=new String[5];
- mt.addRow(date1);
- nrLinii++;
- }
- else if(e.getSource()==butoane.get(1)){
- nrLinii--;
- mt.removeRow(nrLinii);
- }
- else if(e.getSource()==butoane.get(2)){
- nrCol++;
- mt.addColumn("Coloana"+nrCol);
- }
- else if(e.getSource()==butoane.get(3)){
- nrCol--;
- TableColumn tc=mc.getColumn(tabel.getSelectedColumn());
- mc.removeColumn(tc);
- }
- }
- }
- private class AscultatorFocus extends FocusAdapter{
- private int i,j;
- private String sir;
- public void focusLost(FocusEvent e){
- i=tabel.getSelectedRow();
- j=tabel.getSelectedColumn();
- sir=String.valueOf(mt.getValueAt(i,j));
- try{
- Integer.parseInt(sir);
- }catch(NumberFormatException ex){
- mt.setValueAt(0, i,j);
- JOptionPane.showMessageDialog(TabelDinamic.this,"Tv si tastezi: numere intregi");
- }
- }
- }
- public static void main(String[] args) {
- JFrame f = new TabelDinamic();
- f.setSize(400,200);
- f.setVisible(true);
- f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement