简体   繁体   中英

Creating a new identical jPanel when clicking a button that is independent from the rest of the jPanels

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))
    );
}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM