Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / June 2004

Tip: Looking for answers? Try searching our database.

Extending JLabel to Render JButtons in Combobox

Thread view: 
Aldrin Piri - 10 Jun 2004 14:54 GMT
I'm trying to make a drop down menu of sorts by having buttons in a
combobox.  I got the buttons to render, but they don't seem to have
any button functionality.  I'm still relatively new to java and even
newer to swing, any help would be greatly appreciated.

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

public class CustomRendererMenu extends JPanel {
   ImageIcon[] images;
   String[] choiceStrings = {"Selected", "All", "Column", "Row"};

   public CustomRendererMenu() {
       super(new BorderLayout());
       Integer[] intArray = new Integer[choiceStrings.length];
       for (int i = 0; i < choiceStrings.length; i++) {
           intArray[i] = new Integer(i);
       }

       JComboBox buttonList = new JComboBox(intArray);
       ComboBoxRenderer renderer= new ComboBoxRenderer();
       renderer.setPreferredSize(new Dimension(50, 50));
       buttonList.setRenderer(renderer);

       add(buttonList, BorderLayout.PAGE_START);
       setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
   }

   private static void createAndShowGUI() {
       //Make sure we have nice window decorations.
       JFrame.setDefaultLookAndFeelDecorated(true);

       JFrame frame = new JFrame("Drop Down Menu Chooser");
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

       JComponent newContentPane = new CustomRendererMenu();
       newContentPane.setOpaque(true);
       frame.setContentPane(newContentPane);

       frame.pack();
       frame.setVisible(true);
   }

   public static void main(String[] args) {

       javax.swing.SwingUtilities.invokeLater(new Runnable() {
           public void run() {
               createAndShowGUI();
           }
       });
   }

   class ComboBoxRenderer extends JLabel
                          implements ListCellRenderer {

       public ComboBoxRenderer() {
           setOpaque(true);
       }

       public Component getListCellRendererComponent(
                                          JList list,
                                          Object value,
                                          int index,
                                          boolean isSelected,
                                          boolean cellHasFocus) {

           int selectedIndex = ((Integer)value).intValue();
           final String name = choiceStrings[selectedIndex];
           JButton choice = new JButton(name);

           choice.addActionListener(new ActionListener() {
               public void actionPerformed(ActionEvent e) {
                   System.out.println("The " + name + "has been pressed.");
               }
           });
           return choice;
       }
   }
}
Hal Rosser - 12 Jun 2004 02:09 GMT
why not make a pop up menu that shows up when you click the button.

> I'm trying to make a drop down menu of sorts by having buttons in a
> combobox.  I got the buttons to render, but they don't seem to have
[quoted text clipped - 76 lines]
>     }
> }


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.