recursive calls to AdjustmentListener - StackOverFlow Error Swing Java

前端 未结 1 1836
清酒与你
清酒与你 2020-12-22 01:31

I have to synchronize the horizontal scrolling of tables. The columns would be the same only property that can be changed for the columns is their size. Now the following co

相关标签:
1条回答
  • 2020-12-22 02:16

    there are important issues as AdjustmentListener

    hardcoded for last JScrollBar only (cant comment something works, better coud be know the value for all JScrollBars and recalculate the concrete value for another JScrollBars)

    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.event.AdjustmentEvent;
    import java.awt.event.AdjustmentListener;
    
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JScrollBar;
    import javax.swing.JScrollPane;
    import javax.swing.JTable;
    import javax.swing.table.JTableHeader;
    
    public class ScrollableJTable {
    
        private JFrame frame = null;
        private JPanel panel = null;
        private JTable table = null;
        private JTableHeader header = null;
        private JScrollPane pane = null;
        private JTable table1 = null;
        private JTableHeader header1 = null;
        private JScrollPane pane1 = null;
        private JTable table2 = null;
        private JTableHeader header2 = null;
        private JScrollPane pane2 = null;
    
        public static void main(String[] args) {
            new ScrollableJTable();
        }
    
        public ScrollableJTable() {
            frame = new JFrame("Creating a Scrollable JTable!");
            panel = new JPanel();
            String data[][] = {
                {"001", "vinod", "Bihar", "India", "Biology", "65", "First", "001", "vinod", "Bihar", "India", "Biology", "65", "First"},
                {"002", "Raju", "ABC", "Kanada", "Geography", "58", "second", "001", "vinod", "Bihar", "India", "Biology", "65", "First"},
                {"003", "Aman", "Delhi", "India", "computer", "98", "Dictontion", "001", "vinod", "Bihar", "India", "Biology", "65", "First"},
                {"004", "Ranjan", "Bangloor", "India", "chemestry", "90", "Dictontion", "001", "vinod", "Bihar", "India", "Biology", "65", "First"}
            };
            String col[] = {"Roll", "Name", "State", "country", "Math", "Marks", "Grade", "Roll", "Name", "State", "country", "Math", "Marks", "Grade"};
            table = new JTable(data, col);
            table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
            header = table.getTableHeader();
            header.setBackground(Color.yellow);
            pane = new JScrollPane(table);
            pane.setPreferredSize(new Dimension(400, 200));
            table1 = new JTable(data, col);
            table1.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
            header1 = table1.getTableHeader();
            header1.setBackground(Color.yellow);
            pane1 = new JScrollPane(table1);
            pane1.setPreferredSize(new Dimension(400, 200));
            table2 = new JTable(data, col);
            table2.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
            header2 = table2.getTableHeader();
            header2.setBackground(Color.yellow);
            pane2 = new JScrollPane(table2);
            pane2.setPreferredSize(new Dimension(200, 200));
            panel.setSize(500, 500);
            panel.add(pane);
            panel.add(pane1);
            panel.add(pane2);
            frame.add(panel);
            frame.pack();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setVisible(true);
            final JScrollBar bar = pane.getHorizontalScrollBar();
            final JScrollBar bar1 = pane1.getHorizontalScrollBar();
            final JScrollBar bar2 = pane2.getHorizontalScrollBar();
            bar2.addAdjustmentListener(new AdjustmentListener() {
    
                @Override
                public void adjustmentValueChanged(AdjustmentEvent e) {
                    bar.setValue(e.getValue());
                    bar1.setValue(e.getValue());
                }
            });
        }
    }
    
    1. don't use setSize, most of LayoutManagers accepting PreferredSize (hardcoded in my code example)

    2. FlowLayout (built_in LayoutManager for JPanel) accepting only PreferredSize

    3. use JFrame#pack() rether than JFrame#setSize(int, int)

    4. have look at InitialThread

    5. (event everybody can tell you that) output from AdjustmentListener is done on EDT, wrap output to the invokeLater() in the case that you hare to calculate value for different JScrollBars with different size on the screen, output should be delayed and then nicer, without jumping of JScrollPanes contens on the screen

    EDIT

    whatever, anything I tried no exception, only in the case that ColumnModel doesn't equals RowModel, rest is only to calculate the ratio for every ScrollBars Models

    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.EventQueue;
    import java.awt.event.AdjustmentEvent;
    import java.awt.event.AdjustmentListener;
    
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JScrollBar;
    import javax.swing.JScrollPane;
    import javax.swing.JTable;
    import javax.swing.table.JTableHeader;
    
    public class ScrollableJTable {
    
        private JFrame frame = null;
        private JPanel panel = null;
        private JTable table = null;
        private JTableHeader header = null;
        private JScrollPane pane = null;
        private JTable table1 = null;
        private JTableHeader header1 = null;
        private JScrollPane pane1 = null;
        private JTable table2 = null;
        private JTableHeader header2 = null;
        private JScrollPane pane2 = null;
    
        public static void main(String[] args) {
            EventQueue.invokeLater(new Runnable() {
    
                @Override
                public void run() {
                    ScrollableJTable scrollableJTable = new ScrollableJTable();
                }
            });
        }
    
        public ScrollableJTable() {
            frame = new JFrame("Creating a Scrollable JTable!");
            panel = new JPanel();
            String data[][] = {
                {"001", "vinod", "Bihar", "India", "Biology", "65", "First",
                    "001", "vinod", "Bihar", "India", "Biology", "65", "First"},
                {"002", "Raju", "ABC", "Kanada", "Geography", "58", "second",
                    "001", "vinod", "Bihar", "India", "Biology", "65", "First"},
                {"003", "Aman", "Delhi", "India", "computer", "98", "Dictontion",
                    "001", "vinod", "Bihar", "India", "Biology", "65", "First"},
                {"004", "Ranjan", "Bangloor", "India", "chemestry", "90", "Dictontion",
                    "001", "vinod", "Bihar", "India", "Biology", "65", "First"}
            };
            String data1[][] = {
                {"001", "vinod", "Bihar", "India", "Biology", "65", "First",
                    "001", "vinod", "Bihar", "India", "Biology", "65", "First",
                    "001", "vinod", "Bihar", "India", "Biology", "65", "First",
                    "001", "vinod", "Bihar", "India", "Biology", "65", "First"},
                {"002", "Raju", "ABC", "Kanada", "Geography", "58", "second",
                    "001", "vinod", "Bihar", "India", "Biology", "65", "First",
                    "002", "Raju", "ABC", "Kanada", "Geography", "58", "second",
                    "001", "vinod", "Bihar", "India", "Biology", "65", "First"},
                {"003", "Aman", "Delhi", "India", "computer", "98", "Dictontion",
                    "001", "vinod", "Bihar", "India", "Biology", "65", "First",
                    "003", "Aman", "Delhi", "India", "computer", "98", "Dictontion",
                    "001", "vinod", "Bihar", "India", "Biology", "65", "First"},
                {"004", "Ranjan", "Bangloor", "India", "chemestry", "90", "Dictontion",
                    "001", "vinod", "Bihar", "India", "Biology", "65", "First",
                    "004", "Ranjan", "Bangloor", "India", "chemestry", "90", "Dictontion",
                    "001", "vinod", "Bihar", "India", "Biology", "65", "First"}
            };
            String data2[][] = {
                {"001", "vinod", "Bihar", "India", "Biology", "65", "First",
                    "001", "vinod", "Bihar", "India", "Biology", "65", "First",
                    "001", "vinod", "Bihar", "India", "Biology", "65", "First",
                    "001", "vinod", "Bihar", "India", "Biology", "65", "First",
                    "001", "vinod", "Bihar", "India", "Biology", "65", "First",
                    "001", "vinod", "Bihar", "India", "Biology", "65", "First"},
                {"002", "Raju", "ABC", "Kanada", "Geography", "58", "second",
                    "001", "vinod", "Bihar", "India", "Biology", "65", "First",
                    "002", "Raju", "ABC", "Kanada", "Geography", "58", "second",
                    "001", "vinod", "Bihar", "India", "Biology", "65", "First",
                    "002", "Raju", "ABC", "Kanada", "Geography", "58", "second",
                    "001", "vinod", "Bihar", "India", "Biology", "65", "First"},
                {"003", "Aman", "Delhi", "India", "computer", "98", "Dictontion",
                    "001", "vinod", "Bihar", "India", "Biology", "65", "First",
                    "003", "Aman", "Delhi", "India", "computer", "98", "Dictontion",
                    "001", "vinod", "Bihar", "India", "Biology", "65", "First",
                    "003", "Aman", "Delhi", "India", "computer", "98", "Dictontion",
                    "001", "vinod", "Bihar", "India", "Biology", "65", "First"},
                {"004", "Ranjan", "Bangloor", "India", "chemestry", "90", "Dictontion",
                    "001", "vinod", "Bihar", "India", "Biology", "65", "First",
                    "004", "Ranjan", "Bangloor", "India", "chemestry", "90", "Dictontion",
                    "001", "vinod", "Bihar", "India", "Biology", "65", "First",
                    "004", "Ranjan", "Bangloor", "India", "chemestry", "90", "Dictontion",
                    "001", "vinod", "Bihar", "India", "Biology", "65", "First"}
            };
            String col[] = {"Roll", "Name", "State", "country", "Math", "Marks",
                "Grade", "Roll", "Name", "State", "country", "Math", "Marks", "Grade"};
            String col1[] = {"Roll", "Name", "State", "country", "Math", "Marks",
                "Grade", "Roll", "Name", "State", "country", "Math", "Marks", "Grade",
                "Roll", "Name", "State", "country", "Math", "Marks", "Grade",
                "Roll", "Name", "State", "country", "Math", "Marks", "Grade"};
            String col2[] = {"Roll", "Name", "State", "country", "Math", "Marks",
                "Grade", "Roll", "Name", "State", "country", "Math", "Marks", "Grade",
                "Roll", "Name", "State", "country", "Math", "Marks", "Grade",
                "Roll", "Name", "State", "country", "Math", "Marks", "Grade",
                "Roll", "Name", "State", "country", "Math", "Marks", "Grade",
                "Roll", "Name", "State", "country", "Math", "Marks", "Grade"};
            table = new JTable(data, col);
            table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
            table.setPreferredScrollableViewportSize(table.getPreferredSize());
            header = table.getTableHeader();
            header.setBackground(Color.yellow);
            pane = new JScrollPane(table);
            pane.setPreferredSize(new Dimension(400, 200));
            table1 = new JTable(data1, col1);
            table1.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
            table1.setPreferredScrollableViewportSize(table1.getPreferredSize());
            header1 = table1.getTableHeader();
            header1.setBackground(Color.yellow);
            pane1 = new JScrollPane(table1);
            pane1.setPreferredSize(new Dimension(400, 200));
            table2 = new JTable(data2, col2);
            table2.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
            table2.setPreferredScrollableViewportSize(table2.getPreferredSize());
            header2 = table2.getTableHeader();
            header2.setBackground(Color.yellow);
            pane2 = new JScrollPane(table2);
            pane2.setPreferredSize(new Dimension(200, 200));
            panel.setSize(500, 500);
            panel.add(pane);
            panel.add(pane1);
            panel.add(pane2);
            frame.add(panel);
            frame.pack();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setVisible(true);
            final JScrollBar bar = pane.getHorizontalScrollBar();
            final JScrollBar bar1 = pane1.getHorizontalScrollBar();
            final JScrollBar bar2 = pane2.getHorizontalScrollBar();
            bar2.addAdjustmentListener(new AdjustmentListener() {
    
                @Override
                public void adjustmentValueChanged(final AdjustmentEvent e) {
                    if (e.getValueIsAdjusting()) {
                        final int i = bar.getMaximum();
                        if (e.getValue() > i) {
                            EventQueue.invokeLater(new Runnable() {
    
                                @Override
                                public void run() {
                                    bar.setValue(i);
                                }
                            });
                        } else {
                            EventQueue.invokeLater(new Runnable() {
    
                                @Override
                                public void run() {
                                    bar.setValue(e.getValue());
                                }
                            });
                        }
                        final int i1 = bar1.getMaximum();
                        if (e.getValue() > i1) {
                            EventQueue.invokeLater(new Runnable() {
    
                                @Override
                                public void run() {
                                    bar1.setValue(i1);
                                }
                            });
                        } else {
                            EventQueue.invokeLater(new Runnable() {
    
                                @Override
                                public void run() {
                                    bar1.setValue(e.getValue());
                                }
                            });
                        }
                    }
                }
            });
        }
    }
    
    0 讨论(0)
提交回复
热议问题