在JTable的最后一行之后添加JButton

时间:2014-09-16 09:30:55

标签: java swing layout-manager

我搜索了很长一段时间的答案,但大多数情况与我的不同。

我想在表的最后一行之后直接添加一个JButton,该表是动态的,位于scrollPane中。

几乎所有其他问题都在BorderLayout.South中的scrollPane之后添加了一个按钮,这不是我想要的。

感谢

enter image description here

更新图片以更好地表示它,按钮应该出现在最后一行之后的scrollPane内。

1 个答案:

答案 0 :(得分:2)

详细了解不同的LayoutManager's。使用适当的经理或他们的组合。

这是一个简单的例子:

import java.awt.BorderLayout;
import java.awt.FlowLayout;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;


public class TestFrame extends JFrame {

    public TestFrame() {
        init();
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        pack();
        setLocationRelativeTo(null);
        setVisible(true);
    }

    private void init() {
        JTable t = new JTable(3,3);
        add(new JScrollPane(t));
        JPanel btns = new JPanel(new FlowLayout(FlowLayout.LEFT,0,0));
        JButton jButton = new JButton("here");
        btns.add(jButton);
        add(btns,BorderLayout.SOUTH);
    }


    public static void main(String args[]) {
        new TestFrame();
    }

}

enter image description here

编辑:

尝试使用下一个示例:

import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.DefaultCellEditor;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellEditor;
import javax.swing.table.TableCellRenderer;


public class TestFrame extends JFrame {

    public TestFrame() {
        init();
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        pack();
        setLocationRelativeTo(null);
        setVisible(true);
    }

    private void init() {
        JTable t = new JTable(new DefaultTableModel(3,3){
            @Override
            public boolean isCellEditable(int row, int column) {
                if(row == getRowCount()-1 && column != 0){
                    return false;
                }
                return super.isCellEditable(row, column);
            }
        });
        t.setGridColor(t.getTableHeader().getBackground());
        add(new JScrollPane(t));
        for(int i=0;i<3;i++)
            t.getColumnModel().getColumn(i).setCellRenderer(getDummyRenderer());
        t.getColumnModel().getColumn(0).setCellEditor(getDummyEditor());
    }


    private TableCellEditor getDummyEditor() {
        return new DummyEditor();
    }

    private TableCellRenderer getDummyRenderer() {
        return new DefaultTableCellRenderer() {

            @Override
            public Component getTableCellRendererComponent(JTable table, Object value,
                    boolean isSelected, boolean hasFocus, int row, int column) {
                if(row == table.getRowCount()-1){
                    if(column == 0){
                        return new JButton("dummy");
                    } else {
                        JLabel jLabel = new JLabel("");
                        jLabel.setOpaque(true);
                        jLabel.setBorder(null);
                        return jLabel;
                    }
                } else {
                    Component tableCellRendererComponent = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
                    return tableCellRendererComponent;
                }
            }
        };
    }

    public static void main(String args[]) {
        new TestFrame();
    }

    private class DummyEditor extends DefaultCellEditor implements TableCellEditor{

        private JButton btn = new JButton("dummy");

        public DummyEditor(){
            super(new JTextField());
            btn.addActionListener(new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent e) {
                    System.out.println("clicked");
                }
            });
        }

        @Override
        public Component getTableCellEditorComponent(JTable table,
                Object value, boolean isSelected, int row, int column) {
            return row == table.getRowCount()-1 ? btn : super.getTableCellEditorComponent(table, value, isSelected, row, column);
        }

    }

}

enter image description here