All pastes #951796 Raw Edit

Someone

public java v1 · immutable
·published 2008-03-21 17:47 UTC
rendered paste body
import java.awt.*;import java.awt.event.*;import javax.swing.*;import javax.swing.plaf.*;import javax.swing.JScrollPane;import javax.swing.JTable;import javax.swing.table.TableModel;import javax.swing.table.DefaultTableModel;public class LinkStart extends Frame {// import javax.swing.table.AbstractTableModel; ////Init vars	int win_x = 500;			//Window size X	int win_y = 500;			//Window size Y	String[] columnNames = {"Link Postion", "Link Data"};	Object[][] data = {null};	//Init items	private Button addButon;	private Button genButton;	private Button editButton;	private Button sortButton;	//Init the object - The all important one!	LinkControl LinkData = new LinkControl();	//Init table stuff, also disable the cell edits	TableModel model = new DefaultTableModel(data, columnNames);	JTable table = new JTable(model){      public boolean isCellEditable(int rowIndex, int colIndex) {        return false;   //Disallow the editing of any cell		}    };		public LinkStart()     {		//Set frame title and size		super("Linked List GUI");        setSize(win_x, win_y);	        //Listeners        addWindowListener(                new WindowAdapter() {                    public void windowClosing(WindowEvent e) {                        System.exit(0);                    }                }        );				final ActionListener buttonListener = new ActionListener() {            public void actionPerformed(ActionEvent ae) {                String action = ae.getActionCommand();                if (action.equals("Add")) {					LinkData.add();                 } else if (action.equals("Generate")){					LinkData.gen();				 } else if (action.equals("Edit")){					LinkData.edit(table);				 } else if (action.equals("Sort")){					LinkData.sort();				} else {                    System.out.println(action);                }            }		};						//Set toolbars		Panel controlPanel = new Panel();		Panel tablePanel = new Panel();		//Create buttons 		JButton addButton = new JButton("Add");		JButton genButton = new JButton("Generate");		JButton editButton = new JButton("Edit");		JButton sortButton = new JButton("Sort");				//Add buttons to the buttonListener		addButton.addActionListener(buttonListener);		genButton.addActionListener(buttonListener);		editButton.addActionListener(buttonListener);		sortButton.addActionListener(buttonListener);				//Set tool tips for some simple help		addButton.setToolTipText("Add to linked list");		genButton.setToolTipText("Generate a new list");		editButton.setToolTipText("Edit selected row");		sortButton.setToolTipText("Sort by");				//Add Items to panel and add panel		controlPanel.add(addButton);		controlPanel.add(genButton);		controlPanel.add(editButton);		controlPanel.add(sortButton);		add(controlPanel, BorderLayout.SOUTH);					JScrollPane scrollPane = new JScrollPane(table);		table.setFillsViewportHeight(true);		tablePanel.add(scrollPane);		add(tablePanel, BorderLayout.CENTER);	}public static void main(String Args[]) 	{		LinkStart LinkFrame = new LinkStart();        LinkFrame.setVisible(true);			}}import java.util.LinkedList;import java.util.Iterator;import java.util.ListIterator;import javax.swing.JTable;import java.util.Random;import javax.swing.table.TableModel;class LinkControl{LinkedList LinkObject = new LinkedList();Iterator iterator;int i;public LinkControl()		{		System.out.println("Link control() works");		}	void add()		{		System.out.println("Link control.add() works");		}		void gen()		{				System.out.println("Link control.gen() works");		//We must set the first and last entrys before we add anything else, otherwise the java program throws a fit		LinkObject.addFirst(randomData());		LinkObject.addLast(randomData());		//loop through and add 10 more entrys		for (i = 1; i<=10; i ++){		LinkObject.add(i,randomData());		}		// purely for debuging, just to check the list		iterator = LinkObject.iterator();		while (iterator.hasNext()){			System.out.println(iterator.next()+" ");  			//System.out.println(iterator.nextIndex()+" ");			}			}void edit(JTable table)		{		System.out.println("Link control.edit() works");		System.out.println(table.getValueAt(getFocusRow(table), 0));		}		void sort()		{		System.out.println("Link control.sort() works");		}		public static int getFocusRow(JTable table) {		return table.getSelectionModel().getLeadSelectionIndex();		//System.out.println(table.getSelectionModel().getLeadSelectionIndex());		}		public static int getFocusColumn(JTable table) {		return table.getColumnModel().getSelectionModel().getLeadSelectionIndex();		}		public static long randomData(){		Random rand = new Random();		return rand.nextLong();		}	}