you need to develop a class that enters data (numbers) and draws bar charts. I have a problem that I can not fix. When adding data to the list, the add () method works once and no longer starts, as if I didn’t want it. You do not know how to fix this?
import javax.swing.*; import javax.swing.border.Border; import javax.swing.event.AncestorListener; import javax.swing.event.ListSelectionEvent; import javax.swing.event.ListSelectionListener; import java.awt.*; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; public class BarChart { private static JFrame frame; private static JList list; private static JPanel panel1; private static GraphicsComponent gc; private static String methodName; private static ArrayList<Integer> lst = new ArrayList<>(); private static final String[] FUNC = { "Add", "Draw" }; public static void main(String[] args) { frame = new JFrame(); frame.setSize(600, 400); frame.setTitle("Application"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); panel1 = new JPanel(); panel1.setLayout(new GridLayout(0,1)); list = new JList(FUNC); list.setLayoutOrientation(JList.VERTICAL_WRAP); list.addListSelectionListener(new ListSelectionListener() { @Override public void valueChanged(ListSelectionEvent e) { methodName = list.getSelectedValue().toString(); frame.setTitle(methodName); switch (methodName){ case "Add": try { add(); } catch (IOException e1) { e1.printStackTrace(); } break; case "Draw": gc = new GraphicsComponent(); updateScreen(); break; } } }); panel1.add(list); frame.add(panel1); frame.setVisible(true); } private static void draw(Graphics2D g2){ Rectangle rect = null; Color col = null; int x = 40; int y = Collections.max(lst); int temp = y; for (int i = 0; i < lst.size(); i++) { if (i > 0){ if (lst.get(i) < 0){ col = Color.RED; lst.set(i, lst.get(i) * (-1)); if (lst.get(i) > lst.get(i-1)){ y -= Math.abs((lst.get(i) - y)) + (lst.get(i) - lst.get(i-1))+ Collections.min(lst); } else{ y += Math.abs((lst.get(i) - y)) + ((lst.get(i-1) - lst.get(i)) - (Math.abs(temp - lst.get(i)))) + Collections.min(lst); } } else{ col = Color.BLACK; if (lst.get(i) > lst.get(i-1)){ y -= Math.abs((lst.get(i) - y)) + (lst.get(i) - lst.get(i-1)); } else{ y += Math.abs((lst.get(i) - y)) + ((lst.get(i-1) - lst.get(i)) - (Math.abs(temp - lst.get(i)))); } } } rect = new Rectangle(x, y, 40, lst.get(i)); x += 80; g2.setColor(col); g2.fill(rect); g2.draw(rect); } } static class GraphicsComponent extends JComponent{ @Override protected void paintComponent(Graphics g) { Graphics2D g2 = (Graphics2D) g; draw(g2); } } private static void add() throws IOException { BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); System.out.println("Enter number: "); lst.add(Integer.parseInt(reader.readLine())); } private static void updateScreen(){ panel1.removeAll(); //panel1.add(list); panel1.add(gc); frame.revalidate(); } }