0

I'm trying to create an task-list type of applet with a jPanel within a jFrame. The jPanel has items such as an Text Input block, a slider, a "Complete Task" button, and a "Clear Task" button. I also have an "Add Task" button that will ask a user the name of the new task and then create an identical jPanel with the same configuration. My question is, how do I create the new jPanel with the same buttons/sliders/input box but these have a different variable name every time the "Add Task" button is clicked? I attempted to do this by creating a separate method that takes a random number, converts it into a string, and then use that string value as the name of the new jPanel...it gives me an error saying "double cannot be dereferenced".

public void createNewTask(){
  double panelTask = Math.random();
  Double.toString(panelTask);

  javax.swing.GroupLayout panelTaskLayout = new javax.swing.GroupLayout(panelTask);
  panelTask.setLayout(panelTaskLayout);
  panelTaskLayout.setHorizontalGroup(
        panelTaskLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(panelTaskLayout.createSequentialGroup()
            .addContainerGap()
                .addGroup(panelTaskLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(panelTaskLayout.createSequentialGroup()
                    .addComponent(completeTask, javax.swing.GroupLayout.DEFAULT_SIZE, 259, Short.MAX_VALUE)
                    .addGap(18, 18, 18)
                    .addComponent(clearTask, javax.swing.GroupLayout.PREFERRED_SIZE, 103, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addComponent(inputText)
                .addComponent(sliderExample, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addContainerGap())
    );
    panelTaskLayout.setVerticalGroup(
        panelTaskLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, panelTaskLayout.createSequentialGroup()
            .addContainerGap()
            .addComponent(inputText, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
            .addComponent(sliderExample, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addGap(10, 10, 10)
            .addGroup(panelTaskLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                .addComponent(completeTask, javax.swing.GroupLayout.DEFAULT_SIZE, 71, Short.MAX_VALUE)
                .addComponent(clearTask, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
    );
}
A.Sharma
  • 2,771
  • 1
  • 11
  • 24
  • I don't really understand why, if the components are to be exactly the same, you need to create a whole new panel of components. Can you please clarify. – Paul Samsotha Apr 21 '14 at 01:27
  • Isn't that what the code is doing? I copied the same code from the netbeans form builder in this separate method. I just want to change the variable names of each of them dynamically so that I can get values from the sliders and have a percentage complete message...I don't want the same variable name for every single task. For example: jPanel1 then the user clicks the "Add Task" button and now there is jPanel1 and jPanel2. Within these two there is sliderExample1 and sliderExample2. Then in a separate class I want to take value = sliderExample1.getValue() + sliderExample2.getValue().. – A.Sharma Apr 21 '14 at 01:36
  • Maybe instead, a better option would be to have model objects that hold the state for each (Task, or whatever, I'm not sure). Say class `Task` that holds the state for fields and slider values. Or just use a `JTable` to hold the state for each task. That's what I don't understand. Why the need for different variable names? What is the purpose. Maybe we can offer a better solution. – Paul Samsotha Apr 21 '14 at 01:39
  • thats what I've resorted to. I can do what I want to do by building a predetermined number of tasks (currently I have 8 jPanels made with the GUI builder). I have a separate class that does the addition of the values. I guess my question is, if I specify a new swing control to be created when a button is pressed, does java create a random variable name? If it does this, how do I get this variable name? And I have the buttons/swing components wrapped within a jPanel because I want to change the background color depending on importance of the task. – A.Sharma Apr 21 '14 at 01:51
  • No, it doesn't work like that. Variable names can't be created at run time. I still don't 100% understand your requirements, but check out the `CardLayout` in my links below. See if it offers you any alternative to what you are trying to achieve – Paul Samsotha Apr 21 '14 at 01:53
  • Shoot im hoping to transition this to an android app for fun so I need to figure out how to enable this add task feature – A.Sharma Apr 21 '14 at 03:37

1 Answers1

1

I'm not sure why exactly you want to create a whole new panel with the exact same components, but I do see you've pretty much just copy and pasted the auto-generated code from the GUI Builder tool and tried to paste it into a method, to recreate the panel. I've never tried it myself, but it just looks wrong.

Without clarification of your requirements, I'd just suggest you use a CardLayout to switch between views. You could create a JPanel form with the builder tool and use the same form twice, once for each card. You can see more at How to Use CardLayout with NetBeans GUI Builder and see How to Use CardLayout Oracle tutorial for the basics of what CardLayout has to offer.

Or perhaps, reuse a JDialog to get user input for a Task.

Or maybe instead, a better option would be to have model objects that hold the state for each (Task, or whatever, I'm not sure). Say class Task that holds the state for fields and slider values. Or just use a JTable to hold the state for each task.

But really, I don't see the point of two identical panels. So please offer some more detail as to why you want two identical panels, and what different functionality do they provide, that you would need two.

Community
  • 1
  • 1
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720