Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: I can not redeem the action of a button within a table.

  1. #1
    Junior Member
    Join Date
    Dec 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy I can not redeem the action of a button within a table.

    Good morning,

    Yesterday I was searching the internet a code in which I could place a button inside a table. I found the code below, I am usually able to put the button and made some adjustments to my system, but I'm not able to recover the action of clicking it, can someone help me?




    class BotaoVisualizarTabelaProgramadas extends AbstractCellEditor
            implements TableCellRenderer, TableCellEditor, ActionListener {
     
        JTable table;
        JButton renderButton;
        JButton editButton;
        String text;
     
     
    public BotaoVisualizarTabelaProgramadas(JTable table, int column) {
            super();
            this.table = table;
     
            renderButton = new JButton();
     
            editButton = new JButton();
            editButton.setFocusPainted(false);
            editButton.addActionListener(this);
     
            TableColumnModel columnModel = table.getColumnModel();
            columnModel.getColumn(column).setCellRenderer(this);
            columnModel.getColumn(column).setCellEditor(this);
        }
     
        @Override
        public Component getTableCellRendererComponent(
                JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
            if (hasFocus) {
                renderButton.setForeground(table.getForeground());
                renderButton.setBackground(UIManager.getColor("Button.background"));
            } else if (isSelected) {
                renderButton.setForeground(table.getSelectionForeground());
                renderButton.setBackground(table.getSelectionBackground());
            } else {
                renderButton.setForeground(table.getForeground());
                renderButton.setBackground(UIManager.getColor("Button.background"));
            }
     
           renderButton.setText((value == null) ? "" : value.toString());
            return renderButton;
        }
     
        @Override
        public Component getTableCellEditorComponent(
                JTable table, Object value, boolean isSelected, int row, int column) {
            text = (value == null) ? "" : value.toString();
            return editButton;
        }
     
        @Override
        public Object getCellEditorValue() {
            return text;
        }
     
        @Override
        public void actionPerformed(ActionEvent e) {
            fireEditingStopped();
            System.out.println(e.getActionCommand() + " : " + table.getSelectedRow());
        }
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: I can not redeem the action of a button within a table.

    The posted code needs import statements to be able to compile and test it.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Problem with Animation and Button Action
    By ygeva in forum AWT / Java Swing
    Replies: 8
    Last Post: December 3rd, 2011, 01:57 PM
  2. button action problem
    By macko in forum What's Wrong With My Code?
    Replies: 4
    Last Post: March 29th, 2011, 10:51 AM
  3. Action button HELP?
    By utoptas in forum Java Theory & Questions
    Replies: 8
    Last Post: August 27th, 2010, 03:32 PM
  4. Action Listener
    By Suzanne in forum What's Wrong With My Code?
    Replies: 7
    Last Post: May 29th, 2010, 10:50 AM
  5. Action from Radio Button
    By halfwaygone in forum What's Wrong With My Code?
    Replies: 0
    Last Post: April 25th, 2010, 10:52 AM