Results 1 to 2 of 2
- 03-21-2013, 04:53 AM #1
Member
- Join Date
- Sep 2012
- Posts
- 26
- Rep Power
- 0
how can i make Jtable Editable when i click the JMenuItem??
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.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 try this code: if(itmEditVal1.isSelected()){return column == 0;};
but nothing happens.
Any Suggestions or code sample would be appreciated :D
- 03-21-2013, 05:29 AM #2
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)
dbWhy do they call it rush hour when nothing moves? - Robin Williams
Similar Threads
-
Click on jmenuitem open the jinternalframe..
By mcajavaprogramer in forum AWT / SwingReplies: 5Last Post: 05-16-2012, 12:56 PM -
Table cells change to editable by button click action
By Zamioculcas in forum New To JavaReplies: 2Last Post: 11-24-2011, 09:50 PM -
Editable multiple line ToolTip on mouse right click
By smitharavi in forum AWT / SwingReplies: 1Last Post: 12-18-2010, 10:34 AM -
How to make a transparent JMenuItem
By Nuclear Mosquito in forum AWT / SwingReplies: 2Last Post: 07-28-2010, 09:53 PM -
How to make the column of Jtable editable
By man4ish in forum AWT / SwingReplies: 1Last Post: 01-06-2010, 03:53 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks