java left logo
java middle logo
java right logo
 

Home arrow Java SE Tips
 
 
Main Menu
Home
Java Tutorials
Book Reviews
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Java Network
Java Forums
Java Blog




Most Visited Tips
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Book Reviews
Top Rated Tips
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Book Reviews


Statistics
Registered Users: 4093
Java SE Tips: 614
Java ME Tips: 202
Java EE Tips: 183
Other API Tips: 779
Java Applications: 298
Java Libraries: 209
Java Games: 16
Book Reviews:
 
 
 
How to provide user with an option of choosing a color from a pulldown E-mail
User Rating: / 1
PoorBest 

This java tip illustrates a method of providing user with an option of choosing a color from a pulldown. The tip includes a table that allows the user to pick a color from a pulldown list. This is accomplished by using the DefaultCellRenderer and DefaultCellEditor classes.


Image

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

public class ColorTable extends JFrame {
  ColorName colors[] {
    new ColorName("Red")new ColorName("Green")new ColorName("Blue"),
    new ColorName("Black")new ColorName("White")
  };

  public ColorTable() {
    super("Table With DefaultCellEditor Example");
    setSize(500,300);
    setDefaultCloseOperation(EXIT_ON_CLOSE);

    JTable table = new JTable(new AbstractTableModel() {
        ColorName data[] {
          colors[0], colors[1], colors[2], colors[3], colors[4],
          colors[0], colors[1], colors[2], colors[3], colors[4]
        };
        public int getColumnCount() { return 3}
        public int getRowCount() { return 10;  }
        public Object getValueAt(int r, int c) {
          switch (c) {
          case 0:  return (r + 1".";
          case 1:  return "Some pithy quote #" + r;
          case 2:  return data[r];
          }
          return "Bad Column";
        }
        public Class getColumnClass(int c) {
          if (c == 2return ColorName.class;
          return String.class;
        }
        // Make Column 2 editable...
        public boolean isCellEditable(int r, int c) {
          return c == 2;
        }
        public void setValueAt(Object value, int r, int c) {
          data[r(ColorName)value;
        }
      });

    table.setDefaultEditor(ColorName.class,
                           new DefaultCellEditor(new JComboBox(colors)));
    table.setDefaultRenderer(ColorName.class,
                             new DefaultTableCellRenderer());
    table.setRowHeight(20);
    getContentPane().add(new JScrollPane(table));
  }

  public static void main(String args[]) {
    ColorTable ex = new ColorTable();
    ex.setVisible(true);
  }

  public class ColorName {
    String cname;
    public ColorName(String name) { cname = name; }
    public String toString() { return cname; }
  }
}

 Related Tips

 
< Prev   Next >

Page 1 of 0 ( 0 comments )

You can share your information about this topic using the form below!

Please do not post your questions with this form! Thanks.


Name (required)


E-Mail (required)

Your email will not be displayed on the site - only to our administrator
Homepage(optional)



Comment Enable HTML code : Yes No



 
       
         
     
 
 
 
   
 
 
java bottom left
java bottom middle
java bottom right
RSS 0.91 FeedRSS 1.0 FeedRSS 2.0 FeedATOM FeedOPML Feed

Home - About Us - Privacy Policy
Copyright 2005 - 2008 www.java-tips.org
Java is a trademark of Sun Microsystems, Inc.