Problem with new window opening with basic test JTree program

I'm still getting to grips with Swing, and have now moved on to basic JTrees.
I'm currently trying to use an add and delete button to add new nodes to the root etc. The addition and deletion is working ok, except that each time the addButton is pressed, a completely new window is created on top of the current window with the new node added to the root etc.
I know this is probably a simple mistake, but I can't quite see it at the moment.
My current code is as follows,
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.tree.*;
public class TreeAdditionDeletionTester_1 extends JFrame implements ActionListener
    private JTree tree;
    private DefaultTreeModel treeModel;
    private DefaultMutableTreeNode rootNode;
    private JButton addButton;
    private JButton deleteButton;
    private JPanel panel;
    private int newNodeSuffix = 1;
    private static String Add_Command = "Add Node";
    private static String Delete_Command = "Remove Node";
    //DefaultMutableTreeNode rootNode = new DefaultMutableTreeNode("Root");
    //TreeAdditionDeletionTester_1 treeChange;
    public TreeAdditionDeletionTester_1() 
        setTitle("Tree with Add and Remove Buttons");
        //DefaultMutableTreeNode rootNode = new DefaultMutableTreeNode("Root");
        rootNode = new DefaultMutableTreeNode("Root");
        treeModel = new DefaultTreeModel(rootNode);
        tree = new JTree(treeModel);
        tree.setEditable(false);
        tree.setSelectionRow(0);
        JScrollPane scrollPane = new JScrollPane(tree);
        getContentPane().add(scrollPane, BorderLayout.CENTER);
        JPanel panel = new JPanel();
        addButton = new JButton("Add Node");
        addButton.setActionCommand(Add_Command);
        addButton.addActionListener(this);
        panel.add(addButton);
        getContentPane().add(panel, BorderLayout.SOUTH);
        deleteButton = new JButton("Delete Node");
        deleteButton.setActionCommand(Delete_Command);
        deleteButton.addActionListener(this);
        panel.add(deleteButton);
        getContentPane().add(panel, BorderLayout.SOUTH);    
        setSize(300, 400);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setVisible(true);
    public void actionPerformed(ActionEvent event) 
       TreeAdditionDeletionTester_1 treeChange = new TreeAdditionDeletionTester_1();
        String command = event.getActionCommand();
        if (Add_Command.equals(command)) {
            //Add button clicked
            treeChange.addObject("New Node " + newNodeSuffix++);
        } if (Delete_Command.equals(command)) {
            //Remove button clicked
            treeChange.removeCurrentNode();
    public void removeSelectedNode()
        //get the selected node
        DefaultMutableTreeNode selNode = (DefaultMutableTreeNode)tree.getLastSelectedPathComponent();
        //method from Class JTree
        if (selNode != null)
            //get the parent of the selected node
            MutableTreeNode parent = (MutableTreeNode)(selNode.getParent());
            //getParent() from Interface TreeNode
            // if the parent is not null
            if (parent != null)
                //remove the node from the parent
                treeModel.removeNodeFromParent(selNode);//method from Class DefaultTreeModel
                return;
    public DefaultMutableTreeNode addObject(Object child) {
        DefaultMutableTreeNode parentNode = null;
        TreePath parentPath = tree.getSelectionPath();
        if (parentPath == null) {
            parentNode = rootNode;
        } else {
            parentNode = (DefaultMutableTreeNode)
                         (parentPath.getLastPathComponent());
        return addObject(parentNode, child, true);
    public DefaultMutableTreeNode addObject(DefaultMutableTreeNode parent,
                                            Object child,
                                            boolean shouldBeVisible) {
        DefaultMutableTreeNode childNode =
                new DefaultMutableTreeNode(child);
        if (parent == null) {
            parent = rootNode;
        treeModel.insertNodeInto(childNode, parent,
                                 parent.getChildCount());
        //Make sure the user can see the new node.
        if (shouldBeVisible) {
            tree.scrollPathToVisible(new TreePath(childNode.getPath()));
        return childNode;
    public DefaultMutableTreeNode addObject(DefaultMutableTreeNode parent,
                                            Object child) {
        return addObject(parent, child, false);
    public void removeCurrentNode() {
        TreePath currentSelection = tree.getSelectionPath();
        if (currentSelection != null) {
            DefaultMutableTreeNode currentNode = (DefaultMutableTreeNode)
                         (currentSelection.getLastPathComponent());
            MutableTreeNode parent = (MutableTreeNode)(currentNode.getParent());
            if (parent != null) {
                treeModel.removeNodeFromParent(currentNode);
                return;
     * Main method to run as an Application
    public static void main(String[] arg) 
        TreeAdditionDeletionTester_1 addDeleteTree = new TreeAdditionDeletionTester_1();
}      Thanks.

I'm currently trying to use an add and delete button
to add new nodes to the root etc. The addition and
deletion is working ok, except that each time the
addButton is pressed, a completely new window is
created on top of the current window with the new
node added to the root etc.
I know this is probably a simple mistake, but I can't
quite see it at the moment.
Look at the actionPerformed code for your buttons. The first thing you do there is to create a new TreeAdditionDeletionTester_1, so I don't know why you are surprised you get a new window. There is no need to create a new instance in there, just call addObject/removeCurrentNode on the "this" instance.

Similar Messages

  • New windows open with different toolbars (and missing URL bar)

    When I select the "New Window" button or "Open in New Window" from the context menu, a new Firefox window opens - however, the toolbars are completely different from the original open window. Specifically, the URL location bar is missing.
    I can add it back, but the change then modifies my original toolbar setup.
    More bad behavior: Even if I modify the new window back to the way I wanted it, new windows opened from that window revert back to the modified version, with no location toolbar.

    You can try:
    * http://kb.mozillazine.org/Prevent_websites_from_disabling_new_window_features
    * http://kb.mozillazine.org/JavaScript#Advanced_JavaScript_settings
    Start Firefox in <u>[[Safe Mode]]</u> to check if one of the extensions is causing the problem (switch to the DEFAULT theme: Firefox (Tools) > Add-ons > Appearance/Themes).
    * Don't make any changes on the Safe mode start window.
    * https://support.mozilla.com/kb/Safe+Mode
    * https://support.mozilla.com/kb/Troubleshooting+extensions+and+themes

  • Firefox will not open new windows, opens with session restore every time I use it, and won't let me open the throubleshooting tab... Why?

    Whenever I try to open a new window, nothing happens. This includes clicking ctrl+N and through the drop down menu.
    Also, whenever I start firefox to browse the internet, it always open with a Session Restore with the previous session listed, even though I closed all the tabs and shut it down normally without any prompts telling me about saving multiple tabs.
    Finally, when I tried to troubleshoot this problem, the tab never appeared. The only options that work on the Help Menu are:
    - Firefox Help (after a few clicks)
    - Report Broken Web Site
    - Check for Updates
    - About Firefox

    Start Firefox in <u>[[Safe Mode]]</u> to check if one of the extensions is causing the problem (switch to the DEFAULT theme: Firefox (Tools) > Add-ons > Appearance/Themes).
    *Don't make any changes on the Safe mode start window.
    *https://support.mozilla.com/kb/Safe+Mode
    *https://support.mozilla.com/kb/Troubleshooting+extensions+and+themes
    There are other things that need your attention.
    Your above posted system details show outdated plugin(s) with known security and stability risks that you should update.
    *Shockwave Flash 10.0 r45
    Update the [[Managing the Flash plugin|Flash]] plugin to the latest version.
    *http://kb.mozillazine.org/Flash
    *http://www.adobe.com/software/flash/about/

  • When I open a link a new window opens with the toolbar and sidebar missing, on my previous machine the link simply opened in a new tab (not window).

    When I open a link in Firefox instead of opening a new tab it opens in a new window with all of the toolbars missing including the sidebar, right clicking anywhere gives me no options to restore the toolbars. My old machine simply opened the same link that I am trying in a new tab with all my toolbars available.

    Start Firefox in <u>[[Safe Mode|Safe Mode]]</u> to check if one of the extensions (Firefox/Tools > Add-ons > Extensions) or if hardware acceleration is causing the problem (switch to the DEFAULT theme: Firefox/Tools > Add-ons > Appearance).
    *Do NOT click the Reset button on the Safe Mode start window or otherwise make changes.
    *https://support.mozilla.org/kb/Safe+Mode
    *https://support.mozilla.org/kb/Troubleshooting+extensions+and+themes

  • After downloading Maverick, suddenly I'm having problems with new windows (like with Blackboard) closing automatically. Anyone else?

    A few weeks after downloading Maverick, I noticed that some attempts to use software that opens new windows (like Blackboard) are now failing as soon as that window opens. Is this a glitch with Maverick?

    Yes! Been living in a hellish nightmare since Sept-Oct. Unlike you, however, Apple has been of no help. They say the iPad is unhackable, plist and crash reports are none of my business and who understands them anyway, their route tracing apps are probably not reliable. The best they could do was wipe the iPad clean. Lost photos they said would be in the cloud....
    Almost immediately was back in somebody's VPN, their remote controller, video, audio, working. Got the purple buddy for the first time today. I have been invaded, my iPad no longer feels like my own, I have lost thanksgiving, Christmas, new year trying to track down the invaders, signed up for a Cisco course, set/reset passwords.
    I don't know who else to contact... Or what next to do.
    I've read similar frustration in other posts, scattered around the support communities. Yours is the only one that has worked toward a solution.
    How did you get anyone at apple to look at your logs and acknowledge there is a problem?
    Would be happy for a suggestion on how to proceed. Already been the apple store genius and tech on phone route.

  • Handling new window open with WebView

    Hi,
    I'm developing an app where I use webview to display a webpage. The webpage has a link where a file gets downloaded automatically when selected. What I'm trying to do is to get the link of the downloadable file and try to download it from my app. The problem
    I'm facing is that IE automatically opens whenever the downloadable link is pressed. I cant get event handler to detect this scenario. I've seen there could be a solution using Javascript but I dont know how to implement it. I found there is x-ms-webview.MSWebViewNewWindowRequested
    but this one is for Windows 10 only. I wonder if there is an alternative solution for WP8.1. Thanks

    Hi Rob,
    Now I've tried two solutions and none of them worked as expected. Here are the options
    1) In the WebView.NavigationStarted, I download the html content and then modify it to remove target=_blank which triggers opening a new window. I then call  WebView.NavigateToString to call the new html content. Looks like it works somehow but
    the reloaded page does not contain images, links, etc. Is there a way to fix this?
    2) I've tried the solution described in this link:
    http://stackoverflow.com/questions/18799534/notify-click-action-on-button-in-webview
    which is essentially injecting javascript code to handle button clicks.  The code looks like this:
    private void WebView_OnLoadCompleted(object sender, NavigationEventArgs e)
    webView.InvokeScript("eval", new[]
    "var loginButon=document.getElementById(\"login\");" +
    "if (loginButon.addEventListener) {" +
    "loginButon.addEventListener(\"click\", clickFunction, false);" +
    "} else {" +
    "loginButon.attachEvent('onclick', clickFunction);" +
    "} " +
    "function clickFunction(){" +
    " window.external.notify('loginClick');"+
    However, I get error when the code executes webView.InvokeScript (Execption:0x80020101). Do you have any idea why?
    Kind Regards

  • I downloaded fbdownloader and now having a oproblem with random window opening with other websites and a link on firefox I can't get rid of

    I downloaded fbdownloader and after doing so I noticed changes in my facebook and I am getting random webpages popping up on new windows. there is a link on my facebook that I can't get rid of and I have uninstalled this program but seems there is lingering things that I can't get rid of. What can I do?

    You have probably downloaded malware. Do a Microsoft Security Scan at http://www.microsoft.com/security/scanner to check for viruses and tell me the result

  • All of a sudden when i click any button or link a new window opens with an ad

    I have used Firefox for about 7 years. No issues...
    I recently installed Ghostery add on.
    The only way to describe it is:
    I go to a page..
    Click a link (or close to it) or enter or any other button/link that would take me to what I expect..
    BUT instead it opens a new tab to an advertisement site (quibids, P&G, many other random ads) - a few of them I blacklisted with BLOCKSITE (installed after the problem started happening) but several I can't because I would actually use them when I need to (ex: Vistaprint.com)
    When I go back to the original site and click the same link and click it again it works as intended.
    This happens across all sites that I am on...
    It is like there is a hidden link within the one I am clicking on.
    I have pop ups blocked in my browser but this gets around it somehow.
    Since GHOSTERY installed, some sites do not work correctly. If I click on a picture, or something that I want to expand it is as if I am doing nothing at all because it won't complete the action, the functionality is broken.
    I have white listed a couple sites but this is random and if I whitelist everything I come across then it opens me back to what I am trying to avoid..
    EXAMPLES:
    went on Heat.com, clicked the button to go to the White Hot Gear instead it opened a new tab brandonline.com
    was on LinkedIn and hit the comment button on a thread, then it opened a new tab vistaprint.com
    was on publix.com, hit the order refills button, then it opened a new tab premium-promos.net
    did a google search clicked on the link I wanted, it opened a new tab premiumgiftrewards.net
    EACH time I go back to the original site and click the same button/link/etc it takes me where expected.
    Thanks in advance for your help!

    Both Firefox and IE have the same problem. The cursor instead of being a finger when pointing to a link is still an arrow. However I can click anywhere on the page and an ad will pop up. I have eliminated all unwanted bars in Firefox, I ran Malwarebytes until it came out clean. I also have AVG which had detected a few items on it's own. But when I get into Firefox I still have exactly the same problem. Now it will not let me download Browser Safeguard. Usually after one click the pointer returns to normal for a few screens. When I try to click to download Browser Safeguard it keeps bringing up different ads or it tries to get me to upgrade Firefox or some other browser helper program which I know are all BS tactics to get me to load their junk again. PLease help I'm an engineer and pretty good with PCs but this one has me beat at least so far.

  • Everytime I sign in or out of Ebay a new window opens with an advert.

    I've used No-script to block as untrusted but the window still opens. Is this a Firefox or Ebay problem and how do I stop it?

    Both Firefox and IE have the same problem. The cursor instead of being a finger when pointing to a link is still an arrow. However I can click anywhere on the page and an ad will pop up. I have eliminated all unwanted bars in Firefox, I ran Malwarebytes until it came out clean. I also have AVG which had detected a few items on it's own. But when I get into Firefox I still have exactly the same problem. Now it will not let me download Browser Safeguard. Usually after one click the pointer returns to normal for a few screens. When I try to click to download Browser Safeguard it keeps bringing up different ads or it tries to get me to upgrade Firefox or some other browser helper program which I know are all BS tactics to get me to load their junk again. PLease help I'm an engineer and pretty good with PCs but this one has me beat at least so far.

  • While using Firefox, I keep getting a new window opening with the comment, Firefox can't find the file at /C:/Program Files/Mozilla Firefox/:þê+ìñLáR¸=%1AÞÀšlÀ£µSëä85r%1EI:U%0EÐL Vbj¨×mb¾Lvø5%13›H., how can I get rid of this file?

    I can delete this window, but it keeps recurring, sometimes within 5 - 10 seconds.

    The ONLY support for Mozilla programs are web sites like this.
    The people who answer questions here, for the most part, are other Firefox users volunteering their time (like me), not Mozilla employees or Firefox developers.
    If you want to leave feedback for Firefox developers, you can go to the Firefox ''Help'' menu and select ''Submit Feedback...'' or use [https://input.mozilla.org/feedback this link]. Your feedback gets collected at http://input.mozilla.org/, where a team of people read it and gather data about the most common issues.

  • "move to new window" opens blank window (no content)

    On a tab with content I right click and select "move to new window". A new window opens with an empty "New Tab". The content is still in the old window. Feature worked fine in 3.xx

    I find I have the blank windows open only when "cool previews" is enabled. I would like to be able to use both features.

  • When I click on the - fire fox disappears from the task bar; it is still open but gone from the task bar, if I click on any icon it opens a new Firefox window, I end up with several windows open but none on the task bar.

    Question
    When I click on the - to minimize fire fox, it does not close the window but the firefox icon disappears from the task bar; it is still open but gone from the task bar, if I click on any other icon it opens a new Firefox window, I end up with several windows open but none on the task bar.

    Boot the computer in Windows Safe Mode with network support (press F8 on the boot screen) as a test to see if that helps.
    Do a malware check with some malware scanning programs on the Windows computer.<br>
    Please scan with all programs because each program detects different malware.<br>
    All these programs have free versions.
    Make sure that you update each program to get the latest version of their databases before doing a scan.
    *Malwarebytes' Anti-Malware:<br>http://www.malwarebytes.org/mbam.php
    *AdwCleaner:<br>http://www.bleepingcomputer.com/download/adwcleaner/<br>http://www.softpedia.com/get/Antivirus/Removal-Tools/AdwCleaner.shtml
    *SuperAntispyware:<br>http://www.superantispyware.com/
    *Microsoft Safety Scanner:<br>http://www.microsoft.com/security/scanner/en-us/default.aspx
    *Windows Defender: Home Page:<br>http://www.microsoft.com/windows/products/winfamily/defender/default.mspx
    *Spybot Search & Destroy:<br>http://www.safer-networking.org/en/index.html
    *Kasperky Free Security Scan:<br>http://www.kaspersky.com/security-scan
    You can also do a check for a rootkit infection with TDSSKiller.
    *Anti-rootkit utility TDSSKiller:<br>http://support.kaspersky.com/5350?el=88446
    See also:
    *"Spyware on Windows": http://kb.mozillazine.org/Popups_not_blocked

  • Whenever I open a new window of firefox a new tab opens with the new window. How do I get it to stop doing that?

    Whenever I open a new window of Firefox, the new window opens at the hope page (like I want it to), but then a tab opens up with the new window. I want Firefox to stop doing this. When I open a new window I just want the window to open. I don't know how to fix this as I have know idea how I made this happen. This first happened when I hit control + N to open a new window, but I didn't hit the "N" cleanly. I pressed one of the other keys around the "N" and this issue started.

    Start Firefox in <u>[[Safe Mode|Safe Mode]]</u> to check if one of the extensions (Firefox/Tools > Add-ons > Extensions) or if hardware acceleration is causing the problem (switch to the DEFAULT theme: Firefox/Tools > Add-ons > Appearance).
    *Do NOT click the Reset button on the Safe Mode start window.
    *https://support.mozilla.org/kb/Safe+Mode
    *https://support.mozilla.org/kb/Troubleshooting+extensions+and+themes

  • Firefox is adding advertising links to certain words on my website cornish-surfing.co.uk, and when I open a new tab an additional window opens with unwanted sit

    Firefox is adding advertising links to certain words on my website cornish-surfing.co.uk, and when I open a new tab an additional window opens with unwanted sites.
    1. How can I stop these things from happening?
    2. Why is it happening?
    These things only happen when I’m using the Firefox browser. I use Sophos antivirus and firewall and the scans indicate my system is clean. So I’m assuming that it’s built into Firefox or there’s a security flaw with Firefox.
    Can you let me know which and how I can stop it from happening?
    Graham Jappé

    hello, this sounds like a problem possibly caused by adware on your pc (certainly nothing that is shipping with a plain official firefox build).
    please go to ''firefox > addons > extensions'' & remove any suspicious entries (toolbars, things that you have not installed intentionally, don't know what purpose they serve, etc). also go to the windows control panel / programs and remove all toolbars or potentially unwanted software from there and run a full scan of your system with the security software that you have in place and different other tools like the [http://www.malwarebytes.org/products/malwarebytes_free free version of malwarebytes] & [http://www.bleepingcomputer.com/download/adwcleaner/ adwcleaner].
    [[Remove a toolbar that has taken over your Firefox search or home page]]
    [[Troubleshoot Firefox issues caused by malware]]

  • Upon firefox update this morning, every window opens with error: CaptureMovieFox could not be initialized. Some files of the extension may be missing or there is a problem with the Firefox.

    upon firefox update this morning, every window opens with this error in a popup: ''CaptureMovieFox could not be initialized. Some files of the extension may be missing or there is a problem with the Firefox.''
    when I close the popup, the original window opens seemingly without issue.

    http://getfirebug.com/releases/firebug/1.8/
    visit this and find the compatible version of this f***ing firebug. which certainly has nothing to do with us (not-programmers). i have been having this problem for months but now the last link worked for my firefox 6. Find yours here.

Maybe you are looking for

  • Creation of a dynamic Form layout

    [Required Layout|http://imgur.com/aX3hR] Hi All, I am trying to create a dynamic layout using Apex as shown in the above link. The required functionality is to allow the user to select the database (note only 2 shown but there are several), this woul

  • I want to install and extension globally

    I am an it admin and want to install an extension globally, but placing the .xpi file in C:\Program Files (x86)\Mozilla Firefox\browser\extensions\ or C:\Program Files (x86)\Mozilla Firefox\plugins and restarting the browser does not prompt to instal

  • How to resample unequally spaced data in Array form?

    I have an array of unequally spaced time data and corresponding signal data taken at these times. I would like to resample the signal data to be equally spaced in time (constant dt). How do I introduce the time data into the signal, then resample? I

  • Text length in Sap Script

    Hi all, how can I force auto adjust of Text leght in Sap Script ? I have a text to print, and when it is taller than page area it is printed in another page Ok. But I don't want that it is printed in another page, I want that it is adjusted automatic

  • After reloading adobe creative suite cs3 on my mac 10.4.11 acrobat will not open any pdfs it previously opened. help!

    after reloading adobe creative suite cs3 on my mac 10.4.11 acrobat will not open any pdfs it previously opened. help!