wood burning stoves 2.0*
The moose likes Swing / AWT / SWT and the fly likes Problem in adding/deleting rows from Jtable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Problem in adding/deleting rows from Jtable" Watch "Problem in adding/deleting rows from Jtable" New topic
Author

Problem in adding/deleting rows from Jtable

Savvy Makol
Greenhorn

Joined: Jun 03, 2003
Posts: 6
I am trying to add /remove rows from JTable whose first column is JButton and others are JComboBox's.If no rows are selected,new row is added at the end.If user selects some row & then presses insert button,new row is added below it.Rows can only be deleted if user has made some selection.Kindly help me,where i am making mistake.If any function is to be used.My code is as follows....
*****************************************************************************
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;
import javax.swing.border.*;

public class JButtonTableExample extends JFrame implements ActionListener{
JComboBox mComboLHSType = new JComboBox();
JComboBox mComboRHSType = new JComboBox();
JLabel mLabelLHSType = new JLabel("LHS Type");
JLabel mLabelRHSType = new JLabel("RHS Type");
JButton mButtonDelete = new JButton("Delete");
JButton mButtonInsert = new JButton("Insert");
JPanel mPanelButton = new JPanel();
JPanel mPanelScroll = new JPanel();
JPanel mPanelCombo = new JPanel();
DefaultTableModel dm ;
JTable table;
int currentRow = -1;
static int mSelectedRow = -1;
public JButtonTableExample()
{
super( "JButtonTable Example" );
makeForm();

setSize( 410, 222 );
setVisible(true);
}
private void makeForm()
{
this.getContentPane().setLayout(null);
mPanelCombo.setLayout(null);
mPanelCombo.setBorder(new LineBorder(Color.red));
mPanelCombo.setBounds(new Rectangle(1,1,400,30));
mLabelLHSType.setBounds(new Rectangle(26,5,71,22));
mComboLHSType.setBounds(new Rectangle(83,5,100,22));
mLabelRHSType.setBounds(new Rectangle(232,5,71,22));
mComboRHSType.setBounds(new Rectangle(292,5,100,22));
mPanelCombo.add(mLabelLHSType,null);
mPanelCombo.add(mComboLHSType,null);
mPanelCombo.add(mLabelRHSType,null);
mPanelCombo.add(mComboRHSType,null);
mPanelScroll.setLayout(null);
mPanelScroll.setBorder(new LineBorder(Color.blue));
mPanelScroll.setBounds(new Rectangle(1,28,400,135));
mPanelButton.setLayout(null);
mPanelButton.setBorder(new LineBorder(Color.green));
mPanelButton.setBounds(new Rectangle(1,165,400,30));
mButtonInsert.setBounds(new Rectangle(120,5,71,22));
mButtonDelete.setBounds(new Rectangle(202,5,71,22));
mButtonDelete.addActionListener(this);
mButtonInsert.addActionListener(this);
mPanelButton.add(mButtonDelete,null);
mPanelButton.add(mButtonInsert,null);
dm = new DefaultTableModel();
//dm.setDataVector(null,
//new Object[]{"Button","Join","LHS","Operator","RHS"});
dm.setDataVector(new Object[][]{{"","","","",""}},
new Object[]{"","Join","LHS","Operator","RHS"});
table = new JTable(dm);
table.getTableHeader().setReorderingAllowed(false);
table.setRowHeight(25);
int columnWidth[] = {20,45,95,95,95};
TableColumnModel modelCol = table.getColumnModel();
for (int i=0;i<5;i++)
{
modelCol.getColumn(i).setPreferredWidth(columnWidth[i]);
}
//modelCol.getColumn(0).setCellRenderer(new ButtonRenderer());
//modelCol.getColumn(0).setCellEditor(new ButtonEditor(new JCheckBox()));
modelCol.getColumn(0).setCellRenderer(new ButtonCR());
modelCol.getColumn(0).setCellEditor(new ButtonCE(new JCheckBox()));
modelCol.getColumn(0).setResizable(false);
setUpJoinColumn(modelCol.getColumn(1));
setUpLHSColumn(modelCol.getColumn(2));
setUpOperColumn(modelCol.getColumn(3));
setUpRHSColumn(modelCol.getColumn(4));
JScrollPane scroll = new JScrollPane(table);
scroll.setBounds(new Rectangle(1,1,400,133));
mPanelScroll.add(scroll,null);

this.getContentPane().add(mPanelCombo,null);
this.getContentPane().add(mPanelScroll,null);
this.getContentPane().add(mPanelButton,null);
}//end of makeForm()
public void actionPerformed(ActionEvent ae)
{
if (ae.getSource() == mButtonInsert)
{
//int currentRow = table.getSelectedRow();
currentRow = ButtonCE.selectedRow;
System.out.println("Before Insert CURRENT ROW"+currentRow);
if(currentRow == -1)
{
int rowCount = dm.getRowCount();
//mSelectedRow = rowCount-1;
//table.clearSelection();
dm.insertRow(rowCount,new Object[]{"","","","",""});
currentRow = -1;
ButtonCE.selectedRow = -1;
}
else
{
table.clearSelection();
dm.insertRow(currentRow+1,new Object[]{"","","","",""});
currentRow = -1;
ButtonCE.selectedRow = -1;
}
//System.out.println("After INSERT CURRENT ROW"+currentRow);
}
if(ae.getSource() == mButtonDelete)
{
//int currentRow = table.getSelectedRow();
currentRow = ButtonCE.selectedRow;
System.out.println("Before DELETE CURRENT ROW"+currentRow);
if(currentRow != -1)
{
dm.removeRow(currentRow);
table.clearSelection();
currentRow = -1;
ButtonCE.selectedRow = -1;
//System.out.println("Selected Row"+mSelectedRow);
}
else
JOptionPane.showMessageDialog(null, "Select row first", "alert", JOptionPane.ERROR_MESSAGE);
//System.out.println("DELETE CURRENT ROW"+currentRow);
}
}

public void setUpJoinColumn(TableColumn joinColumn)
{
//Set up the editor for the sport cells.
JComboBox comboBox = new JComboBox();
comboBox.addItem("AND");
comboBox.addItem("OR");
comboBox.addItem("NOT");
joinColumn.setCellEditor(new DefaultCellEditor(comboBox));
//Set up tool tips for the sport cells.
DefaultTableCellRenderer renderer =
new DefaultTableCellRenderer();
renderer.setToolTipText("Click for combo box");
joinColumn.setCellRenderer(renderer);
//Set up tool tip for the sport column header.
TableCellRenderer headerRenderer = joinColumn.getHeaderRenderer();
if (headerRenderer instanceof DefaultTableCellRenderer) {
((DefaultTableCellRenderer)headerRenderer).setToolTipText(
"Click the sport to see a list of choices");
}
}
public void setUpLHSColumn(TableColumn LHSColumn)
{
//Set up the editor for the sport cells.
JComboBox comboBox = new JComboBox();
comboBox.addItem("Participant1");
comboBox.addItem("Participant2");
comboBox.addItem("Variable1");
LHSColumn.setCellEditor(new DefaultCellEditor(comboBox));
//Set up tool tips for the sport cells.
DefaultTableCellRenderer renderer =
new DefaultTableCellRenderer();
renderer.setToolTipText("Click for combo box");
LHSColumn.setCellRenderer(renderer);
//Set up tool tip for the sport column header.
TableCellRenderer headerRenderer = LHSColumn.getHeaderRenderer();
if (headerRenderer instanceof DefaultTableCellRenderer) {
((DefaultTableCellRenderer)headerRenderer).setToolTipText(
"Click the sport to see a list of choices");
}
}
public void setUpOperColumn(TableColumn operColumn)
{
//Set up the editor for the sport cells.
JComboBox comboBox = new JComboBox();
comboBox.addItem("=");
comboBox.addItem("!=");
comboBox.addItem("Contains");
operColumn.setCellEditor(new DefaultCellEditor(comboBox));
//Set up tool tips for the sport cells.
DefaultTableCellRenderer renderer =
new DefaultTableCellRenderer();
renderer.setToolTipText("Click for combo box");
operColumn.setCellRenderer(renderer);
//Set up tool tip for the sport column header.
TableCellRenderer headerRenderer = operColumn.getHeaderRenderer();
if (headerRenderer instanceof DefaultTableCellRenderer) {
((DefaultTableCellRenderer)headerRenderer).setToolTipText(
"Click the sport to see a list of choices");
}
}
public void setUpRHSColumn(TableColumn rhsColumn)
{
//Set up the editor for the sport cells.
JComboBox comboBox = new JComboBox();
comboBox.addItem("Variable1");
comboBox.addItem("Constant1");
comboBox.addItem("Constant2");
rhsColumn.setCellEditor(new DefaultCellEditor(comboBox));
//Set up tool tips for the sport cells.
DefaultTableCellRenderer renderer =
new DefaultTableCellRenderer();
renderer.setToolTipText("Click for combo box");
rhsColumn.setCellRenderer(renderer);
//Set up tool tip for the sport column header.
TableCellRenderer headerRenderer = rhsColumn.getHeaderRenderer();
if (headerRenderer instanceof DefaultTableCellRenderer) {
((DefaultTableCellRenderer)headerRenderer).setToolTipText(
"Click the sport to see a list of choices");
}
}

public static void main(String[] args) {
JButtonTableExample frame = new JButtonTableExample();
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
//Button as a renderer for the table cells
class ButtonCR implements TableCellRenderer
{
JButton btnSelect;
public ButtonCR()
{

btnSelect = new JButton();
btnSelect.setMargin(new Insets(0,0,0,0));
}

public Component getTableCellRendererComponent(JTable table,Object value,boolean isSelected,boolean hasFocus,int row,int column)
{

if (column != 0) return null; //meany !!!
//System.out.println("Inside renderer########################Selected row");
//btnSelect.setText(value.toString());
//btnSelect.setIcon(new ImageIcon("capsigma.gif"));
return btnSelect;
}
}//end fo ButtonCR
//Default Editor for table
class ButtonCE extends DefaultCellEditor implements ActionListener
{
JButton btnSelect;
JTable table;
//Object val;
static int selectedRow = -1;
public ButtonCE(JCheckBox whoCares)
{
super(whoCares);
//this.row = row;
btnSelect = new JButton();
btnSelect.setMargin(new Insets(0,0,0,0));
btnSelect.addActionListener(this);
setClickCountToStart(1);

}
public Component getTableCellEditorComponent(JTabletable,Object value,boolean isSelected,int row,int column)
{
if (column != 0) return null; //meany !!!
this.selectedRow = row;
this.table = table;
table.clearSelection();
System.out.println("Inside getTableCellEditorComponent");
return btnSelect;
}
//public Object getCellEditorValue()
//{
//return val;
//}
public void actionPerformed(ActionEvent e)
{
// Your Code Here...
System.out.println("Inside actionPerformed");
System.out.println("Action performed Row selected "+selectedRow);
btnSelect.setIcon(new ImageIcon("capsigma.gif"));
}
}//end of ButtonCE
VIJAY Yadlapati
Ranch Hand

Joined: Aug 04, 2003
Posts: 175
Your code works as you described - what's the problem
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problem in adding/deleting rows from Jtable
 
Similar Threads
Right Align numbers in Jtable
changing headers on JTable caused other problems
JTables & Combo Boxes
Why I can't catch NumberFormatException in the tableModel
adding a row to a jtable by pressing a key