Está en la página 1de 3

APUNTES EXAMEN SWING

Repintar un panel:
SwingUtilities.updateComponentTreeUI(nombre_panel);

Jspinner:
Jspinner jEdad = new JSpinner();
jEdad.setModel(new SpinnerNumberModel(18(default),1(min),99(max),1(salto)));

RadioButton:
ButtonGroup grupo = new ButtonGroup();
JRadioButton radio1 = new JRadioButton("Radio1");
grupo.add(radio1);
boton1.setActionCommand(“Radio1");
contentPane.add(radio1);
JRadioButton radio2 = new JRadioButton("Radio2");
grupo.add(radio2);
boton1.setActionCommand(“Radio2");
contentPane.add(radio2);
String seleccionado=grupo.getSelection().getActionCommand();

Combobox a partir de otro:


String [] bebida = {“Agua“,”Vino”};
String [] aguas = {“Sin gas”, “Con gas”, “Del grifo”};
String [] vinos={“Tinto“,”Rosado”, “Blanco”};
JComboBox cBebida = new JComboBox(bebida);
JComboBox cTipoBebida = new JComboBox();
int b=cBebida.getSelectedIndex();
String[] opcion={""};
if (b==0){ opcion = aguas; }
if (b==1){ opcion= vinos; }
cTipoBebida.removeAllItems();
cTipoBebida.setEnabled(true);
cTipoBebida.setModel(new DefaultComboBoxModel(opcion));

JFILECHOOSER
JFileChooser explorador = new JFileChooser("");
explorador.setDialogTitle("Escoge el fichero...");
int seleccion = explorador.showSaveDialog(null);
switch(seleccion){
case JFileChooser.APPROVE_OPTION:
File archivo =explorador.getSelectedFile(); //nombre del archivo
try{ //Escribe en el archivo
FileWriter writer =new FileWriter(archivo);
String cadena=“Estoy escribiendo en el fichero”;
writer.write(cadena);
writer.close();
}catch(IOException ex){
ex.printStackTrace();
}
break;
case JFileChooser.CANCEL_OPTION:
break;
case JFileChooser.ERROR_OPTION:
System.out.println("Se ha producido un error");
break;
}
APUNTES EXAMEN SWING
Mostrar etiquetas en JSlider:
Dictionary mitabla=new Hashtable();
mitabla.put(new Integer(0), new JLabel(" 0%"));
mitabla.put(new Integer(25), new JLabel(" 25%"));
mitabla.put(new Integer(50), new JLabel(" El medio (50%)"));
mitabla.put(new Integer(75), new JLabel(" 75%"));
mitabla.put(new Integer(100), new JLabel(" 100%"));
slider.setLabelTable(mitabla); // asociamos el slider con la tabla hash
slider.setPaintLabels(true); //muestra las etiquetas numéricas

Crear icono:
ImageIcon icon = new ImageIcon(getClass().getResource("ok-peq.jpg"));

Poner título a un Panel:


PanelTexto.setBorder(BorderFactory.createTitledBorder("Escriba un nombre"));

JLIST:
Jlist <String> listaNombres = new Jlist <String>();
DefaultListModel modelo = new DefaultListModel <String>();
listaNombres.setModel(modelo);

Class DefaultListModel<E>
APUNTES EXAMEN SWING

También podría gustarte