I am trying to create a dynamic Swing GUI. I need to add/delete JPanels
when I click on add/delete button. I am not able to add JPanels
dynamically. Initially JPanel
loads but the array of JPanels
fail to work. How do I do it?
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.print.attribute.standard.JobHoldUntil;
import javax.swing.*;
public class AccessoryFileChooser2 extends JFrame {
JFileChooser chooser = null;
JLabel statusbar;
JLabel file;
JCheckBox checkBox;
int count;
int increment;
JPanel [] panel = new JPanel[10]; //array
public AccessoryFileChooser2() {
setSize(350, 200);
count=0;
increment=1;
setDefaultCloseOperation(EXIT_ON_CLOSE);
final Container c = getContentPane();
c.setLayout(new BorderLayout());
checkBox =new JCheckBox("");
String fileName = "Choose File Name";
file = new JLabel(fileName);
final JButton accButton = new JButton("Browse");
final JButton add = new JButton("Add");
final JButton validate = new JButton("Validate");
final JButton delete = new JButton("Delete");
accButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
int option = chooser.showOpenDialog(AccessoryFileChooser2.this);
if (option == JFileChooser.APPROVE_OPTION) {
statusbar.setText(
(
chooser.getSelectedFile().getPath()));
}
}
});
statusbar = new JLabel("Output of your selection will go here");
chooser = new JFileChooser();
final JPanel panels =new JPanel();
//JPanel panel2 =new JPanel();
panel[count]=new JPanel();
panel[count].add(checkBox);
panel[count].add(file);
panel[count].add(accButton );
panel[count].add(statusbar);
c.add(panel[count],BorderLayout.CENTER);
panels.add(add);
panels.add(delete);
panels.add(validate);
add.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
count=count+1;;
increment=increment+1;;
panel[count]=new JPanel();
System.out.println("You clicked the ADD button");
panel[count].add(checkBox);
panel[count].add(file);
panel[count].add(accButton );
开发者_高级运维panel[count].add(statusbar);
panel[count].revalidate();
panel[count].repaint();
panel[count].updateUI();
c.add(panel[count]);
}
});
delete.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
increment--;
System.out.println("You clicked the Delete button");
System.out.println(checkBox.isSelected());
for (int i = 0; i < panel.length; i++) {
JCheckBox box=(JCheckBox) panel[i].getComponent(0);
if(box.isSelected()){
c.remove(panel[i]);
}
}
}
});
validate.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
System.out.println("You clicked the Validate button");
}
});
c.add(panels,BorderLayout.SOUTH);
}
public static void main(String args[]) {
AccessoryFileChooser2 afc = new AccessoryFileChooser2();
afc.setVisible(true);
}
}
not that not correct, there are lots of newbie mistakes,
1/ because same way as you created array for JPanels
JPanel [] panel = new JPanel[10];
you need to create Array
for its JComponents
, because one JComponent could be added only once time
EDIT: partial amended/changed code diplayed this issue
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.LineBorder;
public class AccessoryFileChooser2 extends JFrame {
private static final long serialVersionUID = 1L;
private JFileChooser chooser = null;
private JLabel statusbar;
private JLabel file;
private JCheckBox checkBox;
private int count;
private int increment = 10;
private JPanel parentPanel = new JPanel();
private JPanel panels = new JPanel();
private JPanel[] panel = new JPanel[increment]; //array
private JButton accButton = new JButton("Browse");
private JButton addButton = new JButton("Add");
private JButton validate = new JButton("Validate");
private JButton delete = new JButton("Delete");
public AccessoryFileChooser2() {
count = 0;
parentPanel.setLayout(new GridLayout(10, 1, 10, 10));
checkBox = new JCheckBox("");
String fileName = "Choose File Name";
file = new JLabel(fileName);
accButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
int option = chooser.showOpenDialog(AccessoryFileChooser2.this);
if (option == JFileChooser.APPROVE_OPTION) {
statusbar.setText((chooser.getSelectedFile().getPath()));
}
}
});
statusbar = new JLabel("Output of your selection will go here");
chooser = new JFileChooser();
panel[count] = new JPanel();
panel[count].add(checkBox);
panel[count].add(file);
panel[count].add(accButton);
panel[count].add(statusbar);
panel[count].setPreferredSize(new Dimension(450, 40));
panel[count].setBorder(new LineBorder(Color.BLACK, 1));
parentPanel.add(panel[count]);
addButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (count < increment - 1) {
count += 1;
panel[count] = new JPanel();
System.out.println("You clicked the ADD button");
panel[count].add(checkBox);
panel[count].add(file);
panel[count].add(accButton);
panel[count].add(statusbar);
panel[count].revalidate();
panel[count].repaint();
//panel[count].updateUI();
parentPanel.add(panel[count]);
parentPanel.revalidate();
parentPanel.repaint();
pack();
if (count == increment - 1) {
addButton.setEnabled(false);
}
}
}
});
delete.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("You clicked the Delete button");
System.out.println(checkBox.isSelected());
/*for (int i = 0; i < parentPanel.getComponentCount(); i++) {
JCheckBox box = (JCheckBox) panel[i].getComponent(0);
if (box.isSelected()) {
parentPanel.remove(panel[i]);
}
}*/
}
});
validate.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("You clicked the Validate button");
}
});
panels.setLayout(new GridLayout(1, 3, 10, 10));
panels.add(addButton);
panels.add(delete);
panels.add(validate);
add(parentPanel, BorderLayout.CENTER);
add(panels, BorderLayout.SOUTH);
setDefaultCloseOperation(EXIT_ON_CLOSE);
pack();
setVisible(true);
}
public static void main(String args[]) {
AccessoryFileChooser2 afc = new AccessoryFileChooser2();
}
}
with this output to the GUI
2/ and so on ...
don't do it this way, there is possible a lots of issues with performance, if you add and remove JPanel[]
lots of times ..., GUI would be freeze or will be un-responsive
it would be better to look for JTable
with one TableColumn
as is suggested here
When adding or removing components from a visible GUI the general code should be:
panel.add(...);
panel.revalidate();
panel.repaint();
The revalidate basically invoke the layout manager.
The repaint makes sure the components are repainted in case the layout has changed.
Add c.validate()
after c.add(panel[count]);
to update the GUI.
It is also recommended to check the current number of JPanel
s, because this way, you will get ArrayIndexOutOfBoundsException
pretty fast...
I have had success with:
repaint();
validate();
after making changes to the gui at run time. When I create, remove, move panels I call these two methods on the component/container.
精彩评论