Results 1 to 13 of 13
  1. #1
    jon80's Avatar
    jon80 is offline Senior Member
    Join Date
    Feb 2008
    Location
    Malta (EU)
    Posts
    211
    Rep Power
    7

    Default [newbie] Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException

    I'm trying to read images from my project directory, however, they seem to return a null.

    Some of the sliders are not showing up within the panel; only the first two are showing up!

    What does slider.setPaintTrack(false); do because I haven't noticed any visual impact?

    :confused:

    Java Code:
    [B]SliderTestFrame.java[/B]
    package homenetwork.bkr.training;
    
    import java.awt.*;
    import java.io.IOException;
    import java.util.*;
    import javax.swing.*;
    import javax.swing.event.*;
    import javax.imageio.*;
    
    public class SliderTestFrame extends JFrame {
    
    	public SliderTestFrame()
    	{
    		setTitle("Slider Test");
    		setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
    		
    		sliderPanel = new JPanel();
    		sliderPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
    		
    		//common listener for all sliders
    		listener = new ChangeListener()
    		{
    			public void stateChanged(ChangeEvent event)
    			{
    				//update text filed when the slider value changes
    				JSlider source = (JSlider) event.getSource();
    				textField.setText("" + source.getValue());
    			}
    		};
    		
    		//add a plain slider
    		JSlider slider = new JSlider();
    		addSlider(slider, "Plain");
    		
    		//add a slider with major and minor ticks
    		slider = new JSlider();
    		slider.setPaintTicks(true);
    		slider.setMajorTickSpacing(20);
    		slider.setMinorTickSpacing(5);
    		addSlider(slider, "Ticks");
    		
    		//add a slider that snaps to ticks  DOES NOT SHOW UP ON PANEL
    		slider = new JSlider();
    		slider.setPaintTicks(true);
    		slider.setMajorTickSpacing(20);
    		slider.setMinorTickSpacing(5);
    		slider.setPaintTrack(false); //TODO what is this exactly?
    		addSlider(slider, "Snap to ticks");
    		
    		//add a slider with no track DOES NOT SHOW UP ON PANEL
    		slider = new JSlider();
    		slider.setPaintTicks(true);
    		slider.setMajorTickSpacing(20);
    		slider.setMinorTickSpacing(5);
    		slider.setPaintTrack(false); 
    		addSlider(slider, "No track");
    		
    		[B]//add an inverted slider DOES NOT SHOW UP ON PANEL[/B]
    		slider = new JSlider();
    		slider.setPaintTicks(true);
    		slider.setMajorTickSpacing(20);
    		slider.setMinorTickSpacing(5);
    		slider.setInverted(true); 
    		addSlider(slider, "Inverted");
    		
    		[B]//add a slider with numeric labels DOES NOT SHOW UP ON PANEL[/B]
    		slider = new JSlider();
    		slider.setPaintTicks(true);
    		slider.setPaintLabels(true);
    		slider.setMajorTickSpacing(20);
    		slider.setMinorTickSpacing(5);
    		addSlider(slider, "Numeric labels");
    		
    [B]		//add a slider with alphabetic labels DOES NOT SHOW UP ON PANEL[/B]
    		slider = new JSlider();
    		slider.setPaintLabels(true);
    		slider.setPaintTicks(true);
    		slider.setMajorTickSpacing(20);
    		slider.setMinorTickSpacing(5);
    		
    		Dictionary<Integer, Component> labelTable = new Hashtable<Integer, Component>();
    		labelTable.put(0, new JLabel("A"));
    		labelTable.put(20, new JLabel("B"));
    		labelTable.put(40, new JLabel("C"));
    		labelTable.put(60, new JLabel("D"));
    		labelTable.put(80, new JLabel("E"));
    		labelTable.put(100, new JLabel("F"));
    		
    		slider.setLabelTable(labelTable);
    		addSlider(slider, "Custom labels");
    		
    		//add a slider with icon labels
    		slider = new JSlider();
    		slider.setPaintTicks(true);
    		slider.setPaintLabels(true);
    		slider.setSnapToTicks(true);
    		slider.setMajorTickSpacing(20);
    		slider.setMinorTickSpacing(20);
    		
    		labelTable = new Hashtable<Integer, Component>();
    		
    		//add card images
    		[B]try {
    		labelTable.put(0, new JLabel(new ImageIcon(ImageIO.read(getClass().getResource("images/nine.png")))));
    		labelTable.put(20, new JLabel(new ImageIcon(ImageIO.read(getClass().getResource("images/ten.png")))));
    		labelTable.put(40, new JLabel(new ImageIcon(ImageIO.read(getClass().getResource("images/jack.png")))));
    		labelTable.put(60, new JLabel(new ImageIcon(ImageIO.read(getClass().getResource("images/queen.png")))));
    		labelTable.put(80, new JLabel(new ImageIcon(ImageIO.read(getClass().getResource("images/king.png")))));
    		labelTable.put(100, new JLabel(new ImageIcon(ImageIO.read(getClass().getResource("images/ace.png"))))); 
    [/B]
    		
    		if (debug)
    		{
    			System.out.println("DEBUG: " + labelTable.get(0));
    			System.out.println("DEBUG: " + labelTable.get(20));
    			System.out.println("DEBUG: " + labelTable.get(40));
    			System.out.println("DEBUG: " + labelTable.get(60));
    			System.out.println("DEBUG: " + labelTable.get(80));
    			System.out.println("DEBUG: " + labelTable.get(100));
    		}
    		
    		} catch (IOException e) {
    			
    			e.printStackTrace();
    		}
    		slider.setLabelTable(labelTable);
    		addSlider(slider, "Icon labels");
    		
    		
    		//add the text field that displays the slider value
    		textField = new JTextField();
    		add(sliderPanel, BorderLayout.CENTER);
    		add(sliderPanel, BorderLayout.SOUTH);
    		
    	}
    	
    	/**
    	 * Add a slider to the slider panel and hooks up the listener.
    	 * @param s: the slider
    	 * @param description: the slider description
    	 */
    	public void addSlider(JSlider s, String description)
    	{
    		s.addChangeListener(listener);
    		JPanel panel = new JPanel();
    		panel.add(s);
    		panel.add(new JLabel(description));
    		sliderPanel.add(panel);
    	}
    	
    	public static final int DEFAULT_WIDTH = 350;
    	public static final int DEFAULT_HEIGHT = 450;
    	
    	private JPanel sliderPanel;
    	private JTextField textField;
    	private ChangeListener listener;
    	
    	private boolean debug = true;
    }
    
    [B]SliderTest.java[/B]
    package homenetwork.bkr.training;
    
    import java.awt.*;
    import javax.swing.*;
    
    public class SliderTest {
    
    	public static void main(String[] args) {
    		EventQueue.invokeLater(new Runnable()
    		{	
    			public void run()
    			{
    				SliderTestFrame frame = new SliderTestFrame();
    				frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    				frame.setVisible(true);
    			}
    		});
    
    	}
    
    }
    Output:
    Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: input == null!
    at javax.imageio.ImageIO.read(Unknown Source)
    at homenetwork.bkr.training.SliderTestFrame.<init>(Sl iderTestFrame.java:104)
    at homenetwork.bkr.training.SliderTest$1.run(SliderTe st.java:13)
    at java.awt.event.InvocationEvent.dispatch(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilter s(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(U nknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)

    Screen looks like this.

  2. #2
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    1) You need to call validate() after constructing your GUI
    2) Your getResource() calls are returning null; your paths or classpath are wrong
    Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
    How To Ask Questions The Smart Way

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    You're getting messed up by wrong layouts and by adding at least one component twice:
    Java Code:
        textField = new JTextField();
        add(sliderPanel, BorderLayout.CENTER); // sliderPanel is added here
        add(sliderPanel, BorderLayout.SOUTH); // and here??!!
    to see the layout issue, expand the size of your app to see what happens. Edit: also FlowLayout is a very weak layout. You may be better off with a GridLayout(0, 1) for the panel holding the sliders, and place the sliders into their own little panels that use BorderLayout with the sliders at CENTER and the description labels BorderLayout.NORTH.
    Last edited by Fubarable; 05-25-2009 at 11:13 PM.

  4. #4
    jon80's Avatar
    jon80 is offline Senior Member
    Join Date
    Feb 2008
    Location
    Malta (EU)
    Posts
    211
    Rep Power
    7

    Default

    Thanks for correcting my typos. I've tried moving the files to another directory (C:\images), although I would like to include the image files with the project files (C:\Users\Administrator\workspace\SliderTest\image s) and possibly code the demo app with some sort of relative paths so I don't have to review my code when I need to copy it to another server.

    As for the GridBayLayout, I still haven't covered it in the tutorials I'm following :)

    Java Code:
    ...
    
    labelTable.put(0, new JLabel(new ImageIcon(ImageIO.read(getClass().getResource("c:\\images\\nine.png")))));
    		labelTable.put(20, new JLabel(new ImageIcon(ImageIO.read(getClass().getResource("c:\\images\\ten.png")))));
    		labelTable.put(40, new JLabel(new ImageIcon(ImageIO.read(getClass().getResource("c:\\images\\jack.png")))));
    		labelTable.put(60, new JLabel(new ImageIcon(ImageIO.read(getClass().getResource("c:\\images\\queen.png")))));
    		labelTable.put(80, new JLabel(new ImageIcon(ImageIO.read(getClass().getResource("c:\\images\\king.png")))));
    		labelTable.put(100, new JLabel(new ImageIcon(ImageIO.read(getClass().getResource("c:\\images\\ace.png")))));
    		
    
    ...
    Last edited by jon80; 05-26-2009 at 08:32 AM.

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    As for the GridBayLayout, I still haven't covered it in the tutorials I'm following
    Who suggested or even mentioned GridBagLayout here (besides you)?

  6. #6
    jon80's Avatar
    jon80 is offline Senior Member
    Join Date
    Feb 2008
    Location
    Malta (EU)
    Posts
    211
    Rep Power
    7

    Default

    Umm..right you mentioned GridLayout. As a matter of fact the images are not so good to look at because they seem to be on top of one another. I have a gut feeling this would work better in a "table like" grid.

    This is wot it looks like: output.png - Windows Live (sarks)

    Anyway this seems to work although the ..getResource does not (see my previous post).
    Any idea how to make the image path relative? How about making the slider stretch to fit the width of the images?

    *thinking*

    Java Code:
    labelTable.put(0, new JLabel(new ImageIcon("c:\\images\\nine.png")));
    		labelTable.put(20, new JLabel(new ImageIcon("c:\\images\\ten.png")));
    		labelTable.put(40, new JLabel(new ImageIcon("c:\\images\\jack.png")));
    		labelTable.put(60, new JLabel(new ImageIcon("c:\\images\\queen.png")));
    		labelTable.put(80, new JLabel(new ImageIcon("c:\\images\\king.png")));
    		labelTable.put(100, new JLabel(new ImageIcon("c:\\images\\ace.png")));
    NOTE: Which reminds me, it would be nice to be able to upload attachments (eg. videos, screendumps, log files), although I'm aware of the guidelines at Short, Self Contained, Correct Example.

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    You actually can upload a jar file that contains your java files and your resources such as images. You may need to first rename the file as a zip file before uploading though.

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Here's an image of the result of my version of your program using a GridLayout(0, 2, 10, 10), using a BorderLayout for the smaller panels:
    http://img5.imageshack.us/img5/5766/sliderpanel.jpg

  9. #9
    jon80's Avatar
    jon80 is offline Senior Member
    Join Date
    Feb 2008
    Location
    Malta (EU)
    Posts
    211
    Rep Power
    7

    Default

    Looks good; so far I couldn't figure out how to integrate the GridLayout within the JPanel..

  10. #10
    Join Date
    Mar 2009
    Posts
    25
    Rep Power
    0

    Default

    JPanel p = new JPanel(new GridLayout(2, 2)); // creates a JPanel with a gridlayout of 2 rows and 2 columns
    p.add(component1) // adds to row 0 column 0
    p.add(component2) // adds to row 0 column 1
    p.add(component3) // adds to row 1 column 0
    p.add(component4) // adds to row 1 column 1

    So visually it will look like this:
    Java Code:
    -------------------------
    |component1 | component2|
    |-----------------------|
    |component3 | component4|
    -------------------------

  11. #11
    jon80's Avatar
    jon80 is offline Senior Member
    Join Date
    Feb 2008
    Location
    Malta (EU)
    Posts
    211
    Rep Power
    7

    Default

    Well it does and somehow it seems to adjust the grid when there are more sliders to add, however, the icons labels are still crammed:

    output.png - Windows Live

  12. #12
    Join Date
    Mar 2009
    Posts
    25
    Rep Power
    0

    Default

    What you need to do then is see how many items you have (8) and the amount of columns you want (i'll assume 2) then make the JPanel as follows:

    JPanel p = new JPanel(new GridLayout(4, 2));

    As you add more components you will probably need to reflect this in the constructor when making the JPanel, cause if you are using my example and constructing it as new GridLayout(2, 2) then it doesn't know which is the constant value (in this case you want it to be the columns value).

    So to be on the safe side you should explicitly state how many rows, and how many columns you need.

  13. #13
    jon80's Avatar
    jon80 is offline Senior Member
    Join Date
    Feb 2008
    Location
    Malta (EU)
    Posts
    211
    Rep Power
    7

Similar Threads

  1. Replies: 2
    Last Post: 12-14-2009, 02:46 AM
  2. Replies: 10
    Last Post: 01-29-2009, 09:00 AM
  3. Replies: 12
    Last Post: 10-05-2008, 07:52 AM
  4. Replies: 3
    Last Post: 01-29-2008, 02:37 AM
  5. Replies: 1
    Last Post: 08-06-2007, 07:04 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •