How To Color ComboBox and All of The Header of a Table

Discussion in 'Java' started by clusardi2k@aol.com, Aug 24, 2012.

  1. Guest

    //(1) For the below project, how do I color an entire combobox. Tthat's the
    //entire visible part of it.
    //(2) How do I color the complete header of a table. I want to leave the
    //scrollbar code in the project as is!

    //Thank you,

    package color_combobox;

    import java.awt.Color;
    import javax.swing.JScrollPane;
    import javax.swing.table.JTableHeader;

    public class Color_ComboBox extends javax.swing.JFrame
    {

    public Color_ComboBox()
    {
    initComponents();

    jTable1.setBackground(Color.BLUE);
    jTable1.setForeground(Color.WHITE);

    JTableHeader header = jTable1.getTableHeader();
    header.setOpaque(false);
    header.setBackground(Color.YELLOW);
    header.setForeground(Color.BLACK);
    jTable1.setTableHeader(header);

    jScrollPane1.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_NEVER);
    jScrollPane1.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
    }


    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

    jComboBox1 = new javax.swing.JComboBox();
    jScrollPane1 = new javax.swing.JScrollPane();
    jTable1 = new javax.swing.JTable();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    setBackground(new java.awt.Color(51, 255, 0));
    setForeground(new java.awt.Color(51, 255, 0));

    jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

    jTable1.setModel(new javax.swing.table.DefaultTableModel(
    new Object [][] {
    {null, null, null, null},
    {null, null, null, null},
    {null, null, null, null},
    {null, null, null, null}
    },
    new String [] {
    "Title 1", "Title 2", "Title 3", "Title 4"
    }
    ));
    jScrollPane1.setViewportView(jTable1);

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
    layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
    .addGroup(layout.createSequentialGroup()
    .addContainerGap()
    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
    .addGroup(layout.createSequentialGroup()
    .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
    .addGap(0, 0, Short.MAX_VALUE))
    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE))
    .addContainerGap())
    );
    layout.setVerticalGroup(
    layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
    .addGroup(layout.createSequentialGroup()
    .addContainerGap()
    .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
    .addGap(75, 75, 75)
    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 88, javax.swing.GroupLayout.PREFERRED_SIZE)
    .addContainerGap(129, Short.MAX_VALUE))
    );

    pack();
    }// </editor-fold>

    public static void main(String args[])
    {

    //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
    /*
    * If Nimbus (introduced in Java SE 6) is not available, stay with the
    * default look and feel. For details see
    * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
    */
    try {
    for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
    if ("Nimbus".equals(info.getName())) {
    javax.swing.UIManager.setLookAndFeel(info.getClassName());
    break;
    }
    }
    } catch (ClassNotFoundException ex) {
    java.util.logging.Logger.getLogger(Color_ComboBox.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (InstantiationException ex) {
    java.util.logging.Logger.getLogger(Color_ComboBox.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (IllegalAccessException ex) {
    java.util.logging.Logger.getLogger(Color_ComboBox.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (javax.swing.UnsupportedLookAndFeelException ex) {
    java.util.logging.Logger.getLogger(Color_ComboBox.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    }
    //</editor-fold>

    java.awt.EventQueue.invokeLater(new Runnable()
    {

    public void run()
    {
    Color_ComboBox box = new Color_ComboBox();
    box.setVisible(true);
    box.setBackground(Color.yellow);
    box.setForeground(Color.red);
    }
    });
    }

    // Variables declaration - do not modify
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    // End of variables declaration
    }
     
    , Aug 24, 2012
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. -
    Replies:
    14
    Views:
    606
    Chris Uppal
    Mar 26, 2005
  2. mlt
    Replies:
    2
    Views:
    907
    Jean-Marc Bourguet
    Jan 31, 2009
  3. Kamaljeet Saini
    Replies:
    0
    Views:
    483
    Kamaljeet Saini
    Feb 13, 2009
  4. Replies:
    7
    Views:
    410
    Mick White
    Feb 2, 2005
  5. Replies:
    5
    Views:
    508
    Roedy Green
    Oct 11, 2012
Loading...

Share This Page