Skip to content

Instantly share code, notes, and snippets.

/JPanelCombo

Created Aug 21, 2012
Embed
What would you like to do?
パネルJComboBoxを生成
import java.awt.FlowLayout;
import java.util.Iterator;
import java.util.List;
import javax.swing.JComponent;
import javax.swing.JPanel;
public class JPanelCombo extends JPanel{
JComboBox[] ComboBox ;
public JPanelPrueba()
{
this.setLayout(new BorderLayout());
}
public void addComboBoxs(String[] Label,List<String> items) {
JPanel panel_objetos = new JPanel();
JLabel[] etiqueta;
panel_objetos.setLayout(new FlowLayout(FlowLayout.LEFT));
panel_objetos.setOpaque(false);
//-------------------------------------/
ComboBox = new JComboBox[Label.length];
etiqueta=new JLabel[Label.length];
//---------------------------------/
Iterator it = items.iterator();
while(it.hasNext())
{
for(int e=0; e<Label.length; e++ ) {
ComboBox[e] = new JComboBox();
//addItems
ComboBox[e].addItem(it.next());
//*******************************/
etiqueta[e]=new JLabel(Label[e]);
panel_objetos.add(etiqueta[e]);
panel_objetos.add(ComboBox[e]);
}
}
//-------------------------------------/
this.add(panel_objetos,BorderLayout.CENTER);
}
}
public class prueba_form1 extends JFrame {
JPanelCombo panel;
public prueba_form1() {
super();
initialize();
}
/**
* This method initializes this
*
* @return void
*/
private void initialize() {
this.setSize(1050, 726);
this.setContentPane(getJContentPane());
this.setLocationRelativeTo(null);
}
private JPanel getPanelBOX() {
if (panel== null) {
panel= new JPanelCombo();
//--------------------------/
//0 //1 //2
String Label[]={"PELICULAS:","COSAS:","COLORES"};
List<String> list = new ArrayList<String>();
list.addAll(Arrays.asList("ACCION","TERROR","SUSPENSO","COMEDIA","DRAMA","EROTICA"));//0
list.addAll(Arrays.asList("RADIO","TTV","DVD","PC","CELULAR","COCINA"));//1
list.addAll(Arrays.asList("AMARILLO","ROJO","AZUL","CELESTE","MORADO","LILA"));//2
//------------------------------------
panel.addComboBoxs(Label,list);
//-------------------------------------
panel.ComboBox[0].addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
JComboBox c = (JComboBox)e.getSource();
System.out.println("ITEM" + c.getSelectedItem());
}
});
//----------------------------------------
panel.ComboBox[1].addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
JComboBox c = (JComboBox)e.getSource();
System.out.println("ITEM" + c.getSelectedItem());
}
});
//-------------------------------
panel.ComboBox[2].addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
JComboBox c = (JComboBox)e.getSource();
System.out.println("ITEM" + c.getSelectedItem());
}
});
//--------------------------
}
return panel;
}
private JPanel getJContentPane() {
if (jContentPane == null) {
jContentPane = new JPanel();
jContentPane.setLayout(new BorderLayout());
jContentPane.add(getPanelBOX(),BorderLayout.CENTER);
}
return jContentPane;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
SwingUtilities.invokeLater(new Runnable() {
public void run() {
try
{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
catch (Exception e)
{
e.printStackTrace();
}
prueba_form1 thisClass = new prueba_form1();
thisClass.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
thisClass.setVisible(true);
}
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment