I forgot to mention that I have 5 columns in the JTable so the second is not the last.
And also I forgot to tell that the data is refreshing itself when the header is being removed or added so actually it's almost a new table with different header and different data in the same JFrame, but I have no idea how to do it without creating a new table.
Use the DefaultTableModel to do basic funtions like adding rows and columns:
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.io.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.table.*;
public class TableRowColumn extends JFrame
{
private final static String LETTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
JTable table;
DefaultTableModel model;
JPanel buttonPanel;
JButton button;
public TableRowColumn()
{
// Create table
Object[][] data =
{
{new Integer(1), "A"},
{new Integer(2), "B"},
{new Integer(3), "C"}
};
String[] columnNames = {"Number","Letter"};
model = new DefaultTableModel(data, columnNames);
table = new JTable(model)
{
public boolean isCellEditable(int row, int column)
{
return true;
}
};
table.putClientProperty("terminateEditOnFocusLost", Boolean.TRUE);
table.setPreferredScrollableViewportSize(table.getPreferredSize());
table.getColumnModel().getColumn(0).
setCellRenderer( table.getDefaultRenderer(Integer.class) );
// Add table and a Button panel to the frame
final JScrollPane scrollPane = new JScrollPane( table );
getContentPane().add( scrollPane, BorderLayout.CENTER );
buttonPanel = new JPanel();
getContentPane().add( buttonPanel, BorderLayout.SOUTH );
button = new JButton( "Add Row" );
button.setMnemonic('A');
buttonPanel.add( button );
button.addActionListener( new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
model.addRow( createRow() );
int row = table.getRowCount() - 1;
table.changeSelection(row, 0, false, false);
table.requestFocusInWindow();
table.scrollRectToVisible(table.getCellRect(row, 0, true) );
}
});
button = new JButton( "Insert Row" );
buttonPanel.add( button );
button.addActionListener( new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
model.insertRow( 0, createRow() );
table.changeSelection(0, 0, false, false);
table.requestFocusInWindow();
table.scrollRectToVisible(table.getCellRect(0, 0, true) );
}
});
button = new JButton( "Empty Row" );
buttonPanel.add( button );
button.addActionListener( new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
model.setRowCount( model.getRowCount() + 1 );
int row = table.getRowCount() - 1;
table.changeSelection(row, 0, false, false);
table.requestFocusInWindow();
}
});
button = new JButton( "Add Column" );
buttonPanel.add( button );
button.addActionListener( new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String header = "Col" + (table.getColumnCount() + 1);
model.addColumn( header );
header = "Col" + (table.getColumnCount() + 1);
model.addColumn( header );
table.moveColumn(table.getColumnCount() - 1, 0);
table.requestFocusInWindow();
}
});
button = new JButton( "Add Column & Data" );
buttonPanel.add( button );
button.addActionListener( new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String header = "Col" + (table.getColumnCount() + 1);
int rows = table.getRowCount();
String[] values = new String[rows];
for (int j = 0; j < rows; j++)
{
values[j] = Integer.toString(j);
}
model.addColumn( header, values );
table.requestFocusInWindow();
}
});
button = new JButton( "Add Column - No Reordering" );
buttonPanel.add( button );
button.addActionListener( new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
// Use this method when you don't want existing columns
// to be rebuilt from the model.
// (ie. moved columns will not be reordered)
table.setAutoCreateColumnsFromModel( false );
String header = "Col" + (table.getColumnCount() + 1);
model.addColumn( header );
// AutoCreate is turned off so create table column here
TableColumn column = new TableColumn( table.getColumnCount() );
column.setHeaderValue( header );
table.addColumn( column );
// These won't work once setAutoCreate... has been set to false
buttonPanel.getComponent(3).setEnabled( false );
buttonPanel.getComponent(4).setEnabled( false );
table.requestFocusInWindow();
}
});
button = new JButton( "Remove Last Column" );
buttonPanel.add( button );
button.addActionListener( new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
int columns = model.getColumnCount();
if (columns > 0)
{
if (!table.getAutoCreateColumnsFromModel())
{
int view = table.convertColumnIndexToView(columns - 1);
TableColumn column =
table.getColumnModel().getColumn(view);
table.getColumnModel().removeColumn( column );
}
model.setColumnCount( columns - 1 );
}
table.requestFocusInWindow();
}
});
JTable lineTable = new LineNumberTable( table );
scrollPane.setRowHeaderView( lineTable );
}
private Object[] createRow()
{
Object[] newRow = new Object[2];
int row = table.getRowCount();
newRow[0] = Integer.toString(row + 1);
row = (row % 26) + 1;
newRow[1] = LETTERS.substring(row-1, row);
return newRow;
}
public static void main(String[] args)
{
TableRowColumn frame = new TableRowColumn();
frame.setDefaultCloseOperation( EXIT_ON_CLOSE );
frame.pack();
frame.setLocationRelativeTo( null );
frame.setVisible(true);
}
}
By the way I looked again in your program without compiling it.
As far as I understood it is removing the last columns and adding the new columns at the of the table ,but what I need is to put it in the middle or to drop it from the start.
I think what you gave me I already saw in the forum and it is not what I wanted!