#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    74
    Rep Power
    2

    JPanel image background


    im having a problem in with my JPanel that holds the background image. whenever i create the jar file and transfers the jar file into another computer. background image is no longer working. how to work on this?

    Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    
    public class MT implements ActionListener
    {    
        String[] tagalog = {"ako", "ay" , "matapang", "at", "malakas", "kumain"};
        String[] english = {"me", "is", "brave", "and","strong", "eat"};
        String string;
        int counter;
        
        JTextField field1, field2;
        JPanel sub_pane;
        JLabel text1, text2, bg;
        JButton translate;
        Image ball;
        
        
        
        public JPanel contentPANE()
        {
            JPanel rootpane = new JPanel();
            rootpane.setLayout(null);
            rootpane.setOpaque(true);
            rootpane.setBackground(Color.yellow);
            
            sub_pane = new JPanel();
            sub_pane.setLayout(null);
            sub_pane.setBackground(Color.yellow);
            sub_pane.setBounds(5,55,215,63);
            rootpane.add(sub_pane);
            sub_pane.setVisible(true);
            
            bg = new JLabel(new ImageIcon("D:\\My Files\\BlueJ Projects\\binary_search_AI\\New folder\\tip.jpg"));
            bg.setBounds(0,0,63,63);
            sub_pane.add(bg);
            
            text1 = new JLabel("Technological Institute of");
            text1.setBounds(70,18,180,15);
            sub_pane.add(text1);
            
            text2 = new JLabel("         the Panorama");
            text2.setBounds(70,32,180,15);
            sub_pane.add(text2);
            
            
            field1 = new JTextField();
            field1.setBounds(5, 5 , 100, 20);
            field1.setBackground(Color.black);
            field1.setForeground(Color.yellow);
            field1.setHorizontalAlignment(JTextField.CENTER);
            field1.setBorder(BorderFactory.createEmptyBorder());
            rootpane.add(field1);
            
            field2 = new JTextField();
            field2.setBounds(120, 5, 100, 20);
            field2.setEditable(false);
            field2.setBackground(Color.black);
            field2.setForeground(Color.yellow);
            field2.setHorizontalAlignment(JTextField.CENTER);
            field2.setBorder(BorderFactory.createEmptyBorder());
            rootpane.add(field2);
            
            translate = new JButton("translate");
            translate.setBounds(5, 30, 215, 20);
            translate.addActionListener(this);
            rootpane.add(translate);
            return rootpane;
        }
        
        private static void createGUI()
        {
            JFrame frame = new JFrame("[AI] [CS32FB1]");
            frame.setSize(230,152);
            frame.setLocationRelativeTo(null);
            frame.setResizable(false);
            frame.setVisible(true);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            MT contentpane = new MT();
            frame.setContentPane(contentpane.contentPANE());
        }
        
        public void actionPerformed(ActionEvent ae)
        {
            field1.getText();
            for(counter = 0; counter < tagalog.length; counter++)
            {
                if(ae.getSource() == translate && field1.getText().equals(tagalog[counter]))
                {
                    field2.setText(english[counter]);
                }
                else if(ae.getSource() == translate && field1.getText().equals(english[counter]))
                {
                    field2.setText(tagalog[counter]);
                }
            }
        }
               
        
        public static void main(String[] args)
        {
            SwingUtilities.invokeLater(new Runnable()
            {
                public void run()
                {
                    createGUI();
                    
                }
            });
        }
    }
    Last edited by Kuiva; February 8th, 2014 at 08:45 AM. Reason: added my source code
  2. #2
  3. Java Junkie
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Feb 2004
    Location
    Mobile, Alabama
    Posts
    4,023
    Rep Power
    1285
    You will need to place the image in the jar file and then write your code that that it loads it from there
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    74
    Rep Power
    2
    Originally Posted by bullet
    You will need to place the image in the jar file and then write your code that that it loads it from there
    how do i do that? i completely don't have an idea. i am knew to this stuff.
  6. #4
  7. Lord of the Dance
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Oct 2003
    Posts
    3,730
    Rep Power
    1959
    Take a look at this page:
    http://docs.oracle.com/javase/tutorial/deployment/webstart/retrievingResources.html
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    74
    Rep Power
    2
    Originally Posted by MrFujin
    Take a look at this page:
    http://docs.oracle.com/javase/tutorial/deployment/webstart/retrievingResources.html

    tried it and it somehow works. but the buttons are gone. but when i hover the mouse to the location of the buttons they show up. what could possibly be wrong in it?
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    74
    Rep Power
    2
    problem solved Thank you all

IMN logo majestic logo threadwatch logo seochat tools logo