当我使用firePropertyChange更新我的JList时,我的jtabbedpane不再更新

时间:2019-06-04 08:31:59

标签: java swing awt jtabbedpane propertychanged

我创建了一个带有行标题(左侧的标题)的JTable。它也可以调整大小。为了更新行标题的大小,我使用了list.firePropertyChange("cellRenderer", 0, 1);。我的问题是,当我使用此代码时,我的JTabbedPane仅在调整JFrame的大小时才会更新,但是如果我不使用此代码,因此JTabbedPane会自动更新,则行头将不再更新。我该如何解决?

代码示例:

public class Frame extends JFrame {
    public static final String DIR = System.getProperty("user.dir");
    private JTabbedPane menu;
    private Panel menuSide0, menuSide1, menuSide2, menuSide3; //menu panels
    private JPanel mainPanel; //main Panel where content is on
    private JPanel menuPanel; //menu Panel where the Menu and selection is on
    private JPanel selectionPanel; //The Panel where you will get the information about which parts u have selected
    private JPanel optionPanel; //The Panel where you scroll and you can change between diffrent files
    private ImageButton saveB, save_asB, openB;

    public Frame() {
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (Exception e) {
            e.printStackTrace();
        }

        /*Frame Options*/
        this.setExtendedState(JFrame.MAXIMIZED_BOTH); 
        this.setMinimumSize(new Dimension(500, 400));
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setLocationRelativeTo(null);
        this.setBackground(Color.WHITE);

        /*Menu*/
        menuPanel = new JPanel();
        this.add(menuPanel, BorderLayout.NORTH);
        menuPanel.setLayout(new BorderLayout());
        menuPanel.setPreferredSize(new Dimension(this.getWidth(), 200));

        UIManager.put("TabbedPane.selected", Color.WHITE);
        UIManager.put("TabbedPane.focus", Color.WHITE);
        menu = new JTabbedPane(JTabbedPane.TOP);
        menu.setSize(this.getWidth(), 100);
        menu.setPreferredSize(new Dimension(this.getWidth(), 130));
        menu.setBackground(Color.white);
        menuPanel.add(menu, BorderLayout.NORTH);

        /*Menusides*/
        JPanel spaceTop = new JPanel();
        spaceTop.setSize(Toolkit.getDefaultToolkit().getScreenSize().width, 20);
        spaceTop.setBackground(new Color(255, 255, 255));
        menuSide0 = new Panel(new BorderLayout());
        menuSide0.add(spaceTop, BorderLayout.NORTH);
        menuSide1 = new Panel();
        menuSide2 = new Panel();

        menu.add("menuside0", menuSide0);
        menu.add("menuside1", menuSide1);
        menu.add("menuside2", menuSide2);

        //menuside0
        JPanel panel0 = new JPanel(new FlowLayout(FlowLayout.LEFT));
        panel0.setBackground(Color.WHITE);
        panel0.add(new JButton("ARGH"));
        panel0.add(new JButton("ARGH"));
        panel0.add(new JButton("ARGH"));
        menuSide0.add(panel0);

        JTable table = new JTable(10, 10);
        RowHeaderRenderer rhr = new RowHeaderRenderer(table);


        JScrollPane scroll = new JScrollPane(table);
        scroll.setRowHeaderView(buildRowHeader(table));
        this.add(scroll);
        this.add(menu, BorderLayout.NORTH);
        this.setVisible(true);
    }

    private static final long serialVersionUID = 1L;
    private JTable table;
    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;

    static class RowHeaderRenderer extends JLabel implements ListCellRenderer<Object> {
        /**
         * 
         */

        private static final long serialVersionUID = 1L;
        private JTable table;

        RowHeaderRenderer(JTable table) {
            try {
                UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
            } catch (Exception e) {}
            this.table = table;
            JTableHeader header = this.table.getTableHeader();
            setOpaque(true);
            setBorder(new MatteBorder(1, 1, 0, 1, Color.LIGHT_GRAY));
            setHorizontalAlignment(CENTER);
            setForeground(null);
            setBackground(Color.WHITE);
            setFont(header.getFont());
            setDoubleBuffered(true);
        }

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

    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(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);
                    }
                    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;
    }

    public static void main(String[] args) {
        Frame frame = new Frame();
    }

}

编辑: 正如我发现的那样,当我直接将panel0添加到JTabbedPane而不是将其添加到menuSide0时,该Bug不会出现,但是我格式化得很好。我应该只使用其他布局还是以其他方式修复它?

0 个答案:

没有答案