Results 1 to 14 of 14
- 11-29-2007, 06:23 AM #1
Member
- Join Date
- Nov 2007
- Posts
- 12
- Rep Power
- 0
insert row and column and delete row and column
hiiii....im new here...can any one help me to solve the problem of this coding...i can compile but cannot run the code...plz help me....
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;
import java.io.*;
import java.util.Vector;
public class TableModelDemo extends JApplet {
// Create table column names
private String[] columnNames =
{"Country", "Capital", "Population in Millions", "Democracy"};
// Create table data
private Object[][] rowData = {
{"USA", "Washington DC", 280, true},
{"Canada", "Ottawa", 32, true},
{"United Kingdom", "London", 60, true},
{"Germany", "Berlin", 83, true},
{"France", "Paris", 60, true},
{"Norway", "Oslo", 4.5, true},
{"India", "New Deli", 1046, true}
};
// Create a table model
private DefaultTableModel tableModel = new DefaultTableModel(
rowData, columnNames);
// Create a table
private JTable jTable1 = new JTable(tableModel);
// Create buttons
private JButton jbtAddRow = new JButton("Add New Row");
private JButton jbtAddColumn = new JButton("Add New Column");
private JButton jbtDeleteRow = new JButton("Delete Selected Row");
private JButton jbtDeleteColumn = new JButton(
"Delete Selected Column");
private JButton jbtSave = new JButton("Save");
private JButton jbtClear = new JButton("Clear");
private JButton jbtRestore = new JButton("Restore");
// Create a combo box for selection modes
private JComboBox jcboSelectionMode =
new JComboBox(new String[] {"SINGLE_SELECTION",
"SINGLE_INTERVAL_SELECTION", "MULTIPLE_INTERVAL_SELECTION"});
// Create check boxes
private JCheckBox jchkRowSelectionAllowed =
new JCheckBox("RowSelectionAllowed", true);
private JCheckBox jchkColumnSelectionAllowed =
new JCheckBox("ColumnSelectionAllowed", false);
public TableModelDemo() {
JPanel panel1 = new JPanel();
panel1.setLayout(new GridLayout(2, 2));
panel1.add(jbtAddRow);
panel1.add(jbtAddColumn);
panel1.add(jbtDeleteRow);
panel1.add(jbtDeleteColumn);
JPanel panel2 = new JPanel();
panel2.add(jbtSave);
panel2.add(jbtClear);
panel2.add(jbtRestore);
JPanel panel3 = new JPanel();
panel3.setLayout(new BorderLayout(5, 0));
panel3.add(new JLabel("Selection Mode"), BorderLayout.WEST);
panel3.add(jcboSelectionMode, BorderLayout.CENTER);
JPanel panel4 = new JPanel();
panel4.setLayout(new FlowLayout(FlowLayout.LEFT));
panel4.add(jchkRowSelectionAllowed);
panel4.add(jchkColumnSelectionAllowed);
JPanel panel5 = new JPanel();
panel5.setLayout(new GridLayout(2, 1));
panel5.add(panel3);
panel5.add(panel4);
JPanel panel6 = new JPanel();
panel6.setLayout(new BorderLayout());
panel6.add(panel1, BorderLayout.SOUTH);
panel6.add(panel2, BorderLayout.CENTER);
add(panel5, BorderLayout.NORTH);
add(new JScrollPane(jTable1),
BorderLayout.CENTER);
add(panel6, BorderLayout.SOUTH);
// Initialize table selection mode
jTable1.setSelectionMode(ListSelectionModel.SINGLE _SELECTION);
jbtAddRow.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (jTable1.getSelectedRow() >= 0)
tableModel.insertRow (jTable1.getSelectedRow(),
new java.util.Vector());
else
tableModel.addRow(new java.util.Vector());
}
});
jbtAddColumn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String name = JOptionPane.showInputDialog("New Column Name");
tableModel.addColumn(name, new java.util.Vector());
}
});
jbtDeleteRow.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(jTable1.getSelectedRow() >= 0)
tableModel.removeRow(jTable1.getSelectedRow());
}
});
jbtDeleteColumn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (jTable1.getSelectedColumn() >= 0) {
TableColumnModel columnModel = jTable1.getColumnModel();
TableColumn tableColumn =
columnModel.getColumn(jTable1.getSelectedColumn()) ;
columnModel.removeColumn(tableColumn);
}
}
});
jbtSave.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
ObjectOutputStream out = new ObjectOutputStream(
new FileOutputStream("tablemodel.dat"));
out.writeObject(tableModel.getDataVector());
out.writeObject(getColumnNames());
out.close();
}
catch (Exception ex) {
ex.printStackTrace();
}
}
});
jbtClear.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
tableModel.setRowCount(0);
}
});
jbtRestore.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
ObjectInputStream in = new ObjectInputStream(
new FileInputStream("tablemodel.dat"));
Vector rowData = (Vector)in.readObject();
Vector columnNames = (Vector)in.readObject();
tableModel.setDataVector(rowData, columnNames);
in.close();
}
catch (Exception ex) {
ex.printStackTrace();
}
}
});
jchkRowSelectionAllowed.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
jTable1.setRowSelectionAllowed(
jchkRowSelectionAllowed.isSelected());
}
});
jchkColumnSelectionAllowed.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
jTable1.setColumnSelectionAllowed(
jchkColumnSelectionAllowed.isSelected());
}
});
jcboSelectionMode.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String selectedItem =
(String) jcboSelectionMode.getSelectedItem();
if (selectedItem.equals("SINGLE_SELECTION"))
jTable1.setSelectionMode(
ListSelectionModel.SINGLE_SELECTION);
else if (selectedItem.equals("SINGLE_INTERVAL_SELECTION"))
jTable1.setSelectionMode(
ListSelectionModel.SINGLE_INTERVAL_SELECTION);
else if (selectedItem.equals("MULTIPLE_INTERVAL_SELECTION" ))
jTable1.setSelectionMode(
ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
}
});
}
private Vector getColumnNames() {
Vector<String> columnNames = new Vector<String>();
for (int i = 0; i < jTable1.getColumnCount(); i++)
columnNames.add(jTable1.getColumnName(i));
return columnNames;
}
}
- 11-29-2007, 08:56 AM #2
Member
- Join Date
- Aug 2007
- Posts
- 26
- Rep Power
- 0
From the code you have posted I could see a problem only in this line
jTable1.setSelectionMode(ListSelectionModel.SINGLE _SELECTION);
There is a space before the underscore. When i removed it, I was able to compile and run the code fine.
It seems to work fine for me...
Do you get any errors??
- 11-29-2007, 01:57 PM #3
Member
- Join Date
- Nov 2007
- Posts
- 12
- Rep Power
- 0
reply.....
went i compile run the code...it will come out masage....
java.lang.NoSuchMethodError: main
Exception in thread "main"
Process completed.
any one can solve this problem plz...
- 11-29-2007, 06:20 PM #4
You need to have a main method somewhere in the project.
Java Code:public static void main(String[] args){ //instantiate objects and call methods. }
- 11-29-2007, 06:58 PM #5
Member
- Join Date
- Nov 2007
- Posts
- 12
- Rep Power
- 0
i already try to put this code.but still cannot work..
public static void main(String[] args){
TableModelDemo table=new TableModelDemo();
}
- 11-29-2007, 07:11 PM #6
Are you getting a compiler error or what?
- 11-30-2007, 09:06 AM #7
Member
- Join Date
- Nov 2007
- Posts
- 12
- Rep Power
- 0
no output
after compile and run i cannot see any output..wat can i see only blank output...i use jCreator 4 to compile and run..
- 11-30-2007, 11:32 AM #8
Member
- Join Date
- Aug 2007
- Posts
- 26
- Rep Power
- 0
You need to run the program as Java applet instead of a Java application. I use eclipse and I run your program as Java applet it works fine. I am not sure how you do that in JCreator.
If you run as JApplet the init() method of the applet is automatically invoked and you don't need a 'main()' method in that case.
-R
- 09-22-2011, 05:13 PM #9
Member
- Join Date
- Sep 2011
- Posts
- 2
- Rep Power
- 0
marlon
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;
import java.io.*;
import java.util.Vector;
public class TableModelDemo extends JFrame {
// Create table column names
private String[] columnNames =
{"Country", "Capital", "Population in Millions", "Democracy"};
// Create table data
private Object[][] rowData = {
{"USA", "Washington DC", 280, true},
{"Canada", "Ottawa", 32, true},
{"United Kingdom", "London", 60, true},
{"Germany", "Berlin", 83, true},
{"France", "Paris", 60, true},
{"Norway", "Oslo", 4.5, true},
{"India", "New Deli", 1046, true}
};
// Create a table model
private DefaultTableModel tableModel = new DefaultTableModel(
rowData, columnNames);
// Create a table
private JTable jTable1 = new JTable(tableModel);
// Create buttons
private JButton jbtAddRow = new JButton("Add New Row");
private JButton jbtAddColumn = new JButton("Add New Column");
private JButton jbtDeleteRow = new JButton("Delete Selected Row");
private JButton jbtDeleteColumn = new JButton(
"Delete Selected Column");
private JButton jbtSave = new JButton("Save");
private JButton jbtClear = new JButton("Clear");
private JButton jbtRestore = new JButton("Restore");
// Create a combo box for selection modes
private JComboBox jcboSelectionMode =
new JComboBox(new String[] {"SINGLE_SELECTION",
"SINGLE_INTERVAL_SELECTION", "MULTIPLE_INTERVAL_SELECTION"});
// Create check boxes
private JCheckBox jchkRowSelectionAllowed =
new JCheckBox("RowSelectionAllowed", true);
private JCheckBox jchkColumnSelectionAllowed =
new JCheckBox("ColumnSelectionAllowed", false);
public TableModelDemo() {
JPanel panel1 = new JPanel();
panel1.setLayout(new GridLayout(2, 2));
panel1.add(jbtAddRow);
panel1.add(jbtAddColumn);
panel1.add(jbtDeleteRow);
panel1.add(jbtDeleteColumn);
JPanel panel2 = new JPanel();
panel2.add(jbtSave);
panel2.add(jbtClear);
panel2.add(jbtRestore);
JPanel panel3 = new JPanel();
panel3.setLayout(new BorderLayout(5, 0));
panel3.add(new JLabel("Selection Mode"), BorderLayout.WEST);
panel3.add(jcboSelectionMode, BorderLayout.CENTER);
JPanel panel4 = new JPanel();
panel4.setLayout(new FlowLayout(FlowLayout.LEFT));
panel4.add(jchkRowSelectionAllowed);
panel4.add(jchkColumnSelectionAllowed);
JPanel panel5 = new JPanel();
panel5.setLayout(new GridLayout(2, 1));
panel5.add(panel3);
panel5.add(panel4);
JPanel panel6 = new JPanel();
panel6.setLayout(new BorderLayout());
panel6.add(panel1, BorderLayout.SOUTH);
panel6.add(panel2, BorderLayout.CENTER);
add(panel5, BorderLayout.NORTH);
add(new JScrollPane(jTable1),
BorderLayout.CENTER);
add(panel6, BorderLayout.SOUTH);
// Initialize table selection mode
jTable1.setSelectionMode(ListSelectionModel.SINGLE _SELECTION);
jbtAddRow.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (jTable1.getSelectedRow() >= 0)
tableModel.insertRow (jTable1.getSelectedRow(),
new java.util.Vector());
else
tableModel.addRow(new java.util.Vector());
}
});
jbtAddColumn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String name = JOptionPane.showInputDialog("New Column Name");
tableModel.addColumn(name, new java.util.Vector());
}
});
jbtDeleteRow.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(jTable1.getSelectedRow() >= 0)
tableModel.removeRow(jTable1.getSelectedRow());
}
});
jbtDeleteColumn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (jTable1.getSelectedColumn() >= 0) {
TableColumnModel columnModel = jTable1.getColumnModel();
TableColumn tableColumn =
columnModel.getColumn(jTable1.getSelectedColumn()) ;
columnModel.removeColumn(tableColumn);
}
}
});
jbtSave.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
ObjectOutputStream out = new ObjectOutputStream(
new FileOutputStream("tablemodel.dat"));
out.writeObject(tableModel.getDataVector());
out.writeObject(getColumnNames());
out.close();
}
catch (Exception ex) {
ex.printStackTrace();
}
}
});
jbtClear.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
tableModel.setRowCount(0);
}
});
jbtRestore.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
ObjectInputStream in = new ObjectInputStream(
new FileInputStream("tablemodel.dat"));
Vector rowData = (Vector)in.readObject();
Vector columnNames = (Vector)in.readObject();
tableModel.setDataVector(rowData, columnNames);
in.close();
}
catch (Exception ex) {
ex.printStackTrace();
}
}
});
jchkRowSelectionAllowed.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
jTable1.setRowSelectionAllowed(
jchkRowSelectionAllowed.isSelected());
}
});
jchkColumnSelectionAllowed.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
jTable1.setColumnSelectionAllowed(
jchkColumnSelectionAllowed.isSelected());
}
});
jcboSelectionMode.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String selectedItem =
(String) jcboSelectionMode.getSelectedItem();
if (selectedItem.equals("SINGLE_SELECTION"))
jTable1.setSelectionMode(
ListSelectionModel.SINGLE_SELECTION);
else if (selectedItem.equals("SINGLE_INTERVAL_SELECTION"))
jTable1.setSelectionMode(
ListSelectionModel.SINGLE_INTERVAL_SELECTION);
else if (selectedItem.equals("MULTIPLE_INTERVAL_SELECTION" ))
jTable1.setSelectionMode(
ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
}
});
}
private Vector getColumnNames() {
Vector<String> columnNames = new Vector<String>();
for (int i = 0; i < jTable1.getColumnCount(); i++)
columnNames.add(jTable1.getColumnName(i));
return columnNames;
}
public static void main(String[] args) {
TableModelDemo g = new TableModelDemo();
g.setBounds(1,1,600,500);
g.setVisible(true);
}
}
- 09-22-2011, 05:20 PM #10
Member
- Join Date
- Sep 2011
- Posts
- 2
- Rep Power
- 0
insert row and column and delete row and column
Just copy the whole code that i posted
- 09-22-2011, 05:30 PM #11
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
- 09-22-2011, 05:30 PM #12
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
- 09-22-2011, 07:03 PM #13
Moderator
- Join Date
- Apr 2009
- Posts
- 13,541
- Rep Power
- 27
Re: insert row and column and delete row and column
I would say the same thing, though I would hope the OP had finished their project at some point in the past 4 years.
I do wonder why people resurrect ancient threads like this one.
- 09-22-2011, 07:10 PM #14
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
Similar Threads
-
How to sort column in JTable
By johnt in forum AWT / SwingReplies: 3Last Post: 06-14-2008, 06:48 AM -
Excel Column
By Gajesh Tripathi in forum New To JavaReplies: 2Last Post: 04-13-2008, 03:26 PM -
CSV column titles
By javaplus in forum New To JavaReplies: 2Last Post: 01-24-2008, 04:45 PM -
Display Line# and Column# in JSP
By loganathan.lakshmanan in forum JavaServer Pages (JSP) and JSTLReplies: 3Last Post: 01-17-2008, 01:37 PM -
add a jlist column
By Alan in forum JCreatorReplies: 1Last Post: 05-28-2007, 05:51 AM
Bookmarks