Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 11-29-2007, 06:23 AM
Member
 
Join Date: Nov 2007
Posts: 12
daredavil82 is on a distinguished road
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;
}

}
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 11-29-2007, 08:56 AM
Member
 
Join Date: Aug 2007
Posts: 22
revathi17 is on a distinguished road
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??
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 11-29-2007, 01:57 PM
Member
 
Join Date: Nov 2007
Posts: 12
daredavil82 is on a distinguished road
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...
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 11-29-2007, 06:20 PM
ShoeNinja's Avatar
Senior Member
 
Join Date: Oct 2007
Posts: 112
ShoeNinja is on a distinguished road
Send a message via AIM to ShoeNinja
You need to have a main method somewhere in the project.

Code:
public static void main(String[] args){ //instantiate objects and call methods. }
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 11-29-2007, 06:58 PM
Member
 
Join Date: Nov 2007
Posts: 12
daredavil82 is on a distinguished road
i already try to put this code.but still cannot work..

public static void main(String[] args){
TableModelDemo table=new TableModelDemo();
}
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 11-29-2007, 07:11 PM
ShoeNinja's Avatar
Senior Member
 
Join Date: Oct 2007
Posts: 112
ShoeNinja is on a distinguished road
Send a message via AIM to ShoeNinja
Are you getting a compiler error or what?
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 11-30-2007, 09:06 AM
Member
 
Join Date: Nov 2007
Posts: 12
daredavil82 is on a distinguished road
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..
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 11-30-2007, 11:32 AM
Member
 
Join Date: Aug 2007
Posts: 22
revathi17 is on a distinguished road
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
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to sort column in JTable johnt AWT / Swing 3 06-14-2008 06:48 AM
Excel Column Gajesh Tripathi New To Java 2 04-13-2008 03:26 PM
CSV column titles javaplus New To Java 2 01-24-2008 04:45 PM
Display Line# and Column# in JSP loganathan.lakshmanan JavaServer Pages (JSP) and JSTL 3 01-17-2008 01:37 PM
add a jlist column Alan JCreator 1 05-28-2007 05:51 AM


All times are GMT +3. The time now is 05:39 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org