Rambler's Top100

А Б В Г Д Е Ж З И К Л М Н О П Р С Т У Ф Х Ц Ч Ш Э Ю Я

Все примеры | Примеры по пакетам

Добавить строку в таблицу (JTable) нажатием кнопки (FlowLayout)


//Добавить строку в таблицу (JTable) нажатием кнопки (FlowLayout)
//Для размещения элементов используется FlowLayout
package jtable;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
public class jtableFrame1 {
 static  DefaultTableModel model = new DefaultTableModel();
 static  JButton button = new JButton("OK");
 static Vector rowData=new Vector();
 public static class MyActionListener implements ActionListener {
        public void actionPerformed(ActionEvent evt) {
            JButton button = (JButton)evt.getSource();
            System.out.println("Добавить строку");
            rowData.add("11");
            rowData.add("22");
            model.insertRow(0, rowData);
        }
    }
public static void main(String[] args) {
 model.addColumn("Column1");
 model.addColumn("Column2");
 Vector rowData=new Vector();
 rowData.add("d1");
 rowData.add("d2");
 model.insertRow(0, rowData);
 JTable jtbl=new JTable(model);
 JPanel jp=new JPanel();
 jp.add(jtbl);
 jp.add(button);
 button.addActionListener(new MyActionListener());
        JFrame jf=new JFrame();
        int align = FlowLayout.CENTER;
        jp.setLayout(new FlowLayout(align));
        jf.setDefaultCloseOperation(jf.EXIT_ON_CLOSE);
        jf.getContentPane().add(jp);
        jf.setSize(333, 333);
        jf.setVisible(true);
    }
}

01.02.2009

Rambler's Top100


Ассоциативные ссылки