2017-04-25 2 views
0

Je crée une application Java en utilisant Swing. Il contient un JTextField avec un JComboBox qui contient les derniers mots entrés avec l'heure à laquelle ces mots ont été entrés. La chaîne avec l'heure doit être dans une autre taille et couleur (plus petite et en gris clair) que les derniers mots entrés (style standard). Donc, dans chaque élément de la JComboBox devrait être utilisé deux styles différents. Comment puis je faire ça?Comment créer un JComboBox dont les éléments ont chacun deux styles différents?

+0

[Comment utiliser des boîtes combo ] (https://docs.oracle.com/javase/tutorial/uiswing/components/combobox.html), [Fournir un moteur de rendu personnalisé] (https://docs.oracle.com/javase/tutorial/uiswing/components/ combobox.html # renderer) – MadProgrammer

Répondre

0

Une approche facile est de choisir une apparence et une sensation pour permettre d'y parvenir. Une autre façon peut être de créer un autre JLabel avec la couleur d'arrière-plan que vous avez besoin pour chaque élément JComboBox et les ajouter avec:

JLabel label = new JLabel("Some text"); 
label.setBackground(Color.grey); 
label.setOpaque(true); 

jComboBox.addItem(label); 

JLabel anotherLabel = new JLabel("Another text"); 
anotherLabel.setBackground(Color.white); 
anotherLabel.setOpaque(true); 

jComboBox.addItem(anotherLabel); 
1

Voici un exemple:

import java.awt.BorderLayout; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.text.DateFormat; 
import java.util.Date; 

import javax.swing.JComboBox; 
import javax.swing.JFrame; 
import javax.swing.JTextField; 
import javax.swing.SwingUtilities; 
import javax.swing.WindowConstants; 

public class ComboSample implements Runnable { 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(new ComboSample()); 
    } 

    @Override 
    public void run() { 
     JFrame frm = new JFrame("Combo example"); 
     final JTextField fld = new JTextField(20); 
     TextData[] data = new TextData[]{new TextData("First", new Date(System.currentTimeMillis() - 100000)), 
       new TextData("Second", new Date(System.currentTimeMillis() - 200000)), 
       new TextData("Third", new Date(System.currentTimeMillis() - 300000)), 
       new TextData("Fourth", new Date(System.currentTimeMillis() - 400000))}; 
     JComboBox<TextData> cb = new JComboBox<>(data); 
     cb.setSelectedItem(null); 
     cb.addActionListener(new ActionListener() { 

      @Override 
      public void actionPerformed(ActionEvent e) { 
       JComboBox cb = (JComboBox) e.getSource(); 
       TextData td = (TextData) cb.getSelectedItem(); 
       if (td != null) { 
        fld.setText(td.getText()); 
       } 
      } 
     }); 
     frm.add(fld); 
     frm.add(cb, BorderLayout.EAST); 
     frm.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); 
     frm.pack(); 
     frm.setLocationRelativeTo(null); 
     frm.setVisible(true); 
    } 

    private static class TextData { 
     private static final DateFormat FORMAT = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM); 
     private final String text; 
     private final Date timestamp; 
     public TextData(String text, Date timestamp) { 
      this.text = text; 
      this.timestamp = timestamp; 
     } 
     public String getText() { 
      return text; 
     } 
     @Override 
     public String toString() { 
      return "<html>" + text + " <span style=\"color:#D3D3D3\"><i>" + FORMAT.format(timestamp) + "</i></span></html>"; 
     } 
    } 
}