-1

when i try to run this class after i search it (in the actionPerformed) it doesn't update the JFrame, i tried a whole bunch of different things (repaint(), revalidate(), etc.) but none of them work, if you could spot the problem that would help alot.

public class Scroll extends JPanel implements ActionListener {

    private static final int N = 16;
    private JTextArea last;
    private int index;
    JButton sort, Quit, search;
    arraySorter array;
    JFrame f;
    String[]sValue;
    double[]dValue;
    boolean checks;
    public Scroll(String[]sValues, double[]dValues, boolean check, String compare) {
        sValue=sValues;
        dValue=dValues;
        checks=check;
        this.setLayout(new GridLayout(0, 1, 3, 3));
        System.out.println(compare);
        if(check){   
            for (int i = 0; i < sValues.length; i++) {
                    if(sValues[i].contains(""+compare+""))this.add(create(i, sValues[i], 0, true));
                }
        }else{
            for (int i = 0; i < dValues.length; i++) {
                this.add(create(i, null, dValues[i], false));
            }
        }
    }
    private JTextArea create(int i, String j, double k, boolean check) {
        if(check)last =  new JTextArea(i+1+". "+j+"");
        else last =  new JTextArea(i+1+". "+k+"");
        last.setEditable(false);
        return last;
    }

public void display() {

        f = new JFrame("This is your sorted list!!!!");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.add(new JScrollPane(this));
        f.invalidate();
        f.validate();
        f.repaint();
        f.setSize(300, 300);

        JPanel panel=new JPanel();
        f.add(panel, BorderLayout.NORTH);
        Quit=new JButton("Quit");//sets instruction button for frame
        //adds an actionlistener
        Quit.addActionListener(this);
        panel.add(Quit);


        search=new JButton("search");//sets instruction button for frame
        //adds an actionlistener
        search.addActionListener(this);
        panel.add(search);
        f.invalidate();
        f.validate();
        f.repaint();
        f.setVisible(true);
    }
    public void actionPerformed(ActionEvent e) {
        if(e.getSource()==Quit){
            f.dispose();
            array=new arraySorter();
        }

        if(e.getSource()==search){
            f.dispose();
            String compare=JOptionPane.showInputDialog("What do you want to search for?");
            f.removeAll();
            new Scroll(sValue, dValue, checks, compare);
            display();
        }
    }

}
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
user3063455
  • 123
  • 1
  • 11
  • Try maybe replacing f.dospose() with f.setVisible(false). – selalerer Feb 15 '14 at 06:03
  • Not sure what exactly what you're trying to do, but I see removing components and adding components. Seems to me your situation is better suited for `CardLayout`. See examples from [**this question**](http://stackoverflow.com/questions/21592492/how-can-i-switch-between-jpanels) and runnable example from [**this answer**](http://stackoverflow.com/a/21460065/2587435) and [**How to use CardLayout**](http://docs.oracle.com/javase/tutorial/uiswing/layout/card.html) – Paul Samsotha Feb 15 '14 at 06:03
  • it doesn't update the JFrame means is the JFrame not displayed or the JFrame is displayed but JPanel is not displayed? – JavaTechnical Feb 15 '14 at 06:23

1 Answers1

0

From the looks of it, you're closing the current JFrame, and creating an entirely new JFrame every time you search. Why not simply create an updater method that populates everything inside the current JFrame? The method you're using appears wildly inefficient, and is more than likely the cause of your problem.

JD Davis
  • 3,517
  • 4
  • 28
  • 61