How to put a jpopup menue in a cell of jtable

Hello,
I want to make a jtable in which I want in one of the columns each cell when clicked(with right button) to pop up a jpopu menu with 3 actions. First I thought to make it with combobox but because I want to be activated with right button and then with the left i have changed my mind to jpopup menu.
Can you tell me what kind of render and editor to use? I will be very grateful if you can send me some examples because I am new to the swing.
Thanx,
Miroslav

You probably want to add a mouse listener to the JTable itself.
There is some sample code below. It adjusts the location of the
menu, and it's source component so that the menu can opperate
on the proper object, rather than the source of the event.
On your table add the menu as follows:
    table.addMouseListener(new PopupMouseListener(menu));You will have to decode the location to determine which column
the "Right Click" occurred on. You may then have to decode the
menu you want, (if you have different menus for different
columns/cells).
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
import com.ovotron.util.Debug;
public class PopupMouseListener extends MouseAdapter {
    private JPopupMenu menu    = null;
    private Component  invoker = null;
    public PopupMouseListener(JPopupMenu menu) {
        super();
        this.menu = menu;
    public PopupMouseListener(JPopupMenu menu, Component invoker) {
        this(menu);
        this.invoker = invoker;
    public void mousePressed(MouseEvent e) {
         int mod = e.getModifiers();
         if ((mod & MouseEvent.BUTTON3_MASK) == MouseEvent.BUTTON3_MASK) {
             if (menu == null) {
                 return;
             int       x     = e.getX();
             int       y     = e.getY();
             Component comp  = (Component)e.getSource();
             if (invoker != null) {
                 // Event (X,Y), are relative to the source, need to
                 // re-calculate to be relative to the invoker.
                 Point src = comp.getLocationOnScreen();
                 Point loc = invoker.getLocationOnScreen();
                 x = x - (loc.x - src.x);
                 y = y - (loc.y - src.y);
                 comp = invoker;
             menu.show(comp, x, y);
}

Similar Messages

  • How to put JTextfield and JButton in a cell of JTable

    I'm trying to put two Components into one cell of a JTable. This is no
    problem (in the TableCellRenderer) unless it comes to editing in these
    cells. I have written my own CellEditor for the table.but there is one
    minor problem: Neither the JTextfield nor the JButton has the focus so I
    can't make any input! Does anybody know what's wrong, please help me for this issue ,plese urgent

    Here you go
    import java.awt.BorderLayout;
    import java.awt.Component;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.AbstractCellEditor;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JOptionPane;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.JTable;
    import javax.swing.JTextField;
    import javax.swing.table.DefaultTableModel;
    import javax.swing.table.TableCellEditor;
    public class TableEdit extends JFrame
         JTable table = new JTable();
         private String[] columnNames = {"non-edit1", "edit", "non-edit2"};
         public TableEdit()
              super("Table Edit");
              setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
              createGUI();
              setLocationRelativeTo(null);
              setSize(400, 300);
              setVisible(true);
         private void createGUI()
              table.setModel(new DefaultTableModel(3,3)
                   public int getColumnCount()
                        return 3;
                   public boolean isCellEditable(int row, int column)
                        if(column == 1)
                             return true;
                        return false;
                   public String getColumnName(int column)
                        return columnNames[column];
                   public int getRowCount()
                        return 3;
                   public Class getColumnClass(int column)
                        return String.class;
                   public Object getValueAt(int row, int column)
                        return row + "" + column;
              table.setRowHeight(20);
              table.setDefaultEditor(String.class, new MyTableEditor());
              JScrollPane sp = new JScrollPane(table);
              add(sp);
         public class MyTableEditor extends AbstractCellEditor
         implements TableCellEditor
              JPanel jp = new JPanel(new BorderLayout(5,5));
              JTextField tf = new JTextField();
              JButton btn = new JButton("...");
              public MyTableEditor()
                   jp.add(tf);
                   jp.add(btn, BorderLayout.EAST);
                   btn.addActionListener(new ActionListener(){
                        public void actionPerformed(ActionEvent e)
                             JOptionPane.showMessageDialog(null, "Clicked Lookup");
              public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column)
                   tf.setText(String.valueOf(value));
                   return jp;
              public Object getCellEditorValue()
                   return tf.getText();
         public static void main(String[] parms)
              new TableEdit();
    }

  • How can I get right data in a cell of JTable when table  enter editing

    how can I get right data in a cell of JTable when table enter editing

    how can I get right data in a cell of JTable when table enter editing

  • How to put drop down menu over iframe?

    I created drop down menu in A-Master with height 100 pixels.
    On a page which uses A-Master template I've put youtube video, example:
    <iframe width="1000" height="500" src="//www.youtube.com/embed/8eDuupJ9Uus" frameborder="0" allowfullscreen></iframe>
    and when I export HTML, Iframe video is over dropdown menu so I can't see this menu, iframe is on top of it, how can I place dropdown menu on top of this iframe?
    Tried to search whole internet with no good results, I have only found other people has same problem.
    ...and I have tried this "wmode="transparent" too:
    <iframe width="560" height="349"src="http://www.youtube.com/embed/rLVCjnEGrqQ?wmode=transparent"frameborder="0" allowfullscreen wmode="transparent"></iframe>
    was no good

    Your menu layer should be on top of everything!
    Regards,
    Frank

  • How to put flash as a table or cell background or put HTML elements on top of flash file?

    Hi Guys,
    Could anyone please suggest me that how to put flash file as a table or cell background. I want to design a website and want to put HTML elements like table, images, text on top of flash file. so its look like animation in background. please visit following websites for an example: http://www.gagudju-dreaming.com/ and http://disneyworld.disney.go.com/
    Please guide me ASAP, thanks a lot in advance.
    Nitz.

    Hi Nitz
    The first thing you must do is convert your layout from table based to css based, (tables are o/k for tabular data, but not layout) but if you have never used css for layout it may be a steep learning curve.
    My recommendation is to start with the following tutorial on converting a table based comp to a css layout -
    http://www.adobe.com/devnet/dreamweaver/articles/dw_fw_css_pt1.html
    More info on using css is available here - http://www.adobe.com/devnet/dreamweaver/css.html.
    Once you have converted your site the 'procedure' for using swf's as a background, (does not work well with flv's, but can be done using html5 video without a skin, to a limited extent) is the same as having a background-image resize automatically to the full browser view-port. The best way to learn how to do this is to view the code on a page using the background-image technique, view the code on this page - http://www.pziecina.com/indexold.php.
    However, instead of having a fluid layout I would recommend using a fixed layout size to start with, as this overcomes many problems. I have uploaded a basic test page to illustrate the idea to - http://www.pziecina.com/test_ideas/swfbg_test.html. As I said the page must be resized in this example in order to see the effect, (so do not forget to resize the browser, even if it is only by 1px), but if you use a fixed size layout and your swf's size fits the desired area correctly the resize should not be required.
    PZ
    www.pziecina.com

  • How to deactivate the context menu for data cells in web reports

    Hello,
    we would like to have the data cells (for example: values in euro) without context menu. I mean, right now, when users navigate in web reports and stop with a mouse over the data cell, text like "Context menu for 5.000 Highlighted" appears. Especially users don't want to see the exception cells with this information like
    "Context menu for 6.000 critical 4".
    Is it possible to deactivate this context info?
    Thanks and Regards,
    Nuran Adal
    Message was edited by: Nuran Adal

    Hello Nuran,
    I guess you want to disable the Tooltip and not the ContextMenu. Anyway, use the table interface for it.
    Have a look at paramter C_CELL_CONTENT
    In Method DATA_CELL.
    This one contains strings like:
    30,0
    Rebuild this String to:
    30,0
    Kind Regards,
    Alican

  • How can i create an event from a cell of JTable ?

    Hi , I am having a JTable which has two types of render : checkboxCellRender and DefaultTableCellRenderer
    i want to generate an event by clicking the checkbox which will do color changes for other cells . How can i do that ? Can i pass the events ?
    code snippet :
    class checkboxCellRenderExt extends checkboxCellRender
              public checkboxCellRenderExt(){}
              public Component getTableCellRendererComponent(JTable table, Object value,
                        boolean isSelected, boolean hasFocus,
                        int row, int column)
    JCheckBox c=(JCheckBox)super.getTableCellRendererComponent( table,value,isSelected,hasFocus,row, column);
    JTableModel md = (JTableModel)m_TablePan.getTable().getModel();
    Vector currentRow = md.getRow(row);
    if(isSelected)
    boolean m_bavail = ((Boolean)currentRow.get(COLUMN_TCM_STATUS)).booleanValue();
    if(!m_bavail)
         c.setForeground(Color.BLACK);
         c.setBackground(Color.WHITE);
    else
         c.setForeground(table.getSelectionForeground());
         c.setBackground(table.getSelectionBackground());
    else
    setForeground(table.getForeground());
    setBackground(table.getBackground());
    return this;
    class CustomcellRenderer extends DefaultTableCellRenderer
         public CustomcellRenderer()
         public Component getTableCellRendererComponent(JTable table, Object value,
                                  boolean isSelected, boolean hasFocus,
                                  int row, int column)
              checkboxCellRender chkbox =null;
              JLabel lb;
              Component c = super.getTableCellRendererComponent(table,value,isSelected,hasFocus,row, column);
              if (value == null)
         return c;
              if (value instanceof String)
                   String s = (String) value;
                   lb = (JLabel)c;
                   lb.setBorder (noFocusBorder);
                   lb.setText(s);
         JTableModel md = (JTableModel)m_TablePan.getTable().getModel();
         Vector currentRow = md.getRow(row);
              this.setHorizontalAlignment(JCheckBox.CENTER);
    if(isSelected)
                   boolean m_bavail = ((Boolean)currentRow.get(COLUMN_TCM_STATUS)).booleanValue();
                   if(!m_bavail)
                        c.setForeground(Color.BLACK);
                        c.setBackground(Color.WHITE);
                   else
                   c.setForeground(table.getSelectionForeground());
                   c.setBackground(table.getSelectionBackground());
    else
                   setForeground(table.getForeground());
                   setBackground(table.getBackground());
    return this;
    }

    Don't forget to use the Code Formatting Tags so the posted code retains its original formatting. That is done by selecting the code and then clicking on the "Code" button above the question input area.
    Add a TableModelListener to the TableModel. An event is fired when the contents of the cell are changed.

  • URGENT PLEASE ---How to open popup window after selecting a cell of JTable.

    Hi,
    I am struck at aproblem, I am developing an applet in which I am using TabbedPanes and each tabbed panel has either tables or some key information to display.
    My Problem is, I have a JTable, which has informations related to a perticular record, each record may or may not have some more hidden information. If a record has a hidden information, then the record's cell will contain a message " click here for more information " . after clicking this cell a window will open to show the hidden information.
    I have tried ListSelectionListener and MouseListener to the JTable but nothing is giving me the desired results.
    Please I you could suggest me about this.

    You say its urgent
    So urgent, so oh oh urgent
    Just wait and see
    How urgent my love can be
    Its urgent

  • How to change the Background color of a cell in JTable

    hi!
    Actually i want to change the background color of few cells in JTable
    is there any method to make this change in JTable ?
    and also is it possible to have 5 rows with single column and 5 rows with 3 columns in a single JTable

    i want to change the background color of few cells in JTableDepending on your requirements for the coloring of cells it may be easier to override the prepareRenderer() method of JTable:
    http://forum.java.sun.com/thread.jspa?forumID=57&threadID=610474

  • How to get the standard Menu of Query Designer into Web Template

    Hi Experts,
    is there a way on how to put the standard menu within Bex Query Designer (after executing a Query with the Query Designer you get into ad-hoc reporting with a menu including several functions like print, show as..., properties, etc) into an individual Bex Web Template?
    Copy & Paste source code will not work I guess because of several javascript functions I don't have access to.
    Thanks & Best regards
    Tobias

    BEx queries are displayed on the web using standard web template 0ANALYSIS_PATTERN or some customized version base don your implementation.
    You can either include the 0ANALYSIS_Pattern web template in your custom template or use Save as to create a new template with 0ANALYSIS_Pattern  AS THE STARTING POINT.

  • How can I add custom right-click-menu to column headers in JTable?

    Can anyone point me to a topic on how to customize a popup menu for column headers in JTable? Specifically, I want to add things like "auto-size column" and "hide column".
    Thanks,
    Matt

    Right-click on your table.  Then go to Advanced->Runtime Shortcut Menu->Edit.
    There are only two ways to tell somebody thanks: Kudos and Marked Solutions
    Unofficial Forum Rules and Guidelines

  • How to put separator lines in menu items in a gui status ?

    Hi,
    I have created gui status for my z transaction
    I need the separator lines in the menu items like it is seen in std transactions
    For example, in any screen , in the Help menu, we can see the following.
    Application Help
    SAP Library
    Glossary
    (line)
    Release notes
    (line)
    SAP Service Marketplace
    could you please help me how to put the lines ?
    thanks

    Hi,
    Goto Your GUI status.click on your  MENU bar.
    under the menu bar you will have your own menu items.
    where ever you want separator line  do the following way.
    you will have 2 columns under menu bar
    1) code
    2) text.
    under code dont put anything.
    but under text column you put dashes and enter or select right click there and choose separator line.
    you will get separator line.
    activate and run the ztcode.you will get .
    code                                            text
    Application Help                         
    SAP Library
    Glossary
    Release notes
    SAP Service Marketplace
    Thanks
    Parvathi

  • How can I create an internal hyperlink in iWeb? Would like to put an interactive menu at the top of a page.

    How can I create an internal hyperlink in iWeb? Would like to put an interactive menu at the top of a page.

    Do you meant a link to one of your other pages or to a location on one of your pages?  If it's the latter those are called anchors and iWeb does not support them.  However, there are workarounds for that.  See this blog: How to use anchors in iWeb.  If you don't have a lot of widgets on the page it can work fairly well.
    OT

  • How to put Menu in WebDynPro for ABAP

    How to put Menu in WebDynPro for ABAP

    What kind of menu do you want? Normally WD4A doesn't need any menu's. You can create (drag and drop) UI elements on your view and use navigation links to navigate from one view to another (for example). In classical dynrpo programming these buttons (back, cancel, exit, etc.) are used to navigate.
    But in WD4A 'menu' is created using the UI elements like button, inputfield, textview, table (grid), etc.
    So let me know what kind of menu you're talking about, and I might be able to help you.
    Kind regards,
    Micky.

  • I have bought a Macbook Pro Retina, and the iPhoto which comes with it, has a different iPhoto Book menu.  I cannot see how to put text at the bottom of the pages of photographs, instead of the default place in the middle.  Is there a way to do this?

    I have bought a Macbook Pro Retina, and the iPhoto which comes with it, has a different iPhoto Book menu.  I cannot see how to put text at the bottom of the pages of photographs, instead of the default place in the middle.  Is there a way to do this?

    There are different templates for iPhoto books and they differ from version of iPhoto to iPhoto, so explore the other options.

Maybe you are looking for

  • Issue with AP failover

    I have a WLC (4402) running version 5.2.193.0.  The WLC used to have both interfaces connected to a single switch (LAG was enabled) with etherchannel on the switch end.  It was changed such that each port on the WLC is connected to a different switch

  • Anti Virus for wIndows xp

    Hey Forum, i am using windows xp on boot camp and i would like to know which type of antivirus is free, good security and low processor usage(won't slow down ur com.). I look forward to ur favorable reply Regards Ala.

  • Scheduling type Backwards vs Backwards in time in OPU3

    Hello All, I have a question regarding the Scheduling type in OPU3. What is the difference between Backwards and Backwards in time? Thanks in advance George

  • Can Flash Change Screen Resolution??

    Dear All, i didn't find a complete way to change the screen resolution in flash?? is it any solution to do it without third party programs Please Advice

  • T42 work with Digital Camcorder

         I recently had to wipe my T42 thinkpad laptop clean.  I reinstalled most of the drivers I think, but not too sure if I installed extra junk ones.  So the extra help to lead me to the exact ones I need to make this computer fully functional witho