Hello guys, I have built another small GUI:
c91be0d75e981f9b7ed81b89898a1722
Even before knowing whether the layout of the application I have built is right, I am facing another big problem. At compiling time I get the following error:

Note: Dropdown.java uses unchecked or unsafe operation
Note: Recompile with -Xlint:unchecked for details.

I had a look online and it seems that it has something to do with arrays but for the life of me I don't seem to be able to find the error. ANy idea?
Once that solved we can have a look at any possible error in the layout :-)

Here is the code:

/*Dropdown.java
ex 14.10 p 662.
-create 2 jPanels, one for tick boxes and one for buttons. The dropdown needs no panel. 
Border layout will position the elements in the middle
*/

import java.awt.FlowLayout;//arrange the components inside the panels
import java.awt.BorderLayout;//arrange thepanels containing the components
import javax.swing.JFrame;//provides basic window feature
import javax.swing.JPanel;//for the panels
import javax.swing.JButton;//for the buttons
import javax.swing.JCheckBox;//for the chekboxes
import javax.swing.JComboBox;//for dropdown

public class Dropdown extends JFrame{
    private JCheckBox background;
    private JCheckBox foreground;
    private JPanel comboBoxPanel; //panel holding the checkboxes 
    private JPanel checkboxesPanel; //panel holding the checkboxes 
    private JPanel buttonsPanel;//panel holding the buttons
    private JComboBox dropdown; //combobox
    private BorderLayout JframeLayout;//layout manager for checkboxes panel 
    private FlowLayout comboBoxLayout;//layout manager for combo box
    private FlowLayout checkboxesLayout;//layout manager for checkboxes
    private FlowLayout buttonsLayout;//layout manager for buttons
    private String[] values;//holds the string values of the combo box
    private JButton okButton;//holds the ok button
    private JButton cancelButton;//holds the cancel button

    public Dropdown(){
        super("Colour Select");
        JframeLayout = new BorderLayout( 0, 5);
        setLayout( JframeLayout );//set layout of jframe
        //combo box controller
        comboBoxPanel = new JPanel();
        comboBoxLayout = new FlowLayout();
        String[] values = {"Red", "Yellow", "Blue", "Magenta", "Green", "Orange", "Pink"};
        dropdown = new JComboBox( values );
        dropdown.setMaximumRowCount( 3 );//set maximum visible elements
        comboBoxPanel.setLayout( comboBoxLayout );//set the flow layout for the jpanel
        comboBoxPanel.add( dropdown );//add combo box to Jpanel

        //do I need to set the size of the combo box?

        // checkboxes controllers
        checkboxesPanel = new JPanel();
        checkboxesLayout = new FlowLayout();
        background = new JCheckBox( "Background" );
        foreground = new JCheckBox( "Foreground" );
        checkboxesPanel.setLayout( checkboxesLayout );//set layout of controllers in Jpanel
        checkboxesPanel.add( background );//add checkbox to JPanel
        checkboxesPanel.add( foreground );//add checkbox to JPanel

        //buttons controllers
        buttonsPanel = new JPanel();
        buttonsLayout = new FlowLayout();
        okButton = new JButton( "OK" );
        cancelButton = new JButton( "Cancel" );
        buttonsPanel.setLayout( buttonsLayout );//set layout of controllers in jpanel
        buttonsPanel.add( okButton );//add button to jpanel
        buttonsPanel.add( cancelButton );//add button to jpanel

        //add panels to JFrame
        add( comboBoxPanel, BorderLayout.PAGE_START );//add the combobox panel at the top of the JFrame
        add( checkboxesPanel, BorderLayout.CENTER );// panel at teh centre of the page
        add( buttonsPanel, BorderLayout.CENTER );//add panel at teh bottom
    }//end of constructor


}//end of Dropdown class

And the test class

/*
testing class Dropdown.
DropdownTest.java
*/
import javax.swing.JFrame;
import java.awt.Dimension;//specify the dimensions
public class DropdownTest{
    public static void main( String[] args ){
        Dropdown dropdownTest = new Dropdown();
        dropdownTest.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
        dropdownTest.setVisible( true );

        Dimension minimumSize = new Dimension( 400, 200 );//specify dimensions
        dropdownTest.setMinimumSize( minimumSize );//set dimensions
        dropdownTest.setResizable( false );
        dropdownTest.pack();
    }//end of main
}//end of class

Recommended Answers

All 11 Replies

When it said "Recompile with -Xlint:unchecked for details." that wasn't just to fill in a bit of spare time"
Do what it says and you will get a proper error message with details of the problem and the exact place where it was found.

Sorry I didn't realize it was telling me to do it again with the option. Here it is:

C:\Users\bah\JAVA\GUI\14.10>javac *.java -Xlint:unchecked
Dropdown.java:38: warning: [unchecked] unchecked call to JComboBox(E[]) as a member of the raw type JComboBox
                dropdown = new JComboBox( values );
                           ^
  where E is a type-variable:
    E extends Object declared in class JComboBox
1 warning

OK
With Java 1.5 we got type variables, and these were retro-fitted into the existing classes. In the case of JComboBox this was enhanced to JComboBox<E> so you could specify the type(s) of object that could go in the combo box, and the compiler could (1) check that you were using the right kind of data and (2) use the selected value without having to cast it with an unchecked cast.

You can still use JComboBox without specifyinga type, (that's called using a "raw type"), but it generates a warning that you should specify a type, as in
JComboBox<String> jcb = new JComboBox<>(someStringDataValues);

Uhm, very odd the book I took the exercise from doesn't mention anything at all on the subject.
But surely the compiler should give you the possibility to ignore such a warning and go on regardless!?
Anyway, I have amended it to this:

private JComboBox<String> dropdown; //combobox
dropdown = new JComboBox<>( values );

and the compiler doesn't complain anymore.
So, the layout, here it is
b2e4eb30857d113b5227222c0c585bd3
It looks a bit awful I must say. A couple of things I would like to ask:
My combo box doesn't have any width applied to it. To get the combo box to extend all the way across the window, do I have to create a renderer?
Then somehow the buttons are really far at the bottom. Was my choice of layout managers really wrong? I have used a borderLayout to stack the panels containing each groups of controllers ont he top of one another and I used a flowLayout for each controller pairs, including the combobox.

Maybe your book was written before Java 1.5? Lots were!
It is only a warning (a "note"). Your code has been compiled sucessfully, so you can ignore it and run the program.

The width of the combo box has been determined by its contents - it's just wide enough to hold the longest String in its values, which is perfectly sensible for this layout.

Was my choice of layout managers really wrong?

It makes perfect sense to try the simplest/default layout managers first. For most if us it's a bit of a mystery exactly how each manager will work, whether it will honour any or all of your setMin, Max, Preferred sizes, how it will move stuff when the user resizes the window... you just have to try it and see.

But if control of the layout is important to you... (you know how I would finish that sentence!)

Thanks JamesCherrill. I think the book has been written long after JAVA 1.5, it's deitel and deitel 9th edition.
Yes, I noticed that the width of the combo box was determined by the longest element when I clicked on it :-)

And yes, by now I know very well how you would finish that sentence :-)! There is also another reason why I am not using GridBagLayout: it's because the book I am taking the exercises from doesn't mention GridBagLayout and therefore it assumes that the exercise can be completed with other layouts :-), that's why I was wondering whether my choice of layout manager was wrong.
I came across this render thing on the net, so I thought that perhaps it will help me to set the width of the combo box (although as you said and I agree with that, it makes perfect sense to have it as long as the longest string, but still I can see that in the exercise it is as long as the whole box, so I thought I'd attempt to do that too)

I'm perfectly sure you can do this without a custom renderer. You use those to control how the "inside" of the component is painted, not how they get placed in the layout.

It's odd that BorderLayout hasn't put your combo box all the way across...

aha - just noticed you added the combo box to a completely unnecessary (indeed, disasterous!) panel. The combo box gets in size from its contents, the panel gets it size from the combo box, but then when it's added to the border layout the panel is resized to full width, and the panel then centers the combo box.
You only need a panel if you want to group 2 or more components. A panel with just one component in it is at best useless and at worst disasterous - as in this case. Just get rid of it!

(Didn't we cover unnecessary panels in your previous exercise?)

oh I see, sorry I will get rid of it then. Well, in the previous exercise you said that one of the panels wasn't necessary because it had only one element, but you never mentioned that it could have been a disaster to add it in :-)! Lesson learned, next time I will use it to only group 2 or more elements. So essentially, when there is only one element we can add it directly to the jframe, is that what we are saying?

Yes
("Disaster" was a but strong, but it does change the default layout, so you do get different results with or without the panel)

  • override setPrototypeDisplayValue("long String value") for JComboBox, put this JComboBox to the JFrame.NORTH

  • use JPanel (default LayoutManager is FlowLayout.CENTER) put rest of JComponents to this JPanel (JFrame.CENTER)

  • print screen talking about JFrame (GridLayout with three rows, because there is crazy gap), JComboBox and two separates JPanels contains JComponents (default FlowLayout.CENTER), but is possible wiht one JPanel (have to play with JComboBox)

right thanks guys. @JamesCherrill: offending panel removed, much better now thanks :-)
@mKorbel ok thanks, didn't occur to me I could use only one jpanel :-)!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.