Помогите неопытному программисту !!! Нужно сделать так чтобы инфа в SWT таблице изменялась соответственно к БД. К сожелению SWT не имеет аналога AbstractTableModel как у SWING | Код |
import java.util.Iterator; import java.util.List; import java.util.ArrayList; import java.util.TreeMap;
import org.eclipse.swt.SWT; import org.eclipse.swt.events.ControlAdapter; import org.eclipse.swt.events.ControlEvent; import org.eclipse.swt.graphics.Point; import org.eclipse.swt.graphics.Rectangle; import org.eclipse.swt.layout.*; import org.eclipse.swt.widgets.*;
import com.Beans.BeanLec; import com.DAO.LecturerDAO;
/**This class is intended for forming in the right field table and filling of * her by information of lecturers from a database * @see java.util.Iterator * @see java.util.List * @see java.util.ArrayList * @see java.util.TreeMap * @see org.eclipse.swt.SWT * @see org.eclipse.swt.events.ControlAdapter * @see org.eclipse.swt.events.ControlEvent * @see org.eclipse.swt.graphics.Point * @see org.eclipse.swt.graphics.Rectangle * @see org.eclipse.swt.layout.GridData * @see org.eclipse.swt.widgets.Table * @see org.eclipse.swt.widgets.TableColumn * @see org.eclipse.swt.widgets.TableItem * @see org.eclipse.swt.widgets.Composite * @see org.eclipse.swt.widgets.Listener */
public class TableRight { static TableItem item; TableColumn[] columns; Table table; public static ArrayList alName = new ArrayList(); public static TreeMap Lec = new TreeMap(); JTable tbl = new JTable(); public static final String [] nameColumns = {"NAME", "BIRTHDAY", "ADDRESS", "ZIP", "CITY", "TAX", "MEMO"}; /**This method is intended for forming of table, and also call of methods of * creation of columns, filling of table * @param composite represents a top-level window*/ protected void TableLayout (Composite composite){ table = new Table(composite, SWT.FULL_SELECTION | SWT.SINGLE); table.setLayoutData(new GridData(GridData.FILL_BOTH)); table.setHeaderVisible(true); table.setLinesVisible(true); table.setRedraw(false); /*Cause a method for creation and name of columns*/ createColumns(table); /*Cause a method for filling of table*/ insertData(table, columns); /*Cause a method for the change of width of columns*/ resizeCol(composite, table); /*Cause a method for the receipt of the last name of the chosen lecturer*/ itemListener(table); } /** This method is intended for forming задання name of columns in a table * @param table the table * @return TableColumn[] */ private TableColumn[] createColumns(Table table) { columns = new TableColumn[nameColumns.length]; for (int i = 0; i < columns.length; i++) { columns[i] = new TableColumn(table, SWT.CENTER); /* This text will appear in the column header*/ columns[i].setText(nameColumns[i]); } return columns; } /**This method is intended for filling of table by all information about a lecturer * @param table represents copy of table * @param columns represents copy of columns*/ private void insertData(Table table, TableColumn[] columns) { /*Create the TreeMap of name lecturer <-> ID of lecturer*/ LecturerDAO createTM = new LecturerDAO(); List selectID = createTM.getDataFromDb(3, null); Iterator itLecID = selectID.iterator(); while (itLecID.hasNext()) { BeanLec getLecID = (BeanLec) itLecID.next(); Lec.put(getLecID.getName(), getLecID.getIdDel()); } /*Filling of table by all information about a lecturer*/ LecturerDAO testLecturer = new LecturerDAO(); List selectLecturer = testLecturer.getDataFromDb(1, null); Iterator itLecturer = selectLecturer.iterator(); while (itLecturer.hasNext()) { item = new TableItem(table,SWT.NONE); BeanLec InformLecturer = (BeanLec) itLecturer.next(); item.setText(new String [] {InformLecturer.getName(), InformLecturer.getBirth(), InformLecturer.getAddr(), InformLecturer.getZip(), InformLecturer.getCity(), InformLecturer.getTax(), InformLecturer.getMemo()}); numRows++; } for (int i = 0, n = columns.length; i < n; i++) { columns[i].pack(); } table.setRedraw(true); } /** This method is intended for the change of width of table of table in accordance with * the width of window * @param composite represents a top-level window * @param table represents copy of table */ private void resizeCol(final Composite composite, final Table table) { composite.addControlListener(new ControlAdapter() { public void controlResized(ControlEvent e) { Rectangle area = composite.getClientArea(); int width = area.width - table.getBorderWidth(); Point oldSize = table.getSize(); if (oldSize.x > area.width) { for (int i = 0; i < columns.length; i++) { columns[i].setWidth(width/columns.length-28); } table.setSize(area.width, area.height); } else { table.setSize(area.width, area.height); for (int i = 0; i < columns.length; i++) { columns[i].setWidth(width/columns.length-28); } } } }); } /**This method is intended for the choice of the last name of lecturer for his * subsequent editing or delete * @param table represents copy of table * @throws Exception if lecturer can't selected*/ private void itemListener(final Table table) { table.addListener(SWT.MouseDown, new Listener() { public void handleEvent(Event event) { try { Point pt = new Point(40, event.y); TableItem item = table.getItem(pt); addNameLec(item.getText()); } catch(Exception e) {} } }); } /* public void redraw(){ table.setRedraw(false); table.setRedraw(true); } */ /**This method is intended for the memorization last name of lecturer for his * subsequent editing or delete * @param nameLec surname lecturer*/ @SuppressWarnings("unchecked") private void addNameLec(String nameLec) { alName.clear(); alName.add(nameLec); } }
|
|