2017-04-25 6 views
0

私はSwingを使ってJavaアプリケーションを作成しています。これにはJTextFieldとJComboBoxが含まれています。JComboBoxには、最後に入力された単語と、これらの単語が入力された時刻が含まれます。時間のある文字列は、最後に入力された単語(標準スタイル)とは別のサイズと色で表示されます(小さく、明るい灰色)。そのため、JComboBoxの各要素には2つの異なるスタイルを使用する必要があります。どうやってやるの?要素がそれぞれ異なる2つのスタイルを持つJComboBoxを作成するにはどうすればよいですか?

+0

[コンボボックスを使用する方法](https://docs.oracle.com/javase/tutorial/uiswing/components/combobox.html)、[カスタムレンダラの提供](https://docs.oracle.com/javase/tutorial/uiswing/components/ combobox.html#renderer) – MadProgrammer

答えて

0

簡単なアプローチは、それを達成するためのルックアンドフィールを選択することです。 もう一つの方法は、あなたがすべてのJComboBoxの要素のために必要な背景色と異なるのJLabelを作成して、それらを追加することができます:ここで

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

は一例です:

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>"; 
     } 
    } 
} 
関連する問題