Clickable buttons in jtable

Hi All,
I'm trying to add a JButton to a JTable. There are a few scripts out there that show how to transfer a mouse event from jtable to a button that is placed in its cell. I've added to it a response to mousemoved method to highlight a button when the mouse is over the cell with the button. Unfortunately I have not seen a script that shows the "standard" response of the button as it works on standard jpanels (including a display of the button being pressed down). I wonder are those scripts out there and I missed smthing.
I'm also wondering are there standard [free!!] converters from smthing like xml into jtables. may be not with all the functionality of html tables but with some of it. This would seem as a natural thing to write.
Thanx in advance

That's does not look inspiring so far. Let me rephrase the question. Is it true that I have to implement a custom jbutton that paints its pressed state to make the jbutton pressable in a jtable cell. Or I miss smthing in a standard implementation of jtable that would allow to do it w/o it.

Similar Messages

  • Clickable button in jtable header

    Here is a modification of camickr code to make buttons clickable in the jtable cells. The question is how to modify it to make them clickable in the table headers. It looks like there is quite a different behavior in cells and headers (naturally it probably should be this way). More specifically, I guess my question is how do you modify MyColHeaderRenderer class in the code below. Btw, this is also an example I've mentioned in one of the previous posts when a component responds to a press mouse event but not click event when first pressed.
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.table.*;
    * @author
    public class Example4a extends JFrame {
        MyCell[][] data = {{new MyCell("A1"), new MyCell("A2")}};
        MyColHeader[] headers = {new MyColHeader("col 1"), new MyColHeader("col 2")};
        /** Creates a new instance of Example5 */
        public Example4a() {
            setTable();
            setSize(200, 200);
            setVisible(true);
            super.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        public void setTable() {
            DefaultTableModel model = new DefaultTableModel(data, headers){
                public Class getColumnClass(int column) {
                    return MyCell.class;
            MyTable table = new MyTable(model);       
            JScrollPane scrollpane = new JScrollPane(table);
            JPanel top_panel = new JPanel();
            top_panel.setLayout(new BorderLayout());
            getContentPane().add(top_panel);
            top_panel.add(scrollpane, BorderLayout.CENTER);
        public static void main(String[] args) {
            Example4a ex = new Example4a();
        class MyTable extends JTable {
            public MyTable(TableModel model) {
                super(model);
                MyCellRenderer cell_renderer = new MyCellRenderer();
                MyColHeaderRenderer header_renderer = new MyColHeaderRenderer();
                TableColumnModel columnModel = getColumnModel();
                for(int i=0; i<columnModel.getColumnCount(); i++) {
                    TableColumn column = columnModel.getColumn(i);
                    column.setCellRenderer(cell_renderer);
                    column.setCellEditor(cell_renderer);
                    column.setHeaderRenderer(header_renderer);
                this.setRowHeight(50);
        class MyCellRenderer extends AbstractCellEditor
                implements TableCellRenderer, TableCellEditor {
            MyCell editCell;
            public MyCellRenderer() {
            public Component getTableCellRendererComponent(JTable table,
                Object value, boolean isSelected, boolean hasFocus, int row, int column) {
                return (MyCell)value;
            public Component getTableCellEditorComponent(
                    JTable table, Object value, boolean isSelected, int row, int column)  {
                editCell = (MyCell)value;
                return editCell;
            public Object getCellEditorValue() {       
                return editCell;
        class MyCell extends JPanel {
            String text;
            JButton button;
            public MyCell(String text_) {
                text = text_;
                setLayout(new GridBagLayout());
                button = new JButton(text);
                button.addMouseListener(new MouseListener() {
                    public void mouseClicked(MouseEvent e) {
                        System.out.println("cell button is clicked");
                    public void mouseEntered(MouseEvent e) {                   
                    public void mouseExited(MouseEvent e) {
                    public void mousePressed(MouseEvent e) {
                        System.out.println("cell button is pressed");                   
                    public void mouseReleased(MouseEvent e) {
                add(button, new GridBagConstraints(0, 0, 1, 1, 0, 100.0
                    ,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(10, 5, 10, 5), 20, 10));
        class MyColHeader extends JPanel {
            String text;
            JButton button;
            public MyColHeader(String text_) {
                text = text_;
                setLayout(new GridBagLayout());
                setBorder(BorderFactory.createEtchedBorder());
                button = new JButton(text);
                button.addMouseListener(new MouseListener() {
                    public void mouseClicked(MouseEvent e) {
                        System.out.println("column header button is clicked");
                    public void mouseEntered(MouseEvent e) {                   
                    public void mouseExited(MouseEvent e) {
                    public void mousePressed(MouseEvent e) {
                        System.out.println("column header button is pressed");                   
                    public void mouseReleased(MouseEvent e) {
                add(button, new GridBagConstraints(0, 0, 1, 1, 0, 0.0
                    ,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(3, 0, 3, 0), 5, 0));
        class MyColHeaderRenderer implements TableCellRenderer {
            public Component getTableCellRendererComponent(JTable table,
                Object value, boolean isSelected, boolean hasFocus, int row, int column) {
                return headers[column];
    }

    draw a shape and ensure it is selected
    in the Inspector > hyperlink > enable as hyperlink > click slide button and enter the slide number
    place the object in slide 2 and create a dissolve to automatically build on entry for the object

  • Making clickable buttons to open xml images in gallery

    I want to do an xml grid image gallery similar to http://www.republicofcode.com/tutorials/flash/as3gridgallery/
    However, instead of using thumbnails, I already have images on my site that I want to make clickable buttons to open the images in the gallery...how would I script these buttons?
    [link removed by moderator]

    If you want it to be similar to the one you link then use the images in the grid like in the one you linked.

  • On firefox clickable buttons are not clickable for me eg the + to open new tab ,the sign in . register and other applications buttons on this page wont click yet the 2 blue change buttons are fine

    Question
    on firefox clickable buttons are not clickable for me eg the + to open new tab ,the sign in . register and other applications buttons on this page wont click yet the 2 blue change buttons are fine

    Top of Firefox window non-responsive, toolbars non responsive -- also see [http://kb.mozillazine.org/Problematic_extensions Problematic extensions]
    *caused by Yahoo Toolbar -- https://support.mozilla.com/questions/890908
    *caused by Babylon Toolbar -- https://support.mozilla.com/questions/890670

  • Can I make a cell in iWork 08 a clickable button?

    Is there a way to assign a cell to be a clickable button which would take you to another spreadsheet?

    The response is a simple : no.
    Yvan KOENIG (from FRANCE dimanche 12 octobre 2008 12:00:55)

  • Clickable button in Keynote?

    Hi, I'm wondering if I can create a clickable button in keynote that would then trigger a build-in effect such as dissolve. Is such a thing possible?

    draw a shape and ensure it is selected
    in the Inspector > hyperlink > enable as hyperlink > click slide button and enter the slide number
    place the object in slide 2 and create a dissolve to automatically build on entry for the object

  • I noticed in Firefox that my cursor doesn't change to let me know a button is clickable (button mode) in flash sites, why?

    I noticed that in the new version of Firefox that my cursor doesn't indicate when a button, in flash, is clickable. In other browsers, the cursor changes from the standard arrow to the hand with a finger.

    I noticed that in the new version of Firefox that my cursor doesn't indicate when a button, in flash, is clickable. In other browsers, the cursor changes from the standard arrow to the hand with a finger.

  • Multiple Buttons in JTable Headers:  Listening for Mouse Clicks

    I am writing a table which has table headers that contain multiple buttons. For the header cells, I am using a custom cell renderer which extends JPanel. A JLabel and JButtons are added to the JPanel.
    Unfortunately, the buttons do not do anything. (Clicking in the area of a button doesn't appear to have any effect; the button doesn't appear to be pressed.)
    Looking through the archives, I read a suggestion that the way to solve this problem is to listen for mouse clicks on the table header and then determine whether the mouse clicks fall in the area of the button. However, I cannot seem to get coordinates for the button that match the coordinates I see for mouse clicks.
    The coordinates for mouse clicks seem to be relative to the top left corner of the table header (which would match the specification for mouse listeners). I haven't figured out how to get corresponding coordinates for the button. The coordinates returned by JButton.getBounds() seem to be relative to the top left corner of the panel. I hoped I could just add those to the coordinates for the panel to get coordinates relative to the table header, but JPanel.getBounds() gives me negative numbers for x and y (?!?). JPanel.getLocation() gives me the same negative numbers. When I tried JPanel.getLocationOnScreen(), I get an IllegalComponentStateException:
    Exception in thread "AWT-EventQueue-0" java.awt.IllegalComponentStateException: component must be showing on the screen to determine its location
    Can someone tell me how to get coordinates for the button on the JTableHeader? Or is there an easier way to do this (some way to make the buttons actually work so I can just use an ActionListener like I normally would)?
    Here is relevant code:
    public class MyTableHeaderRenderer extends JPanel implements TableCellRenderer {
    public MyTableHeaderRenderer() {
      setOpaque(true);
      // ... set colors...
      setBorder(UIManager.getBorder("TableHeader.cellBorder"));
      setLayout(new FlowLayout(FlowLayout.LEADING));
      setAlignmentY(Component.CENTER_ALIGNMENT);
    public Component getTableCellRendererComponent(JTable table,
                                                     Object value,
                                                     boolean isSelected,
                                                     boolean hasFocus,
                                                     int row,
                                                     int column){
      if (table != null){
        removeAll();
        String valueString = (value == null) ? "" : value.toString();
        add(new JLabel(valueString));
        Insets zeroInsets = new Insets(0, 0, 0, 0);
        final JButton sortAscendingButton = new JButton("1");
        sortAscendingButton.setMargin(zeroInsets);
        table.getTableHeader().addMouseListener(new MouseAdapter(){
          public void mouseClicked(MouseEvent e) {
            Rectangle buttonBounds = sortAscendingButton.getBounds();
            Rectangle panelBounds = MyTableHeaderRenderer.this.getBounds();
            System.out.println(Revising based on (" + panelBounds.x + ", "
                               + panelBounds.y + ")...");
            buttonBounds.translate(panelBounds.x, panelBounds.y);
            if (buttonBounds.contains(e.getX(), e.getY())){  // The click was on this button.
              System.out.println("Calling sortAscending...");
              ((MyTableModel) table.getModel()).sortAscending(column);
            else{
              System.out.println("(" + e.getX() + ", " + e.getY() + ") is not within "
                                 + sortAscendingButton.getBounds() + " [ revised to " + buttonBounds + "].");
        sortAscendingButton.setEnabled(true);
        add(sortAscendingButton);
        JButton button2 = new JButton("2");
        button2.setMargin(zeroInsets);
        add(button2);
        //etc
      return this;
    }

    I found a solution to this: It's the getHeaderRect method in class JTableHeader.
    table.getTableHeader().addMouseListener(new MouseAdapter(){
      public void mouseClicked(MouseEvent e) {
        Rectangle panelBounds = table.getTableHeader().getHeaderRect(column);
        Rectangle buttonBounds = sortAscendingButton.getBounds();
        buttonBounds.translate(panelBounds.x, panelBounds.y);
        if (buttonBounds.contains(e.getX(), e.getY()) && processedEvents.add(e)){  // The click was on this button.
          ((MyTableModel) table.getModel()).sortAscending(column);
    });

  • Clickable buttons within mc

    Hello,
    I'm working on a project and am having difficulty with a mc inside my swf. The movie clip is stopped at frame 1, showing only a small box with arrows in the top left corner of my swf. When MOUSE_OVER this box the mc plays and stops at frame(25). The mc (moveOut) has a motion tween where a larger box slides onto the stage, embedded are two buttons (toEur & toNeth) that will lead the user back to a different swf. The mc works fine, however if you mouse over either of the buttons it jitters or just goes to frame(1) and stops.
    I think what is happening is that mousing over the buttons is considered mousing out of the mc. How can I make it that the mc plays as normal no matter if mouse_over the butons or not, but still make the buttons clickable?  My code is below:
    import flash.events.MouseEvent; stop(); moveOut.addEventListener(MouseEvent.MOUSE_OVER, playmc); moveOut.addEventListener(MouseEvent.MOUSE_OUT, stopmc); function playmc(e:MouseEvent) :void {      play(); } function stopmc(e:MouseEvent) :void {      gotoAndStop(1); }
    I also tried to add this code, attempting to make the MOUSE_OVER on buttons just go to frame(25), better then the jitter thing.
    toEur.addEventListener(MouseEvent.MOUSE_OVER, endmc); toNeth.addEventListener(MouseEvent.MOUSE_OVER, endmc); function endmc(e:MouseEvent) :void {      gotoAndStop(25); }
    I have yet to make the CLICK code for each button, this will involve unloading children swf's, which is another story!!!
    Thanks in advance if anyone can help me!

    I'm completely new to all of this, I didn't quite understand all of the commentJust keep on going, reflect on your experience and experiment again!
    When I just started, I was unable to read AS3 Developers' Guide without howling at the Moon...
    A couple of words in addition to what Ned said.
    So as to set up a communication between your parent object and a child, which is loaded SWF, you DON'T need to use anything like LocalConnection.
    You need to just load your external SWF and then 'talk' to Objects in it through referencing via dot (look at my sample code above (or below - it's closer...)).
    I encountered some issues with using Loader class, but replacement with ProLoader fixed all.
    import flash.events.MouseEvent;
    import flash.display.MovieClip;
    import fl.display.ProLoader;
    var CPLoader:ProLoader = new ProLoader();
    var ControlPanel:MovieClip;
    addChild(CPLoader);
    CPLoader.load(new URLRequest("ControlPanel.1920.swf"));
    Loader.contentLoaderInfo.addEventListener(Event.COMPLETE, controlPanel_initialiser);
    function controlPanel_initialiser(event:Event):void
    ControlPanel = event.target.content;
    ControlPanel.Power_BTN.ButtonArea.buttonMode = true;
    ControlPanel.Play_BTN.ButtonArea.buttonMode = true;
    ControlPanel.Pause_BTN.ButtonArea.buttonMode = true;
    ControlPanel.Power_BTN.ButtonArea.addEventListener(MouseEvent.CLICK, currentSwitchClick_handler);
    ControlPanel.Play_BTN.ButtonArea.addEventListener(MouseEvent.CLICK, currentSwitchClick_handler);
    ControlPanel.Pause_BTN.ButtonArea.addEventListener(MouseEvent.CLICK, currentSwitchClick_handler);

  • Buttons in JTables?

    Hello,
    Does anybody know how to put buttons and progress bars in JTable cells? is it possible? is there another component which I should use?
    Many thanks
    Alex

    try to look on this
    http://java.sun.com/docs/books/tutorial/uiswing/components/table.html#editrender

  • Using the right mouse button with JTables

    Hi Folks,
    Can someone describe how to allow the right mouse button to select rows in a JTable? Or can someone tell me how to access the code which is activated when the RMB is clicked on a JTable so I can copy it.
    Thanks.

    Can you attach a screenshot?
    *http://en.wikipedia.org/wiki/Screenshot
    Use a compressed image type like PNG or JPG to save the screenshot and make sure that you do not exceed the maximum file size (1 MB).
    Make sure that you do not run Firefox in full screen mode (press F11 or Fn + F11 to toggle; Mac: command+Shift+F).<br />
    If you are in full screen mode then hover the mouse to the top to make the Navigation Toolbar and Tab bar appear.<br />
    You can click the Maximize button at the top right to leave full screen mode or right click empty space on a toolbar and use "Exit Full Screen Mode" or press F11.<br />

  • Buttons to JTable header...

    I'm trying set the column headers in a JTable to be JButtons that can be selected to sort items in the table by that column. I've got the buttons in place with a call to setHeaderRenderer(TableCellRenderer) and calling my own renderer. But the button does nothing (i.e. won't even "click").
    Ideas?

    Check this link
    http://www2.gol.com/users/tame/swing/examples/JTableExamples1.html

  • Buttons in JTable?

    Hi,
    I've got a problem with using Buttons in a JTable. Everything works well except that if I select the tablefield with the Button the button is displayed pressed though the listener doesn't receive any click.
    How can I change this?
    Thanks,
    Mior

    You should write your own class extends JButton implements TableCellEditor or you can add MouseListener to JTable and
    get cell clicked by JTable.getComponentAt(Point) or
    JTable.getComponentAt(int x, int y)

  • How do i make a clickable "button" that works in the PDF?

    I have a newsletter and want to make a button on a page that when clicked, will open the browser and go to that site. I see how to convert objects to a button and enter the URL in its properties but when I convert to a PDF, the entire object is missing. When converting to PDF, I also make sure that hyperlinks and interactive elements are both checked in the General section. I dont know what else I need to do!

    It is a colored square that was also grouped with a text box. I tried also ungrouping it and just putting the button effect on one or the other and had the same problem. When i look at the PDF, it has nothing there, whatever object i have the button effect applied to is totally deleted. I even tried clicking on the area where it was thinking maybe something was invisible and that didnt do anything either. There are no left over or hidden boxes there that could be covering things either. Im good about working cleanly and organized.
    Ill try doing this with a new document tomorrow and see what happens

  • I need help creating a list of vertical clickable buttons in an aside

    OK so here is the setup for this site I am working on. http://www.bestmarketingnames.com/default2.php I need to change that list on the left side into real buttons with destination when you click them. Here is a link that i have been tinkering with. http://www.bestmarketingnames.com/default.php I need them to fit in the left aside and vertical. I can't make them vertical. I'm sure it's a fairly simple thing but I don't know how to do it.
    Thanks

    Try this:
    <!doctype html>
    <html>
    <head>
    <meta charset="utf-8">
    <title>HTML5, Vertical Menu</title>
    <!--[if lt IE 9]>
    <script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
    <![endif]-->
    <style>
        /***add these to your CSS Reset***/
        margin: 0;
        padding: 0;
        -moz-box-sizing: border-box;
        -webkit-box-sizing: border-box;
        box-sizing: border-box;
    nav {
        width: 250px;
        background: #555;
        color: #FFF;
        font-family: Segoe, "Segoe UI", "DejaVu Sans", "Trebuchet MS", Verdana, sans-serif;
    nav li {
        list-style: none;
        width: 248px;
        line-height: 2.5em;
        border: 2px solid #CCC;
        text-align: center;
        font-weight: bold;
        font-size: 16px;
        cursor: pointer;
    nav li:hover {
        background-color: #FFF;
        color: #000
    </style>
    </head>
    <body>
    <aside>
    <nav>
    <ul>
    <li>Menu 1</li>
    <li>Menu 2</li>
    <li>Menu 3</li>
    <li>Menu 4</li>
    </ul>
    </nav>
    </aside>
    </body>
    </html>
    ❄  ☃  ❄Nancy O.

Maybe you are looking for