0

I'm trying to create my first UI with netbeans. What I ultimately want to do is create something that will show a fairly arbitrary number of fields depending on certain variables but to begin with I'm simply trying to add a new component (or a number of new components) in response to a button being pressed. Here is my event handler:

public void newProjectSetup()
{
    javax.swing.JButton newThing = new javax.swing.JButton("test");
    getContentPane().add(newThing, "card2");
    validate();
    pack();
    System.out.println("NewProjectSetup activated");
}

This event handler is definitely firing in response to my pressing the relevant button, but the button "newThing" is not appearing. I'm creating the rest of my GUI with the netbeans GUI builder and have tried my best to copy the code from there, thinking that would work. I'd appreciate any help.

  • 1
    Hard to help - as we have no idea about the *context* in which this method exists. See [mcve]. – GhostCat Aug 29 '17 at 13:46
  • @GhostCat I don't know what further context to add. This is an event handler created to respond to a button push for a GUI created with netbeans GUI builder. I could copy and paste the entire class but that seems to breach the "minimal" guideline. – Jonnie Marbles Aug 29 '17 at 13:56
  • The point is: you add a component to a container. We know *nothing* about that container. It very much depends on your layout manager ... there are a lot of variables that could explain this. See https://stackoverflow.com/questions/25754454/using-java-pack-method for example. – GhostCat Aug 29 '17 at 14:02
  • @GhostCat never mind, I've worked it out now and written the answer below. Thanks for all your help. – Jonnie Marbles Aug 29 '17 at 14:06
  • But again: without much context, it is not very likely that your question or answer will be helpful for future readers. – GhostCat Aug 29 '17 at 14:20
  • I believe the answer to the question you're asking is "it's a JFrame". I would be interested to know if setVisible is only useful in this context, or if it's useful with all Java Swing components in all contexts? – Jonnie Marbles Aug 29 '17 at 14:51
  • Again: you are like person calling his garage "my car making strange noise. now tell me how to fix that?" And your answer then goes like "A kicked the rear end of the car, the noise is gone. Why is that?" – GhostCat Aug 29 '17 at 14:54
  • If the question is not helpful to others, consider removing it – c0der Aug 29 '17 at 15:20
  • @GhostCat Perhaps you could try phrasing your question better, that might help? I've given you the IDE (netbeans), the class of component being used (a JFrame) and the layout manager (or rather, you could infer it from "card2"). I am asking what *specific* context you feel is still lacking? I want to be able to make use of the resources here but it's difficult when people are rude and vague when responding. Thanks. – Jonnie Marbles Aug 29 '17 at 16:48
  • @GhostCat Further to the above, what I'm trying to ascertain is whether, in fact, there is some Swing component which would have Visible set to true upon creation in some other context, which is what your comments imply. If not, you are like the man at the garage insisting he needs to know what kind of engine I have when the problem is that the car is out of gas. – Jonnie Marbles Aug 29 '17 at 17:01
  • I will have a look tomorrow... – GhostCat Aug 29 '17 at 17:23

1 Answers1

0

I worked it out independently: I needed to add

newThing.setVisible(true);

To make it visible