Hi,

I have checked examples and tutorials but never seem to get a direct answer to a couple of questions. When using a Gui with a JButton (Netbeans)where does the code for the array go in order to be displayed? Here is the code for the array and I just want to click the button and have it display. Where do I put it?(the code) This website has been extremely helpful to me as perhaps I am a slow learner and prefer not to copy and paste but I really want to learn the logic behind the code. Thanks in advance for any help. I do not need help with making the JButton but I do need help in having this display after pressing the button.

lynnajoe

int [] aryNums = new int [6];
        aryNums [0] = 10;
        aryNums [1] = 5;
        aryNums [2] = 6;
        aryNums [3] = 9;
        aryNums [4] = 7;
        aryNums [5] = 3;
        
        Arrays.sort(aryNums);

        int i;
        for (i =0; i <aryNums.length;i++) {
        
        System.out.println("number: " + aryNums[i]);
    }
    
    }

Dani AI

Generated

Short answer: keep the array logic in a helper method of your form class and call that method from the button’s action handler that NetBeans generates. That follows ’s pointer to ActionListener and ’s advice to avoid heavy logic directly inside the listener.

Put the helper method anywhere in the same class but outside NetBeans’ guarded initComponents() area. If the array must persist between clicks, make it an instance field and initialize it in the constructor or a setup method; if it is only needed when the button is pressed, create it inside the helper. NetBeans will create an event method for the button (something like jButton1ActionPerformed); that method should be a one-line call to your helper.

Example skeleton (place your array work inside displayNumbers):

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
    displayNumbers();
}

private void displayNumbers() {
    // build or access your array here
    // sort/format it, then update a GUI component (JTextArea, JList, JLabel)
    // or use JOptionPane to show the result
    javax.swing.JOptionPane.showMessageDialog(this, "Results here");
}

Troubleshooting: if nothing appears when you click, make sure the event handler is actually attached (right-click the button in the designer -> Events -> Action -> actionPerformed). Use a quick JOptionPane or a breakpoint to confirm the handler runs. Don’t put long-running work on the Event Dispatch Thread; use SwingWorker for heavier processing. Finally, avoid editing NetBeans’ guarded code—add your methods below the generated section so the GUI builder keeps working.

Recommended Answers

All 3 Replies

Have You learned about event listeners?
Check out ActionListener at the Java API, that's the class used for getting button clicks.

Hi,

I do know about ActionListeners. It is an easy question and I may sound lost because I tried the code in the Netbeans area where they say to place the code and it doesn't come up at all. The window shows but that's it. I only wanted to ask where it goes and not for code itself. If you see what I am saying. It is a basic question that's all. I wanted someone to explain it to me in plain language and not code.

lynnajoe

As a general rule you don't want to put code of that kind of complexity and context directly into an ActionListener. Best practice is to put it into a method in the appropriate class, then just call that method from a one-line ActionListener

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.