This is my button click event. i want to create combo boxes each time i click the button.
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
jButton1.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent evt){
List<JComboBox> listOfComboBoxes = new ArrayList<JComboBox>();
for(int i=1;i<4;i++){
int x = 28;
int y = 100;
int a = 145;
int b = 28;
listOfComboBoxes.get(i);
listOfComboBoxes.get(i).addItem("--Select the Teacher--");
listOfComboBoxes.get(i).setLayout(null);
listOfComboBoxes.get(i).setLocation(x,y);
add(listOfComboBoxes.get(i)).setSize(a,b);
x=x+30;
a=a+40;
i++;
}
}
});
You are coping with two issues: one is adding combos (say to a JPanel), and the second is laying out those combos. Let's break it into two, and understand the adding mechanism first :
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.util.ArrayList;
import java.util.List;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class MakeCombos extends JFrame {
private static final int Number_OF_COMBOS = 4;
private JButton jButton1;
List<JComboBox> listOfComboBoxes;
private JPanel panel;
int counter = 0;
MakeCombos(){
super("Test frame");
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setPreferredSize(new Dimension(400,300));
setLocationRelativeTo(null);
getContentPane().setLayout(new BoxLayout(getContentPane(), BoxLayout.Y_AXIS));
//create a panel
panel = new JPanel();
getContentPane().add(panel);
//add button to it
jButton1 = new JButton("Click Me");
//add action listener to the panel
jButton1.addActionListener((ActionEvent e)-> {
jButton1ActionPerformed();
});
panel.add(jButton1, BorderLayout.CENTER);
validate();
pack();
setVisible(true);
listOfComboBoxes = makeCombos();
}
/**
*@return
*/
private List<JComboBox> makeCombos() {
List combos = new ArrayList<JComboBox>();
for(int i = 0; i < Number_OF_COMBOS; i++) {
JComboBox<String> combo = new JComboBox<>(new String[] {});
combos.add(combo);
}
return combos;
}
private void jButton1ActionPerformed() {
if(counter >= listOfComboBoxes.size()) {
return;
}
listOfComboBoxes.get(counter);
listOfComboBoxes.get(counter).addItem("--Select the Teacher--");
getContentPane().add(listOfComboBoxes.get(counter)) ;//.setSize(a,b);
counter++;
pack();
}
public static void main(String[] args) {
new MakeCombos();
}
}
Run it, and if it is not clear, don't hesitate to ask.
Now let's take it one step further and do the layout as well. Please see comments:
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class MakeCombos extends JFrame {
private static final int Number_OF_COMBOS = 4;
private List<JComboBox<String>> listOfComboBoxes;
private JPanel combosPanel;
private int xPosition = 28;
private int yPosition = 100;
private int width = 145;
private int height = 28;
private int counter = 0;
MakeCombos(){
//make width frame for testing
super("Test frame");
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setPreferredSize(new Dimension(500,400));
setLocationRelativeTo(null);
//set layout manager to the frame
getContentPane().setLayout(new BorderLayout());
//add panel + button to the frame
JPanel buttonPanel = new JPanel();
getContentPane().add(buttonPanel, BorderLayout.NORTH); //add the panel to the frame
//add button to the button panel
JButton jButton1 = new JButton("Click Me");
//add action listener to the button
jButton1.addActionListener((ActionEvent e)-> {
jButton1ActionPerformed();
});
buttonPanel.add(jButton1, BorderLayout.CENTER);
//--add width panel for the combos
combosPanel = new JPanel();
//set layout manager to null so you can layout each combo
//consider using a layout manager instead
combosPanel.setLayout(null);
getContentPane().add(combosPanel, BorderLayout.CENTER); //add the panel to the frame
validate();
pack();
setVisible(true);
//make the combos and add them to width list
listOfComboBoxes = makeCombos();
}
/**
*
*/
private List<JComboBox<String>> makeCombos() {
List<JComboBox<String>> combos = new ArrayList<JComboBox<String>> ();
for(int i = 0; i < Number_OF_COMBOS; i++) {
JComboBox<String> combo = new JComboBox<>(new String[] {});
combos.add(combo);
}
return combos;
}
private void jButton1ActionPerformed() {
if(counter >= listOfComboBoxes.size()) {
return;
}
//////////////////////////////////////////////////////////////////////////////
//////////////// note : all this block could be moved to MakeCombos() ////////
// (adding content and setting bounds could have been done when creating ////
//the combo
//add content to the combo
listOfComboBoxes.get(counter).addItem("--Select the Teacher--");
//set layout bounds to each combo
listOfComboBoxes.get(counter).setBounds(xPosition, yPosition, width, height);
//increment position
xPosition=xPosition+30;
yPosition=yPosition+40;
////////////////////////////////////////////////////////////////////////////
/////////////////////// end of move-to-makeCombos() block //////////////////
////////////////////////////////////////////////////////////////////////////
//add the combo to the combos panel
combosPanel.add(listOfComboBoxes.get(counter)) ;
//increment position and counter
counter++;
repaint();
pack();
}
public static void main(String[] args) {
new MakeCombos();
}
}
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.