Results 1 to 2 of 2
  1. #1
    raffs03 is offline Member
    Join Date
    Sep 2012
    Posts
    41
    Rep Power
    0

    Default how can i make Jtable Editable when i click the JMenuItem??

    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.util.*;
    import javax.swing.table.*;
    import java.awt.event.*;
    public class tbls extends JFrame {
        public tbls(){
             final JMenuItem itmEditVal1 = new JMenuItem("Edit 1st Value");
             final JMenuItem itmEditVal2 = new JMenuItem("Edit 2nd Value");
             final JMenuItem itmEditVal3 = new JMenuItem("Edit 3rd Value");
             final DefaultTableModel Mdl1 = new DefaultTableModel();
              final JPopupMenu pop = new JPopupMenu();
              pop.add(itmEditVal1);
              pop.add(itmEditVal2);
              pop.add(itmEditVal3);
             table = new JTable(Mdl1)
                    {
                @Override
                public boolean isCellEditable(int row, int column) {
                  
                    if(itmEditVal1.isSelected()){return column == 0;};
                    if(itmEditVal2.isSelected()){return column == 1;};
                    if(itmEditVal3.isSelected()){return column == 2;};
                     return false;
                   
                } 
            
            };
           table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
           Mdl1.addColumn("1st value");
           Mdl1.addColumn("2nd value");
           Mdl1.addColumn("3rd value");
           add(new JScrollPane(table));
           setVisible(true);
           setSize(350,150);
           setDefaultCloseOperation(EXIT_ON_CLOSE);
           setLayout(new GridLayout(1,1));
           setLocationRelativeTo(null);
           
           String[] st = {"1","2","3"};
           String[] sp = {"2","4","6"};
           String[] sv = {"3","6","9"};
           Mdl1.addRow(st);
           Mdl1.addRow(sp);
           Mdl1.addRow(sv);
           
           
            table.addMouseListener
                     (
                    new MouseAdapter() 
                    {
                        
                @Override
                        public void mousePressed(MouseEvent e) 
                      {
                        showPopup(e);
                      }
                
                @Override
                public void mouseReleased(MouseEvent e) {
                    showPopup(e);
                }
                        
                        private void showPopup(MouseEvent e)
                        {
                            if(e.isPopupTrigger())
                            {
                                pop.show(e.getComponent(),
                           e.getX(), e.getY());
                                
                            }
                        }
                    }
                    );
        }
        public static void main(String[]args){
           new tbls();
        }
       private JTable table;
       private JScrollPane scrollPane;
    }
    i'm having a hard time to figure out how to put the isCellEditable to JMenuItem. i want to do is when i click the "edit 1st value" in the popup the 1st value column will be editable but the other 2 column will not be editable, when i click the "edit 2nd value" in the popup the 2nd value column will be editable but the 1st and 3rd column will no be editable same as the edit 3rd value.
    I try this code: if(itmEditVal1.isSelected()){return column == 0;};
    but nothing happens.
    Any Suggestions or code sample would be appreciated :D

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,455
    Rep Power
    20

    Default Re: how can i make Jtable Editable when i click the JMenuItem??

    One option: use JCheckBoxMenuItem (to allow multiple selections) or JRadioButtonMenuItem (to allow a single selection, after the items have been added to a ButtonGroup).

    Aside, all Swing constructors and methods should be invoked on the EDT. Not on the Main thread.
    Lesson: Concurrency in Swing (The Java™ Tutorials > Creating a GUI With JFC/Swing)

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Click on jmenuitem open the jinternalframe..
    By mcajavaprogramer in forum AWT / Swing
    Replies: 5
    Last Post: 05-16-2012, 01:56 PM
  2. Table cells change to editable by button click action
    By Zamioculcas in forum New To Java
    Replies: 2
    Last Post: 11-24-2011, 10:50 PM
  3. Editable multiple line ToolTip on mouse right click
    By smitharavi in forum AWT / Swing
    Replies: 1
    Last Post: 12-18-2010, 11:34 AM
  4. How to make a transparent JMenuItem
    By Nuclear Mosquito in forum AWT / Swing
    Replies: 2
    Last Post: 07-28-2010, 10:53 PM
  5. How to make the column of Jtable editable
    By man4ish in forum AWT / Swing
    Replies: 1
    Last Post: 01-06-2010, 04:53 PM

Posting Permissions

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