Results 1 to 14 of 14
  1. #1
    daredavil82 is offline Member
    Join Date
    Nov 2007
    Posts
    12
    Rep Power
    0

    Default 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;
    }

    }

  2. #2
    revathi17 is offline Member
    Join Date
    Aug 2007
    Posts
    26
    Rep Power
    0

    Default

    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??

  3. #3
    daredavil82 is offline Member
    Join Date
    Nov 2007
    Posts
    12
    Rep Power
    0

    Default 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...

  4. #4
    ShoeNinja's Avatar
    ShoeNinja is offline Senior Member
    Join Date
    Oct 2007
    Posts
    124
    Rep Power
    0

    Default

    You need to have a main method somewhere in the project.

    Java Code:
    public static void main(String[] args){
    //instantiate objects and call methods.
    }

  5. #5
    daredavil82 is offline Member
    Join Date
    Nov 2007
    Posts
    12
    Rep Power
    0

    Default

    i already try to put this code.but still cannot work..

    public static void main(String[] args){
    TableModelDemo table=new TableModelDemo();
    }

  6. #6
    ShoeNinja's Avatar
    ShoeNinja is offline Senior Member
    Join Date
    Oct 2007
    Posts
    124
    Rep Power
    0

    Default

    Are you getting a compiler error or what?

  7. #7
    daredavil82 is offline Member
    Join Date
    Nov 2007
    Posts
    12
    Rep Power
    0

    Default 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..

  8. #8
    revathi17 is offline Member
    Join Date
    Aug 2007
    Posts
    26
    Rep Power
    0

    Default

    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

  9. #9
    marlonviado is offline Member
    Join Date
    Sep 2011
    Posts
    2
    Rep Power
    0

    Default 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);
    }

    }

  10. #10
    marlonviado is offline Member
    Join Date
    Sep 2011
    Posts
    2
    Rep Power
    0

    Default insert row and column and delete row and column

    Just copy the whole code that i posted

  11. #11
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,458
    Blog Entries
    7
    Rep Power
    20

    Default Re: insert row and column and delete row and column

    Quote Originally Posted by marlonviado View Post
    Just copy the whole code that i posted
    Cute, spoonfeeding.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  12. #12
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,458
    Blog Entries
    7
    Rep Power
    20

    Default Re: insert row and column and delete row and column

    Quote Originally Posted by marlonviado View Post
    Just copy the whole code that i posted
    Cute, spoonfeeding.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  13. #13
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,966
    Rep Power
    19

    Default 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.

  14. #14
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,458
    Blog Entries
    7
    Rep Power
    20

    Default Re: insert row and column and delete row and column

    Quote Originally Posted by Tolls View Post
    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.
    And I wonder how people are able to find those stone old thread ... I should've paid better attention *grmbl*

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •