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.

Rendering JButton in JCombobox problems...

Thread view: 
Aldrin Piri - 10 Jun 2004 15:03 GMT
I'm relatively new to java and even newer to swinb, but I'm trying to
make a drop down menu of sorts using a JCombobox using JButtons.  I
got my Buttons to render correctly, but they seem to have lost their
button capabilities.  What class must I extend, and how must I do it
so that they regain their normal Component functionality.

import 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:06 GMT
myButton.addActionListener(componentYouWantToListenForTheClick);

> I'm relatively new to java and even newer to swinb, but I'm trying to
> make a drop down menu of sorts using a JCombobox using JButtons.  I
[quoted text clipped - 77 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.