Reputation: 115
I am using Jtable in my project where when tab key is pressed new column is added but the name of that column is not there. It is displaying a blank in that place I want that column name should be of excel format ie. like excel column name should be A,B,C and so on. So what should I check for that.
private void datatypetableKeyPressed(java.awt.event.KeyEvent evt) {
if (evt.getKeyCode() == KeyEvent.VK_TAB) {
addColumn();
}
}
private void addColumn() {
DefaultTableModel model = (DefaultTableModel) datatypetable.getModel();
JTableHeader th = datatypetable.getTableHeader();
TableColumnModel tcm = th.getColumnModel();
TableColumn tc = tcm.getColumn(0);
if (model != null) {
Vector v = new Vector(1);
for (int j = 0; j < datatypetable.getRowCount(); j++) {
tc.setHeaderValue("???");
v.add("");
}
model.addColumn(v);
th.repaint();
}
}
This is my code for adding new columns to jtable.
Upvotes: 4
Views: 1292
Reputation: 115
I have done in this way and its working....Hope my answer help anyone...
private void datatypetableKeyPressed(java.awt.event.KeyEvent evt) {
if (evt.getKeyCode() == KeyEvent.VK_ENTER) {
int col = datatypetable.getSelectedColumn();
int row = datatypetable.getSelectedRow();
int colCount = datatypetable.getColumnCount();
int rowCount = datatypetable.getRowCount();
col++;
if (col >= colCount) {
col = 0;
row++;
}
if (row >= rowCount) {
row = 0;
}
// datatypetable.getSelectionModel().setSelectionInterval(row, row);
// datatypetable.getColumnModel().getSelectionModel().setSelectionInterval(col, col);
// datatypetable.editCellAt(row, col);
if (row == rowCount - 1) {
addRow();
datatypetable.scrollRectToVisible(datatypetable.getCellRect(rowCount, 0, true));
}
}
if (evt.getKeyCode() == KeyEvent.VK_TAB) {
int col = datatypetable.getSelectedColumn();
int row = datatypetable.getSelectedRow();
int colCount = datatypetable.getColumnCount();
int rowCount = datatypetable.getRowCount();
col++;
if (col >= colCount) {
col = 0;
row++;
}
if (row >= rowCount) {
row = 0;
}
// datatypetable.getSelectionModel().setSelectionInterval(row, row);
// datatypetable.getColumnModel().getSelectionModel().setSelectionInterval(col, col);
// datatypetable.editCellAt(row, col);
if (col == colCount - 1) {
addColumn();
int selectedRow = 0;
datatypetable.editCellAt(selectedRow, 0);
datatypetable.setSurrendersFocusOnKeystroke(true);
datatypetable.getEditorComponent().requestFocus();
datatypetable.scrollRectToVisible(datatypetable.getCellRect(colCount, 0, true));
}
}
}
private void addRow() {
DefaultTableModel model = (DefaultTableModel) datatypetable.getModel();
if (model != null) {
Vector v = new Vector(1);
// for (int j = 0; j <= datatypetable.getColumnCount(); j++) {
// v.add("");
// }
v.add("");
model.addRow(v);
}
}
private void addColumn() {
DefaultTableModel model = (DefaultTableModel) datatypetable.getModel();
if (model != null) {
Vector v = new Vector(1);
// for (int j = 0; j < datatypetable.getRowCount(); j++) {
// v.add("");
// }
v.add("");
model.addColumn(Character.toString((char) ('A' + datatypetable.getColumnCount())), v);
}
}
Upvotes: 2
Reputation: 347332
I want that when I have written something in that column after that column should be generated...otherwise not.....
Okay, so basically, when you determine that you're at the last column and the Tab key is pressed, you want to determine if the current column has any values, if it contains any "empty" cells, then it will simply move to the next row/first column, otherwise it will add a new column.
The following tests assumes that empty cells are cells with null
values, so if your needs are different, you will need to update the code ti support it.
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.util.Vector;
import javax.swing.AbstractAction;
import javax.swing.ActionMap;
import javax.swing.InputMap;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.KeyStroke;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.table.DefaultTableModel;
public class Test {
public static void main(String[] args) {
new Test();
}
public Test() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
JTable table = new JTable();
table.setGridColor(Color.GRAY);
table.setShowGrid(true);
DefaultTableModel model = new DefaultTableModel();
for (int col = 0; col < 10; col++) {
Vector<String> colData = new Vector<String>(10);
for (int row = 0; row < 10; row++) {
colData.add(row + "x" + col);
}
model.addColumn(Character.toString((char)('A' + col)), colData);
}
table.setModel(model);
InputMap im = table.getInputMap();
im.put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), "Action.NextCell");
im.put(KeyStroke.getKeyStroke(KeyEvent.VK_TAB, 0), "Action.NextCell");
ActionMap am = table.getActionMap();
am.put("Action.NextCell", new NextCellActioin(table, model));
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new JScrollPane(table));
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class NextCellActioin extends AbstractAction {
private JTable table;
private DefaultTableModel model;
public NextCellActioin(JTable table, DefaultTableModel model) {
this.table = table;
this.model = model;
}
@Override
public void actionPerformed(ActionEvent e) {
int col = table.getSelectedColumn();
int row = table.getSelectedRow();
int colCount = table.getColumnCount();
int rowCount = table.getRowCount();
col++;
boolean newColumn = true;
if (col >= colCount) {
int lastColumn = colCount - 1;
for (int index = 0; index < table.getRowCount(); index++) {
if (table.getValueAt(index, lastColumn) == null) {
newColumn = false;
break;
}
}
if (!newColumn) {
col = 0;
row++;
}
}
if (newColumn) {
String name = Character.toString((char) ('A' + model.getColumnCount() + 1));
model.addColumn(name);
col++;
} else {
if (row >= rowCount) {
row = 0;
}
}
table.getSelectionModel().setSelectionInterval(row, row);
table.getColumnModel().getSelectionModel().setSelectionInterval(col, col);
}
}
}
when we double click on that column
I assume you want to able to add a new column when the last column is double clicked. The basic idea is the same as the one presented in the key binding above, you calculate the column which was, if it's the last one, you determine if it contains any "empty" values or not and add the column as required
table.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
if (SwingUtilities.isLeftMouseButton(e) && e.getClickCount() == 2) {
int viewCol = table.columnAtPoint(e.getPoint());
if (viewCol == table.getColumnCount()) {
// You know, check there are values
// make new column as needed
}
}
}
});
Upvotes: 0
Reputation: 400
You can add column like this.
DefaultTableModel model = new DefaultTableModel();
JTable table = new JTable(model);
//add you dynamic column name logic. ex, I am adding with i loop variable.
for( int i=1; i<5;i++){
model.addColumn("Col"+i);
}
Hope you got way to add column.
Upvotes: 1