jCOMBOBOX

Συζητήσεις για την Java

Συντονιστές: WebDev Moderators, Super-Moderators

Απάντηση
zihatzik
Δημοσιεύσεις: 1
Εγγραφή: 21 Μάιος 2009 16:24

jCOMBOBOX

Δημοσίευση από zihatzik » 21 Μάιος 2009 16:29

ΕΙΑ ΣΑΣ, ΕΧΩ ΦΤΟΙΑΞΕΙ ΕΝΑ JDIALOG ΚΑΙ ΚΑΙ ΘΑ ΗΘΕΛΑ ΝΑ ΠΡΟΣΘΕΣΩ ΕΝΑ ΚΑΙΝΟΥΡΓΙΟ jCOMBOBOX ΤΟ ΟΠΟΙΟ ΕΧΕΙ ΤΗΝ ΔΥΝΑΤΡΟΤΗΤΑ ΝΑ ΠΡΟΒΑΛΕΙ ΚΑΙ ΕΝΑ IMAGE ΔΙΠΛΑ ΣΕ ΚΑΘΕ ΕΠΙΛΟΓΗ. ΤΟ jCOMBOBOX ΠΟΥ ΚΑΤΕΒΑΣΑ ΕΙΝΑΙ ΑΥΤΟ: http://java.sun.com/docs/books/tutor.../combobox.html. ΛΟΙΠΟΝ , ΕΦΤΟΙΑΞΑ ΕΝΑ jCOMBOBOX = new demo();

ΕΚΑΝΑ ΤΗΝ ΚΛΑΣΗ demo() extends JComboBox εκανα την κλάση super();
ΑΛΛΑ ΜΟΥ ΠΕΤΑΕΙ Exception::?

Κώδικας: Επιλογή όλων

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

/*
* CustomComboBoxDemo.java is a 1.4 application that uses the following files:
* images/Bird.gif
* images/Cat.gif
* images/Dog.gif
* images/Rabbit.gif
* images/Pig.gif
*/
public class CustomComboBoxDemo extends JComboBox{
ImageIcon[] images;
String[] petStrings = {"Bird", "Cat", "Dog", "Rabbit", "Pig"};

/*
* Despite its use of EmptyBorder, this panel makes a fine content
* pane because the empty border just increases the panel's size
* and is "painted" on top of the panel's normal background. In
* other words, the JPanel fills its entire background if it's
* opaque (which it is by default); adding a border doesn't change
* that.
*/
public CustomComboBoxDemo() {
super();
setmodel(new.javax.DefaultComboBoxModel(petstrings ));

//Load the pet images and create an array of indexes.
images = new ImageIcon[petStrings.length];
Integer[] intArray = new Integer[petStrings.length];
for &#40;int i = 0; i < petStrings.length; i++&#41; &#123;
intArray&#91;i&#93; = new Integer&#40;i&#41;;
images&#91;i&#93; = createImageIcon&#40;"images/" + petStrings&#91;i&#93; + ".gif"&#41;;
if &#40;images&#91;i&#93; != null&#41; &#123;
images&#91;i&#93;.setDescription&#40;petStrings&#91;i&#93;&#41;;
&#125;
&#125;

//Create the combo box.
JComboBox petList = new JComboBox&#40;intArray&#41;;
ComboBoxRenderer renderer= new ComboBoxRenderer&#40;&#41;;
renderer.setPreferredSize&#40;new Dimension&#40;200, 130&#41;&#41;;
petList.setRenderer&#40;renderer&#41;;
petList.setMaximumRowCount&#40;3&#41;;


&#125;

/** Returns an ImageIcon, or null if the path was invalid. */
protected static ImageIcon createImageIcon&#40;String path&#41; &#123;
java.net.URL imgURL = CustomComboBoxDemo.class.getResource&#40;path&#41;;
if &#40;imgURL != null&#41; &#123;
return new ImageIcon&#40;imgURL&#41;;
&#125; else &#123;
System.err.println&#40;"Couldn't find file&#58; " + path&#41;;
return null;
&#125;
&#125;




class ComboBoxRenderer extends JLabel
implements ListCellRenderer &#123;
private Font uhOhFont;

public ComboBoxRenderer&#40;&#41; &#123;
setOpaque&#40;true&#41;;
setHorizontalAlignment&#40;CENTER&#41;;
setVerticalAlignment&#40;CENTER&#41;;
&#125;

/*
* This method finds the image and text corresponding
* to the selected value and returns the label, set up
* to display the text and image.
*/
public Component getListCellRendererComponent&#40;
JList list,
Object value,
int index,
boolean isSelected,
boolean cellHasFocus&#41; &#123;
//Get the selected index. &#40;The index param isn't
//always valid, so just use the value.&#41;
int selectedIndex = &#40;&#40;Integer&#41;value&#41;.intValue&#40;&#41;;//////ΕΔΩ ΜΟΥ ΧΤΥΠΑΕΙ

if &#40;isSelected&#41; &#123;
setBackground&#40;list.getSelectionBackground&#40;&#41;&#41;;
setForeground&#40;list.getSelectionForeground&#40;&#41;&#41;;
&#125; else &#123;
setBackground&#40;list.getBackground&#40;&#41;&#41;;
setForeground&#40;list.getForeground&#40;&#41;&#41;;
&#125;

//Set the icon and text. If icon was null, say so.
ImageIcon icon = images&#91;selectedIndex&#93;;
String pet = petStrings&#91;selectedIndex&#93;;
setIcon&#40;icon&#41;;
if &#40;icon != null&#41; &#123;
setText&#40;pet&#41;;
setFont&#40;list.getFont&#40;&#41;&#41;;
&#125; else &#123;
setUhOhText&#40;pet + " &#40;no image available&#41;",
list.getFont&#40;&#41;&#41;;
&#125;

return this;
&#125;

//Set the font and text when no image was found.
protected void setUhOhText&#40;String uhOhText, Font normalFont&#41; &#123;
if &#40;uhOhFont == null&#41; &#123; //lazily create this font
uhOhFont = normalFont.deriveFont&#40;Font.ITALIC&#41;;
&#125;
setFont&#40;uhOhFont&#41;;
setText&#40;uhOhText&#41;;
&#125;
&#125;
&#125;
ΚΑΜΙΑ ΒΟΗΘΕΙΑ ?? ΕΥΧΑΡΙΣΤΩ

Απάντηση

Επιστροφή στο “Java”

Μέλη σε σύνδεση

Μέλη σε αυτήν τη Δ. Συζήτηση: Δεν υπάρχουν εγγεγραμμένα μέλη και 0 επισκέπτες