Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

adding a row to a jtable by pressing a key  RSS feed

 
Steffen Durale
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi there again!
i want to add a row to a jtable by pressing a key and delete a row by pressing another key. the row should have default values in each of the 5 columns.
i just looked into my javabook but i didnt find a method for that....
HELP me... its urgend
greetings
Steffen Durale
 
raddikrishna kittur
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I a have a following program which deletes a from the table .
HOpe this helps you
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;
import javax.swing.event .*;

public class SwingTableDeleteRow extends JFrame implements ActionListener{
JButton btn;
JTable table;
DefaultTableModel tableModel;
String s="";
String [] columns ={"product ID", "Description ","Price"};
Object [] [] cells = { columns,{"asds-23","Video Card", "$50"},
{"as-234","Audio Card", "$150"}};
SwingTableDeleteRow(){
Container cp = getContentPane();
btn =new JButton("select");
btn.addActionListener(this);
tableModel = new DefaultTableModel(cells ,columns);
table = new JTable(tableModel);
cp.add(table);
cp.add("South" , btn);
setSize(400,400);
setVisible(true);

}
public void actionPerformed(ActionEvent ae){
tableModel.removeRow(0);
}
public static void main(String str[]){

new SwingTableDeleteRow();
}
}
 
raddikrishna kittur
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I a have a following program which deletes a from the table .
HOpe this helps you
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;
import javax.swing.event .*;

public class SwingTableDeleteRow extends JFrame implements ActionListener{
JButton btn;
JTable table;
DefaultTableModel tableModel;
String s="";
String [] columns ={"product ID", "Description ","Price"};
Object [] [] cells = { columns,{"asds-23","Video Card", "$50"},
{"as-234","Audio Card", "$150"}};
SwingTableDeleteRow(){
Container cp = getContentPane();
btn =new JButton("select");
btn.addActionListener(this);
tableModel = new DefaultTableModel(cells ,columns);
table = new JTable(tableModel);
cp.add(table);
cp.add("South" , btn);
setSize(400,400);
setVisible(true);

}
public void actionPerformed(ActionEvent ae){
tableModel.removeRow(0);
}
public static void main(String str[]){

new SwingTableDeleteRow();
}
}
 
Tauseef Ahmad
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
import javax.swing.JTable;
import javax.swing.table.TableColumn;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.lang.*;
public class AddRowInJtable extends JFrame implements ActionListener{
private boolean DEBUG = true;
public DefaultTableModel myModel;

final Object[] nrow = {"Tauseef", "Ahmad", "Teaching high school", new Integer(4), new Boolean(false)};
public AddRowInJtable() {
super("AddRowInJtable");

final String[] columnNames = {"First Name", "Last Name", "Sport", "# of Years", "Vegetarian"};
final Object[] nRow = {"From", "Pakistan", "Teaching high school", new Integer(4), new Boolean(false)};
myModel = new DefaultTableModel(columnNames,0);
JTable table = new JTable(myModel);
myModel.addRow(nRow);
table.setPreferredScrollableViewportSize(new Dimension(500, 70));

//Create the scroll pane and add the table to it.
JScrollPane scrollPane = new JScrollPane(table);
setUpSportColumn(table.getColumnModel().getColumn(2));
//addig button and action listener
JButton anRow=new JButton("ADD");
anRow.addActionListener(this);
//Add the scroll pane to this window.
getContentPane().add(scrollPane, BorderLayout.CENTER);
getContentPane().add(anRow, BorderLayout.NORTH);

addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}

public void actionPerformed(ActionEvent ae){
myModel.addRow(nrow);
}


public void setUpSportColumn(TableColumn sportColumn) {
//Set up the editor for the sport cells.
JComboBox comboBox = new JComboBox();
comboBox.addItem("Snowboarding");
comboBox.addItem("Rowing");
comboBox.addItem("Chasing toddlers");
comboBox.addItem("Speed reading");
comboBox.addItem("Teaching high school");
comboBox.addItem("None");
sportColumn.setCellEditor(new DefaultCellEditor(comboBox));
//Set up tool tips for the sport cells.
DefaultTableCellRenderer renderer =
new DefaultTableCellRenderer();
renderer.setToolTipText("Click for combo box");
sportColumn.setCellRenderer(renderer);
//Set up tool tip for the sport column header.
TableCellRenderer headerRenderer = sportColumn.getHeaderRenderer();
if (headerRenderer instanceof DefaultTableCellRenderer) {
((DefaultTableCellRenderer)headerRenderer).setToolTipText(
"Click the sport to see a list of choices");
}
}
public static void main(String[] args) {
AddRowInJtable frame = new AddRowInJtable();
frame.pack();
frame.setVisible(true);
}
}
[ March 07, 2003: Message edited by: Tauseef Ahmad ]
 
