import javax.swing.*; import javax.swing.event.*; import javax.swing.table.*; //import com.sun.java.swing.*; //import com.sun.java.swing.event.*; //import com.sun.java.swing.table.*; import java.awt.*; import java.util.Vector; import java.awt.event.*; import java.text.*; public class TablePanel extends JPanel implements PlotSource { private JTable dataTable; private ExtendedTableModel tableModel; private DefaultTableColumnModel columnModel; private JComboBox xComboBox; private JComboBox yComboBox; private JOptionPane changeColumnName; private JScrollPane scrollPane; private int numRows = 25; private String[] names = {"A", "B"}; private Container container; TablePanel(Image iconIm1, Image iconIm2, Image iconIm3, Image iconIm4) { super(false); setLayout(new BorderLayout(10,10)); // Create JTable and embed in a scrollpane tableModel = new ExtendedTableModel(names,numRows); dataTable = new JTable(tableModel) { public void editingStopped(ChangeEvent e) { TableCellEditor editor = getCellEditor(); if (editor != null) { Object value = editor.getCellEditorValue(); Double newValue; try { newValue = new Double((String)value); } catch (NumberFormatException error){ newValue = null; } setValueAt(newValue, editingRow, editingColumn); removeEditor(); } } }; dataTable.getTableHeader().setReorderingAllowed(false); dataTable.getTableHeader().setResizingAllowed(true); dataTable.setFont(new Font("Dialog", Font.PLAIN, 12)); dataTable.setForeground(new Color(0)); dataTable.setBackground(new Color(16777215)); scrollPane = JTable.createScrollPaneForTable(dataTable); add(scrollPane,BorderLayout.CENTER); dataTable.getTableHeader().addMouseListener(new myColumnHeaderListener()); dataTable.getTableHeader().setToolTipText("Click Here to Rename Column"); // Create button panel and populate it GridLayout gl = new GridLayout(1,2); gl.setHgap(10); gl.setVgap(10); JPanel bottomPanel = new JPanel(gl); GridLayout gridLayout = new GridLayout(2,2); gridLayout.setHgap(10); gridLayout.setVgap(10); JPanel buttonPanel = new JPanel(gridLayout); JButton addColumn = new JButton("Add Column",new ImageIcon(iconIm1)); addColumn.addActionListener(new InsertColumnButtonListener()); buttonPanel.add(addColumn); JButton addRow = new JButton("Add Row", new ImageIcon(iconIm2)); addRow.addActionListener(new InsertRowButtonListener()); buttonPanel.add(addRow); JButton calculateColumn = new JButton("Enter Calculation", new ImageIcon(iconIm3) ); calculateColumn.addActionListener(new CalculateColumnButtonListener()); buttonPanel.add(calculateColumn); bottomPanel.add(buttonPanel); JButton clearData = new JButton("Clear Data", new ImageIcon(iconIm4)); clearData.addActionListener(new ClearTableButtonListener()); buttonPanel.add(clearData); // Add combo boxes JPanel plotOptionsPanel = new JPanel(gridLayout,false); JLabel xInstructions = new JLabel("Column to Plot Along x-axis "); JLabel yInstructions = new JLabel("Column to Plot Along y-axis"); plotOptionsPanel.add(xInstructions); plotOptionsPanel.add(yInstructions); xComboBox = new JComboBox(names); yComboBox = new JComboBox(names); xComboBox.setMaximumRowCount(2); yComboBox.setMaximumRowCount(2); xComboBox.setSelectedIndex(0); yComboBox.setSelectedIndex(1); plotOptionsPanel.add(xComboBox); plotOptionsPanel.add(yComboBox); bottomPanel.add(plotOptionsPanel); add(bottomPanel,BorderLayout.SOUTH); setBorder(BorderFactory.createEmptyBorder(15,15,15,15)); container = this; } public int getData(String axisNames[], double data[][]) { dataTable.editingStopped(null); int xIndex = xComboBox.getSelectedIndex(); int yIndex = yComboBox.getSelectedIndex(); int count = 0; double xvalue, yvalue; axisNames[0] = (String)xComboBox.getSelectedItem(); axisNames[1] = (String)yComboBox.getSelectedItem(); data[0] = new double[numRows]; data[1] = new double[numRows]; for(int i=0; i dataTable.getColumnCount())) return; if(column == dataTable.getColumnCount()) { for(int i = 0; i < dataTable.getColumnCount(); i++) for(int j = 0; j < dataTable.getRowCount(); j++) dataTable.setValueAt(null,j,i); } else { for(int j = 0; j < dataTable.getRowCount(); j++) dataTable.setValueAt(null,j,column); } } public void returnValue(String text) {} } class myColumnHeaderListener extends MouseAdapter implements CoverableContainer { int column; public void mouseClicked(MouseEvent e) { column = dataTable.getTableHeader().columnAtPoint(new Point(e.getX(),e.getY())); JModalPanel jm = new JModalPanel(container, this,"What is the new column name?",tableModel.getColumnName(column)); } public void returnValue(String text) { if(text.equals("") || text.equals(null)) return; if(text.equals(dataTable.getColumnName(column))) return; tableModel.setColumnName(column,getUniqueString(text)); addItemToComboBoxes(column); resizeColumns(); } public void returnValue(int i) {} } private void addItemToComboBoxes(int index) { int i1, i2; i1 = xComboBox.getSelectedIndex(); i2 = yComboBox.getSelectedIndex(); String name = dataTable.getColumnName(index); if(index>=xComboBox.getItemCount()) { xComboBox.addItem(name); yComboBox.addItem(name); } else if(index>=0) { xComboBox.removeItemAt(index); xComboBox.insertItemAt(name,index); yComboBox.removeItemAt(index); yComboBox.insertItemAt(name,index); } xComboBox.setSelectedIndex(i1); yComboBox.setSelectedIndex(i2); } private void updateComboBoxes() { // we can probably consolidate this method with addItemToComboBoxes(int index), // but it wouldn't really work without changing addItemToComboBoxes(int index) // around quite a bit. // // Right now this method is being called from paint(); // This is probably not a bad thing since this method does nothing unless // the columns have changed. // // This method would be simple to edit to encapsulate the removal of a column, // just in case we ever add that feature. if((dataTable.getColumnCount() > xComboBox.getItemCount()) || (dataTable.getColumnCount() > yComboBox.getItemCount())) { int index1 = xComboBox.getSelectedIndex(); int index2 = yComboBox.getSelectedIndex(); for(int i = 0; i < dataTable.getColumnCount(); i++) { String name = dataTable.getColumnName(i); if((i < xComboBox.getItemCount()) && (i < yComboBox.getItemCount())) { xComboBox.removeItemAt(i); yComboBox.removeItemAt(i); } xComboBox.insertItemAt(name,i); yComboBox.insertItemAt(name,i); } xComboBox.setSelectedIndex(index1); yComboBox.setSelectedIndex(index2); } } public void resizeColumns() { int width = (scrollPane.getWidth()-20)/dataTable.getColumnCount(); for(int i = 0; i < dataTable.getColumnCount(); i++) { dataTable.getColumn(dataTable.getColumnName(i)).setWidth(width); } } public void update(Graphics g) { paint(g); } public void paint(Graphics g) { resizeColumns(); updateComboBoxes(); paintComponents(g); } public void paintComponents(Graphics g) { g.setColor(getBackground()); int width = getSize().width; int height = getSize().height; g.fillRect(0,0,width,height); Component components[] = getComponents(); for(int i=0; i