1

I am trying to create a simple GUI for a BST tree allowing insertions and deletions. However, I am having a lot of trouble inserting components onto the GUI during runtime. What I thought of doing was to "refresh" the GUI every time an insertion or deletion happened. I created a method called printBst that creates Jlabels to display the numbered indexes as shown below, but they are not showing up. I've tried to revalidate and validate the GUI afterwards but it still doesn't work. Anyone have any ideas?

package source;
import javax.swing.*;
import source.BST.BSTnode;

import java.awt.Color;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.ArrayList;
import java.util.List;

public class Frame2 extends JFrame implements  ActionListener{
    BST bst;
    JPanel displayPanel, buttonPanel, totalGUI;
    JButton insertButton, deleteButton, resetButton;
    JTextField insertField, deleteField;

    public JPanel createContentPane (){
        bst = new BST();
        totalGUI = new JPanel();
        totalGUI.setLayout(null);

        displayPanel = new JPanel();
        displayPanel.setLayout(null);
        displayPanel.setLocation(10, 80);
        displayPanel.setSize(400, 300);
        totalGUI.add(displayPanel);

        buttonPanel = new JPanel();
        buttonPanel.setLayout(null);
        buttonPanel.setLocation(10, 0);
        buttonPanel.setSize(800, 80);
        totalGUI.add(buttonPanel);

        insertField = new JTextField(1);
        insertField.addActionListener(this);
        insertField.setLocation(0, 10);
        insertField.setSize(150, 30);
        buttonPanel.add(insertField);

        insertButton = new JButton("Insert");
        insertButton.setLocation(160, 10);
        insertButton.setSize(150, 30);
        insertButton.addActionListener(this);
        buttonPanel.add(insertButton);

        deleteField = new JTextField(1);
        deleteField.addActionListener(this);
        deleteField.setLocation(320, 10);
        deleteField.setSize(150, 30);
        buttonPanel.add(deleteField);

        deleteButton = new JButton("Delete");
        deleteButton.setLocation(480, 10);
        deleteButton.setSize(150, 30);
        deleteButton.addActionListener(this);
        buttonPanel.add(deleteButton);

        resetButton = new JButton("Reset");
        resetButton.setLocation(640, 10);
        resetButton.setSize(150, 30);
        resetButton.addActionListener(this);
        buttonPanel.add(resetButton);

        totalGUI.setOpaque(true);
        return totalGUI;
    }

    public void printBst(BSTnode node, int x, int x2, int y) {
        if (node != null) {
            JLabel current = new JLabel(""+ node.data);
            current.setLocation((x+x2)/2, y);
            current.setSize(100, 30);
            current.setHorizontalAlignment(0);
            displayPanel.add(current);
            printBst(node.left, x, (x2+x)/2, y+60);
            printBst(node.right, (x2+x)/2, x2, y+60);
            System.out.println("here");
        }
    }

    public void actionPerformed(ActionEvent e) {
        if(e.getSource() == insertButton)
        {
            bst.insert(Integer.valueOf(insertField.getText()));
            displayPanel.removeAll();
            printBst(bst.root, 0, 800, 0);
            totalGUI.revalidate();  
            validate(); 
        }
        else if(e.getSource() == deleteButton)
        {
            bst.delete(Integer.valueOf(deleteField.getText()));
            displayPanel.removeAll();
            printBst(bst.root, 0, 800, 0);
            totalGUI.revalidate();  
            validate(); 
        }
        else if(e.getSource() == resetButton)
        {
            bst.clear();
            displayPanel.removeAll();
            printBst(bst.root, 0, 800, 0);
            totalGUI.revalidate();  
            validate(); 
        }
    }

    private static void createAndShowGUI() {

        JFrame.setDefaultLookAndFeelDecorated(true);
        JFrame frame = new JFrame("[=] JButton Scores! [=]");

        Frame2 demo = new Frame2();
        frame.setContentPane(demo.createContentPane());

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(900, 400);
        frame.setVisible(true);

    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }
}
1
  • 2
    Your BST - Binary Search Tree Class is missing, so none can test this program, that's why no response, for such a long time. Please do provide a working code (SSCCE) +1 for attention :-) Commented Apr 30, 2012 at 4:16

1 Answer 1

3

Make sure you call revalidate() and repaint() after you remove components from displayPanel ie:

displayPanel.removeAll();
printBst(0, 800, 0);
displayPanel.revalidate();
displayPanel.repaint();

Also, note that:

printBst(0, 800, 0);

results in invalid (not in bounds) coordinates inside displayPanel, which size is defined as (400, 300) The top-left corner of a window is 0,0. Try the following and you should see your label somewhere in the middle of the panel:

printBst(0, 400, 0);

Absolute positioning can be very difficult to manage. Check out A Visual Guide to Layout Managers and see if you can find an appropriate layout to help you.

You may also consider ready to go frameworks that can give you 2D canvas to work on. For example JGraph, JFreeChart or Piccolo2D.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.