Mathew Kuruvilla
Ranch Hand
Posts: 145
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
<code>
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;
import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellRenderer;
import java.util.Vector;
public class TableTest2 extends JPanel {
JPanel topPanel;
JTable testTable;
JScrollPane scrollPane;
AbstractAction addRowAction, deleteRowAction;
DefaultTableModel testTableModel;
MouseListener testTableMouseListener;
Object[][] initialTableData;
String[] columnHeaders;
TableCellRenderer buttonCellRenderer;
int selectedRow = -1;
public TableTest2() {
createDefaultComponents();
testTableModel.setColumnIdentifiers(columnHeaders);
testTableModel.addRow(initialTableData[0]);
testTableModel.addRow(initialTableData[1]);
testTableModel.addRow(initialTableData[2]);
testTable.setDefaultRenderer(JButton.class, buttonCellRenderer);
testTable.setModel(testTableModel);
testTable.addMouseListener(testTableMouseListener);
setLayout(new BorderLayout());
add(scrollPane, BorderLayout.CENTER);
}
public void createDefaultComponents() {
testTableMouseListener = new MouseAdapter() {
public void mouseClicked(MouseEvent me) {
int row = TableTest2.this.testTable.rowAtPoint(me.getPoint());
int col = TableTest2.this.testTable.columnAtPoint(me.getPoint());
Object clickedObject = (JButton) TableTest2.this.testTable.
getValueAt(row, col);
if (clickedObject instanceof JButton) {
JButton clickedButton = (JButton) clickedObject;
TableTest2.this.setSelectedRow(row);
clickedButton.doClick();
}
}
};
addRowAction = new AbstractAction("Add Row") {
public void actionPerformed(ActionEvent ae) {
Vector newRow =
new Vector((Vector) TableTest2.this.testTableModel.getDataVector().
elementAt(TableTest2.this.selectedRow));
TableTest2.this.testTableModel.insertRow(
TableTest2.this.selectedRow+1, newRow);
}
};
deleteRowAction = new AbstractAction("Delete Row") {
public void actionPerformed(ActionEvent ae) {
TableTest2.this.testTableModel.removeRow(
TableTest2.this.selectedRow);
}
};
buttonCellRenderer = new TableCellRenderer() {
public Component getTableCellRendererComponent(
JTable table, Object value, boolean isSelected,
boolean hasFocus, int row, int column) {
if (!(value instanceof JButton))
System.err.println("WARNING value is not an instance of JButton");
return (Component) value;
}
};
testTableModel = new DefaultTableModel() {
public boolean isCellEditable(int row, int column) {
return false;
}
public Class getColumnClass(int column) {
return getValueAt(0, column).getClass();
}
};
columnHeaders = new String[] {"", "Column 1", "Column 2", "Column 3", ""};
initialTableData = new Object[][]
{
{new JButton(addRowAction),
"type1Col1", "type1Col2", "type1Col3",
new JButton(deleteRowAction)
},
{new JButton(addRowAction),
"type2Col1", "type2Col2", "type2Col3",
new JButton(deleteRowAction)
},
{new JButton(addRowAction),
"type3Col1", "type3Col2", "type3Col3",
new JButton(deleteRowAction)
}
};
testTable = new JTable();
scrollPane = new JScrollPane(testTable);
topPanel = new JPanel();
}
public void setSelectedRow(int row) {
selectedRow = row;
}
public static void main( String args[]) {
JFrame frame = new JFrame();
frame.getContentPane().add(new TableTest2());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.show();
}
}
</code>
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!