Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Java Swing problem?

  1. #1
    Junior Member
    Join Date
    Jul 2010
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Java Swing problem?

    Hi

    i have a simple code that make you choose an image and display its thumbnail to you

     //GUI
            final JFrame ff=new JFrame();
     
            final JButton b1=new JButton("Choose file");
            final JLabel l1=new JLabel("Choosen file: ");
     
     
     
     
     
     
     
            ff.setVisible(true);
            ff.setSize(700, 500);
            ff.getContentPane().setLayout(null);
     
     
     
     
            b1.setBounds(300, 20, 100, 30);
            l1.setBounds(300, 60, 80, 30);
            ff.getContentPane().add(b1);
            ff.getContentPane().add(l1);
     
     
     
     
     
     
     
     
     
            b1.addActionListener(new ActionListener(){
    			public void actionPerformed(ActionEvent e)
    			{
    			try{
     
                            JFileChooser chooser = new JFileChooser();
                            int returnVal = chooser.showOpenDialog(null);
                            if(returnVal == JFileChooser.APPROVE_OPTION)
                            {
                                //Convert the data into an image
     
                               Image i=ImageIO.read(new File((chooser.getSelectedFile().getPath())));
                               i=i.getScaledInstance(100, 100,i.SCALE_DEFAULT);
                               JLabel img= new JLabel(new ImageIcon(i));
                               img.setBounds(320, 90, 100, 100);
                               ff.getContentPane().add(img);
     
     
     
                            }
                            }catch(IOException IO){System.out.print("File not found");}
                            }});
    it works fine except when the image shows it doesn't show unless i re-size the size of the frame..
    it got nothing to do with the original size of the frame because i tried bigger frames.
    what i noticed is that if i add a component to the frame inside an action listener this happens.

    thx
    Nasser
    Last edited by helloworld922; July 3rd, 2010 at 02:50 PM. Reason: please surround your code with [highlight=Java][/highlight]


  2. #2
    Member Charlie's Avatar
    Join Date
    Jun 2010
    Location
    Sweden
    Posts
    41
    Thanks
    1
    Thanked 5 Times in 5 Posts

    Default Re: Java Swing problem?

    Short version:
    repaint yo.

    Long version:
    "To enable app-triggered painting, the AWT provides the following java.awt.Component methods to allow programs to asynchronously request a paint operation:

    public void repaint()
    public void repaint(long tm)
    public void repaint(int x, int y, int width, int height)
    public void repaint(long tm, int x, int y,
    int width, int height)
    "

    Painting in AWT and Swing

  3. The Following User Says Thank You to Charlie For This Useful Post:

    nasser (July 28th, 2010)

  4. #3
    Junior Member
    Join Date
    Jul 2010
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Java Swing problem?

    thanks a lot!!!
    it worked

Similar Threads

  1. How to Use a JSlider - Java Swing
    By neo_2010 in forum Java Swing Tutorials
    Replies: 4
    Last Post: March 29th, 2010, 10:33 AM
  2. java swing help
    By JM_4ever in forum AWT / Java Swing
    Replies: 3
    Last Post: October 7th, 2009, 07:42 AM
  3. Swing problem, newbie'ish . Perhaps thread related?
    By fenderman in forum AWT / Java Swing
    Replies: 3
    Last Post: July 22nd, 2009, 05:45 AM
  4. How to Use the JList component - Java Swing
    By neo_2010 in forum Java Swing Tutorials
    Replies: 1
    Last Post: July 11th, 2009, 05:02 AM
  5. [SOLVED] [Problem] imports javax.swing problem
    By Brollie in forum AWT / Java Swing
    Replies: 8
    Last Post: July 5th, 2009, 08:59 AM