简体   繁体   中英

JList is showing duplicates

I am working on a small example using Java Swing where I want to draw a sine graph on one panel and the co-ordinates of the graph in another panel. So I created a class that extends the JFrame then I created the JPanel for graph and co-ordinates. For displaying co-ordinates I am using JList . Now the problem is the co-ordinates are showing duplicate values. Here is my code:

public class MyFrame extends JFrame {
    JList list;
    DecimalFormat df = new DecimalFormat("#.###");
    DefaultListModel model = new DefaultListModel();

    private JPanel contentPane;

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    MyFrame frame = new MyFrame();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }


    public MyFrame() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 600, 600);
        contentPane = new JPanel();
        contentPane.setBorder(new LineBorder(new Color(0, 0, 0)));
        setContentPane(contentPane);
        contentPane.setLayout(new GridLayout(1, 2, 0, 0));

        JPanel panel = new MyGraph();
        panel.setBorder(new LineBorder(new Color(0, 0, 0)));
        contentPane.add(panel);

        list = new JList(model);
        list.setVisibleRowCount(4);

        JPanel panel_1 = new JPanel();
        panel_1.setBorder(new LineBorder(new Color(0, 0, 0)));

        panel_1.setLayout(new BoxLayout(panel_1, BoxLayout.Y_AXIS));

        JLabel lblNewLabel_1 = new JLabel("X - Y");
        panel_1.add(lblNewLabel_1);

        JScrollPane slistScroller = new JScrollPane(list);
        panel_1.add(slistScroller);

        contentPane.add(panel_1);

    }

    class MyGraph extends JPanel {

        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            int xBase = 10;
            int top = 100;
            int yScale = 10;
            int xAxis = 360;

            int yBase = top + yScale;

            g.drawLine(xBase, top, xBase, top + 2 * yScale);
            g.drawLine(xBase, yBase, xBase + xAxis, yBase);


            g.setColor(Color.red);

            int x2=0, y2=0;

            int x1 = xBase + 0;
            int y1 = yBase - (int) (10*Math.sin(0) * yScale);
            int i;
            for (i = 0; i < 10; i++) {
                x2 = xBase + i;
                y2 = yBase - (int) (10*Math.sin(i) * yScale);
                g.drawLine(x1, y1, x2, y2);
                x1 = x2;
                y1 = y2;
                df = new DecimalFormat("#.###");
                model.addElement(i +" -- " + df.format(10*Math.sin(i)));
            }
            model.addElement("------END----------");

        }

    }

}

Here is the output of my program:

在此处输入图片说明

As per my program, I have a for loop from angles 0 to 10 and I am adding the values to DefaultListModel model which is added to JList list .

Can someone please help me where I am doing mistake in this code?

Also even when I have this line list.setVisibleRowCount(4); , I was expecting only 4 records displayed to the user with a scroll-bar, but as per the output image it is not working like that.

paintComponent may be any number of times, for any number of reasons, try resizing the frame and see what happens.

Your paint method should focus on doing just that, painting.

You need to change the process so that the paintComponent becomes depend on the model, not the other way round.

Take a look at Painting in AWT and Swing for more details about painting in Swing.

You may also want to consider using a ListCellRender to render the data in the model in the JList , this way, you could more easily share the model and it's data.

See Writing a Custom Cell Renderer for more details

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM