ping
ping

Reputation: 1239

Java Swing -- Jpanel with Jbuttons embedded within a JTable

Im building a Ui in Swing wherein my requirement is to have JPanes within a JTable. These JPanes will have JButtons within them.
My use case is as follows --
Im writing a MethodEditor wherein im providing a UI to store the methods within a supplied file. The UI would also allow editing the parameters being passed to the method on the click of a button.
Every single method would have a UI representation as follows -- enter image description here

My basic representation of the Method class is as follows --

public Class Method {

String methodName;
List<String> InputVariableNames;
String OutputVariableName;
}

Now i have a list of Method objects, List<Method> methodList on which i want to base my JTable. This List is contained in a MethodModel class as follows --

public class MethodModel {
   List<Method> methodModel;
}

I had asked a question earlier and have based my code on the answer provided there.
My code however does not seem to be working. My code is as follows --

public class MethodEditor extends JTable {


    private static final long serialVersionUID = 1L;

    private MethodEditorModel model ;
    private MethodCellRenderer cellRenderer;

    public MethodEditor(MethodModel bean) {
        setRowHeight(25);
        this.setPreferredSize(new Dimension(500, 500));
        model = new MethodEditorModel(bean);
        this.setModel(model);
        setupComponent();
    }

    private void setupComponent() {
        cellRenderer = new MethodCellRenderer();
        this.setDefaultRenderer(Object.class,cellRenderer);
        this.setBorder(BorderFactory.createLineBorder(Color.GRAY));
    }


    private static class MethodEditorModel extends DefaultTableModel implements PropertyChangeListener {

        /**
         * 
         */
        private static final long serialVersionUID = 1L;    
        private MethodModel bean;

        public MethodEditorModel(MethodModel bean) {
            this.bean = bean;
            bean.addPropertyChangeListener(this);
        }


        @Override
        public void propertyChange(PropertyChangeEvent evt) {
            fireTableDataChanged();

        }

    }

    private static class MethodCellRenderer implements TableCellRenderer {

        /**
         * 
         */
        private static final long serialVersionUID = 1L;
        private MethodEditorCellPanel renderer = new MethodEditorCellPanel();


        @Override
        public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
            MethodModel methodModel = (MethodModel)value;
            for(Method method : methodModel.getMethodList()) {
                renderer.setComponents((Method) method);
            }
            return renderer;
        }

    }

    private static class MethodEditorCellPanel extends JPanel implements ActionListener {

        private static final long serialVersionUID = 1L;
        private JButton upButton;
        private JButton downButton;
        private JButton methodDetailsButton;
        private Method method;

        public MethodEditorCellPanel() {
            upButton = new JButton("Up");
            downButton = new JButton("Down");
        }

        public void setComponents(Method method)
        {
            this.method = method;
            methodDetailsButton = new JButton(method.getMethodName());

            upButton.addActionListener(this);
            downButton.addActionListener(this);
            methodDetailsButton.addActionListener(this);

            Box verticalBar =  Box.createHorizontalBox();
            verticalBar.add(upButton);
            verticalBar.add(Box.createHorizontalStrut(15));
            verticalBar.add(methodDetailsButton);
            verticalBar.add(Box.createHorizontalStrut(15));
            verticalBar.add(downButton);
            add(verticalBar);
        }

        @Override
        public void actionPerformed(ActionEvent evt) {
            if(evt.getSource().equals(downButton)) {

            }

            if(evt.getSource().equals(upButton)) {

            }

            if(evt.getSource().equals(methodDetailsButton)) {


            }
        }

    }

}

The code compiles but the JTable does not show up. Any pointers on what i may be doing wrong would be of great help.

Upvotes: 0

Views: 1072

Answers (1)

Jakub Zaverka
Jakub Zaverka

Reputation: 8874

Don't include another components to JTable. Let alone components with multiple other components. The reason is that JTable won't pass mouse events to its cells. So even when you have buttons inside JTable, then you would have to take care about pressing them by yourself, by:

  • get cell it was clicked to
  • get the exact coordinates
  • extrapolate these coordinates to the inner component
  • manually call click on the corresponding button.

And even then you won't get button animation and stuff.

If you need to arrange components into a table, use JPanel with GridLayout or GridBagLayout.

Upvotes: 1

Related Questions