Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/396.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何调整带有JTable的JScrollPane的大小?_Java_Swing_Jtable_Awt_Jscrollpane - Fatal编程技术网

Java 如何调整带有JTable的JScrollPane的大小?

Java 如何调整带有JTable的JScrollPane的大小?,java,swing,jtable,awt,jscrollpane,Java,Swing,Jtable,Awt,Jscrollpane,我创建了一个JTable,其中包含一些很好的函数。我的问题是,当我调整它所在帧的大小时,它不会调整大小。有人能帮我调整代码的大小吗?我尝试了一些我找到的帖子的想法,但对我没有任何帮助。因此,我尝试将JPanel添加到另一个上,并提供一些布局想法。我还尝试使用ComponentListeners和resizeaction设置大小 代码: public类TableyTable扩展了JPanel{ /** * */ 私有静态最终长serialVersionUID=1L; 公共JTableHeader

我创建了一个JTable,其中包含一些很好的函数。我的问题是,当我调整它所在帧的大小时,它不会调整大小。有人能帮我调整代码的大小吗?我尝试了一些我找到的帖子的想法,但对我没有任何帮助。因此,我尝试将JPanel添加到另一个上,并提供一些布局想法。我还尝试使用ComponentListeners和resizeaction设置大小

代码:

public类TableyTable扩展了JPanel{
/**
* 
*/
私有静态最终长serialVersionUID=1L;
公共JTableHeader;
公共JTable表;
公共JScrollPane滚动窗格;
公共jpopmpmenu重命名pup;
公共JTextField文本;
公共表格栏;
私有布尔HeaderEdit=false;
私有布尔tableEditable=false;
公共静态最终维度SCREEN_SIZE=Toolkit.getDefaultToolkit().getScreenSize();
public static final int MIN_ROW_HEIGHT=(int)SCREEN_SIZE.getHeight()/36;
公共静态final int MIN_ROW_WIDTH=(int)SCREEN_SIZE.getWidth()/108;
公共表表(int行,int列){
init(行、列);
}
公共void init(int行,int列){
表=新的JTable(行、列){
/**
* 
*/
私有静态最终长serialVersionUID=1L;
@凌驾
公共布尔值可编辑(int行,int列){
if(tableEditable)返回true;
返回false;
}
};
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
表.changeSelection(0,0,false,false);
header=table.getTableHeader();
header.addMouseListener(新的MouseAdapter(){
@凌驾
公共void mouseClicked(MouseEvent事件){
if(event.getClickCount()==2&&headerEditable){
editColumnAt(event.getPoint());
}
}
});
text=新的JTextField();
text.setboorder(空);
text.addActionListener(新ActionListener(){
已执行的公共无效操作(操作事件e){
如果(HeaderEdit)重命名Column();
}
});
table.setRowSelectionAllowed(false);
table.setCellSelectionEnabled(真);
renamePopup=新的JPopupMenu();
重命名pup.setBorder(新的MatteBorder(0,1,1,1,颜色为深灰色));
重命名pup.add(文本);
scrollPane=新的JScrollPane(表);
setRowHeaderView(buildRowHeader(表));
表.设置行高(最小行高);
TableColumnModel cm=table.getColumnModel();
对于(int i=0;ipublic class TableyTable extends JPanel {
    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    public JTableHeader header;
    public JTable table;
    public JScrollPane scrollPane;
    public JPopupMenu renamePopup;
    public JTextField text;
    public TableColumn column;
    private boolean headerEditable = false;
    private boolean tableEditable = false;

    public static final Dimension SCREEN_SIZE = Toolkit.getDefaultToolkit().getScreenSize();
    public static final int MIN_ROW_HEIGHT = (int)SCREEN_SIZE.getHeight()/36;
    public static final int MIN_ROW_WIDTH = (int)SCREEN_SIZE.getWidth()/108;


    public TableyTable(int row, int column) {
        init(row, column);
    }

    public void init(int row, int column) {
        table = new JTable(row, column) {
            /**
             * 
             */
            private static final long serialVersionUID = 1L;


            @Override
            public boolean isCellEditable(int row, int column) {
                if (tableEditable) return true;
                return false;
            }
        };
        table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
        table.changeSelection(0, 0, false, false);

        header = table.getTableHeader();
        header.addMouseListener(new MouseAdapter(){
            @Override
            public void mouseClicked(MouseEvent event) {
                if (event.getClickCount() == 2 && headerEditable) {
                    editColumnAt(event.getPoint());
                }
            }
        });

        text = new JTextField();
        text.setBorder(null);
        text.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if (headerEditable) renameColumn();
            }
        });

        table.setRowSelectionAllowed(false);
        table.setCellSelectionEnabled(true);


        renamePopup = new JPopupMenu();
        renamePopup.setBorder(new MatteBorder(0, 1, 1, 1, Color.DARK_GRAY));
        renamePopup.add(text);

        scrollPane = new JScrollPane( table );
        scrollPane.setRowHeaderView(buildRowHeader(table));

        table.setRowHeight(MIN_ROW_HEIGHT);
        TableColumnModel cm = table.getColumnModel();
        for(int i = 0; i < table.getColumnModel().getColumnCount(); i++)
            cm.getColumn(i).setWidth(200);
        table.setColumnModel(cm);

        add(scrollPane);
    }

    public void setHeaderEditable(boolean b) {
        headerEditable = b;
    }

    public boolean isHeaderEditable() {
        return headerEditable;
    }

    public void setTableEditable(boolean b) {
        tableEditable = b;
    }

    public boolean isTableEditable() {
        return tableEditable;
    }

    private void editColumnAt(Point p) {
        int columnIndex = header.columnAtPoint(p);

        if (columnIndex != -1) { 
            column = header.getColumnModel().getColumn(columnIndex);
            Rectangle columnRectangle = header.getHeaderRect(columnIndex);

            text.setText(column.getHeaderValue().toString());
            renamePopup.setPreferredSize(new Dimension(columnRectangle.width, columnRectangle.height - 1));
            renamePopup.show(header, columnRectangle.x, 0);

            text.requestFocusInWindow();
            text.selectAll();
        }
    }

    private void renameColumn() {
        column.setHeaderValue(text.getText());
        renamePopup.setVisible(false);
        header.repaint();
    }

    private static JList<Object> buildRowHeader(JTable table) {
        final Vector<String> headers = new Vector<String>();
        for (int i = 0; i < table.getRowCount(); i++) {
            String name = "";
            if (i < 10) {
                name += "0";
            }
            if (i < 100) {
                name += "0";
            }
            name += i;
            headers.add(name);
        }
        ListModel<Object> lm = new AbstractListModel<Object>() {

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

            public int getSize() {
                return headers.size();
            }

            public Object getElementAt(int index) {
                return headers.get(index);
            }
        };

        final JList<Object> rowHeader = new JList<>(lm);
        rowHeader.setOpaque(false);
        rowHeader.setFixedCellWidth(TableyTable.MIN_ROW_HEIGHT);


        MouseInputAdapter mouseAdapter = new MouseInputAdapter() {
            Cursor oldCursor;
            Cursor RESIZE_CURSOR = Cursor.getPredefinedCursor(Cursor.S_RESIZE_CURSOR);
            int index = -1;
            int oldY = -1;

            @Override
            public void mouseMoved(MouseEvent e) {
                super.mouseMoved(e);
                int previ = getLocationToIndex(new Point(e.getX(), e.getY() - 3));
                int nexti = getLocationToIndex(new Point(e.getX(), e.getY() + 3));
                if (previ != -1 && previ != nexti) {
                    if (!isResizeCursor()) {
                        oldCursor = rowHeader.getCursor();
                        rowHeader.setCursor(RESIZE_CURSOR);
                        index = previ;
                    }
                } else if (isResizeCursor()) {
                    rowHeader.setCursor(oldCursor);
                }
            }

            private int getLocationToIndex(Point point) {
                int i = rowHeader.locationToIndex(point);
                if (!rowHeader.getCellBounds(i, i).contains(point)) {
                    i = -1;
                }
                return i;
            }

            @Override
            public void mouseReleased(MouseEvent e) {
                super.mouseReleased(e);
                if (isResizeCursor()) {
                    rowHeader.setCursor(oldCursor);
                    index = -1;
                    oldY = -1;
                }
            }

            @Override
            public void mouseDragged(MouseEvent e) {
                super.mouseDragged(e);
                if (isResizeCursor() && index != -1) {
                    int y = e.getY();
                    if (oldY != -1) {
                        int inc = y - oldY;
                        int oldRowHeight = table.getRowHeight(index);
                        int oldNextRowHeight = table.getRowHeight(index+1);
                        if (oldRowHeight > MIN_ROW_HEIGHT || inc > 0) {
                            int rowHeight = Math.max(MIN_ROW_HEIGHT, oldRowHeight + inc);
                            table.setRowHeight(index, rowHeight);
                            if (rowHeader.getModel().getSize() > index + 1) {
                                int rowHeight1 = table.getRowHeight(index + 1) - inc;
                                rowHeight1 = Math.max(MIN_ROW_HEIGHT, rowHeight1);
                                table.setRowHeight(index + 1, rowHeight1);
                            }
                        }
                        if (table.getRowCount()>index+1)
                            table.setRowHeight(1+index, oldNextRowHeight);
                        else System.out.println("HI");
                    }
                    oldY = y;
                }
            }

            private boolean isResizeCursor() {
                return rowHeader.getCursor() == RESIZE_CURSOR;
            }
        };
        rowHeader.addMouseListener(mouseAdapter);
        rowHeader.addMouseMotionListener(mouseAdapter);
        rowHeader.addMouseWheelListener(mouseAdapter);

        rowHeader.setCellRenderer(new RowHeaderRenderer(table));
        rowHeader.setBackground(table.getBackground());
        rowHeader.setForeground(table.getForeground());
        return rowHeader;
    }

    static class RowHeaderRenderer extends JLabel implements ListCellRenderer<Object> {
        /**
         * 
         */
        private static final long serialVersionUID = 1L;
        private JTable table;

        RowHeaderRenderer(JTable table) {
            this.table = table;
            JTableHeader header = this.table.getTableHeader();
            setOpaque(true);
            setBorder(UIManager.getBorder("TableHeader.cellBorder"));
            setHorizontalAlignment(CENTER);
            setForeground(header.getForeground());
            setBackground(header.getBackground());
            setFont(header.getFont());
            setDoubleBuffered(true);
        }

        public Component getListCellRendererComponent(JList<?> list, Object value,
                                                      int index, boolean isSelected, boolean cellHasFocus) {
            setText((value == null) ? "" : value.toString());
            setPreferredSize(null);
            setPreferredSize(new Dimension((int) getPreferredSize().getWidth(), table.getRowHeight(index)));
            list.firePropertyChange("cellRenderer", 0, 1);
            return this;
        }
    }

    public static void main(String[] args) {
        JFrame frame = new JFrame("Tabley");

        TableyTable table = new TableyTable(1000, 18278);
        table.setHeaderEditable(true);
        table.setTableEditable(true);

        frame.add(table);
        frame.setSize(new Dimension(700, 500));
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }



}