Java Swing - 带有Jbuttons的Jpanel嵌入在JTable中

时间:2012-03-16 12:42:00

标签: java swing user-interface jtable jpanel

我在Swing中构建一个Ui,其中我的要求是在JTable中使用JPanes。这些JPanes将包含JButton。
我的用例如下 -
我正在编写一个MethodEditor,其中我提供了一个UI来存储所提供文件中的方法。 UI还允许在单击按钮时编辑传递给方法的参数 每个方法都有一个UI表示如下 - enter image description here

我对Method类的基本表示如下 -

public Class Method {

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

现在我有一个Method个对象的列表,List<Method> methodList我希望以我的JTable为基础。 此List包含在MethodModel类中,如下所示 -

public class MethodModel {
   List<Method> methodModel;
}

我问了一个问题earlier并根据那里提供的答案编写了我的代码。
但我的代码似乎没有起作用。我的代码如下 -

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)) {


            }
        }

    }

}

代码编译但JTable没有出现。 关于我可能做错的任何指示都会有很大的帮助。

1 个答案:

答案 0 :(得分:1)

不要在JTable中包含其他组件。更不用说具有多个其他组件的组件。原因是JTable 不会将鼠标事件传递给其单元格。因此,即使你在JTable中有按钮,你也必须自己注意按下它们:

  • 获取单元格
  • 获取确切的坐标
  • 将这些坐标外推到内部组件
  • 手动调用点击相应的按钮。

即便如此,你也不会得到按钮动画和东西。

如果您需要将组件排列到表中,请将JPanel与GridLayout或GridBagLayout一起使用。