Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Newcomers » Newcomers » importing Source code
importing Source code [message #1790785] Sun, 17 June 2018 12:20 Go to next message
Walter White is currently offline Walter WhiteFriend
Messages: 1
Registered: June 2018
Junior Member
Hi everyone,
Kindly let me know the proecedure for importing source code into a new java project using eclipse luna.
Re: importing Source code [message #1790853 is a reply to message #1790785] Tue, 19 June 2018 06:50 Go to previous message
Patrick Moran is currently offline Patrick MoranFriend
Messages: 141
Registered: March 2018
Senior Member
Go to New Project. Choose File/new/JavaProject
Give the new project a name, e.g. GUIdoings. (Use the default settings.) Now
when you open that new project you will see, as a sub-menu thereto: JRE System Library
and Src.

Highlight Src and then go to File/New Class.
Give the new class the appropriate title, e.g., HelloWorldGUI2 (Make sure you've navigated to the correct project name and chosen src in its dropdown menu.)

Delete the skeleton program provided by Eclipse, and copy in the code from your source, e.g. http://math.hws.edu/javanotes/c6/s1.html

Check your new program for any errors and correct them.

Hit run, and the program should appear and function correctly.

When you've tried that program, close it, and find its name in the Program Explorer at the left edge of the Eclipse screen. Right click (control click) on that file name and choose "Open with/WindowBuilder Editor."

In the new window that appears, you'll see the original code. At the bottom of the screen just below that code you will see two tabs. You will be using the "Source" tab. If you select the Design tab, a fairly long period of cogitation by your computer will, hopefully, end up with the graphic design components of your new program as provided by Eclipse.

Be aware that, for me at least, this process only works on the simplest of programs.

Originally I tried to go from working code for http://ads.gigatux.nl/tutorial6/uiswing/examples/components/ComboBoxDemoProject/src/components/ComboBoxDemo.java by extracting the code that I laboriously made some time ago and knew would work, and starting over as I would if I had only that source code. I have spent an entire day without being able to get it to work. All that is required to ruin it is to copy the source code into a new project as described above. First, it loses the dimensioning of the initial splash screen. You see only the upper left-hand corner red-light, yellow-light, green-light, and you have to expand it. Then you discover that your program cannot find the image files. (The instructions given by Sun are not clear. What Java expects is to find a folder called, e.g., Images, that you have created by using File/New/Folder, and have then filled by dragging and dropping in five gif files that you've created and stored somewhere else. Just drop them into the "Images" folder you see in Package Explorer.

There must be hidden dependencies that link the components that you see spread out in the Package Explorer. That is definitely true when you import, e.g., jar files and have to do a "build path" routine. But I've noticed, ruefully, that every time I change anything at all about a ComboBox that I made by drawing it in Design mode, and then control-clicking on it to open an action listener, and then have filled out the specifics in the Source tab, just changing one word can make the ComboBox inoperative. Then you ,must go back into Design mode, remove the old ComboBox (you've already saved the guts of it to a file in TextEdit or whatever), then you do the control click and Action Performed stuff one more time, find the new ComboBox wherever Eclipse decided to drop it, paste in the guts, and you get your functionality back.

So I think that the JComboBox on screen must be serialized, the JComboBox code in the Source view must have a corresponding serialization, and there is a record in the mind of Eclipse of what serial number corresponds to what serial number. When you edit the comboBox in Source, then the serial number for that block of code is changed. So it is no longer connected to the combobox on-screen programming. Wasabi PITA.

I'll attach the source code that works for me (in the workspace that I've created for it that has all the hidden configuration stuff that Eclipse inserts in the background). Give it a try. But beware. It isn't at all simple to get it to work, and I'm not sure how I did it the first time around.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.EmptyBorder;

/*                                                 
 * ComboBoxDemo.java uses these additional files:
 *   images/Bird.gif
 *   images/Cat.gif
 *   images/Dog.gif
 *   images/Rabbit.gif
 *   images/Pig.gif
 */
public class ComboBoxDemo extends JPanel
                          implements ActionListener {
    JLabel picture;

    public ComboBoxDemo() {
    	setBackground(new Color(204, 255, 51));

        String[] petStrings = { "Bird", "Cat", "Dog", "Rabbit", "Pig" };
        
        int num = 2;

        //Create the combo box, select the item at index 4.
        //Indices start at 0, so 4 specifies the pig.
        JComboBox petList = new JComboBox(petStrings);
        petList.setBounds(20, 20, 410, 27);
        petList.setSelectedIndex(num);
        petList.addActionListener(this);

        //Set up the picture.
        picture = new JLabel();
        picture.setBounds(20, 20, 410, 260);
        updateLabel(petStrings[petList.getSelectedIndex()]);
        picture.setBorder(BorderFactory.createEmptyBorder(10,0,0,0));


        picture.setPreferredSize(new Dimension(200, 250+10));
        setLayout(null);
        //Lay out the demo.
        add(petList);
        add(picture);
        setBorder(new EmptyBorder(25, 25, 25, 25));
             
    }

    /** Listens to the combo box. */
    public void actionPerformed(ActionEvent e) {
        JComboBox cb = (JComboBox)e.getSource();
        String petName = (String)cb.getSelectedItem();
        updateLabel(petName);
    }

    protected void updateLabel(String name) {
        ImageIcon icon = createImageIcon(name + ".gif");
        picture.setIcon(icon);
        picture.setToolTipText("A drawing of a " + name.toLowerCase());
        if (icon != null) {
            picture.setText(null);
        } else {
            picture.setText("Image not found");
        }
    }

    /** Returns an ImageIcon URL, or null if the path was invalid. */
    protected static ImageIcon createImageIcon(String path) {
        java.net.URL imgURL = ComboBoxDemo.class.getResource(path);
        if (imgURL != null) {
            return new ImageIcon(imgURL);
        } else {
            System.err.println("Couldn't find file: " + path);
            return null;
        }
    }
 
    /**
     * Create the GUI and show it.  For thread safety,
     * this method should be invoked from the
     * event-dispatching thread.
     */
    private static void createAndShowGUI() {
        //Create and set up the window.
        JFrame frame = new JFrame("ComboBoxDemo");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                
        //Create and set up the content pane.
        JComponent newContentPane = new ComboBoxDemo();
        newContentPane.setOpaque(true); //content panes must be opaque
       frame.setContentPane(newContentPane);
     

        //Display the window.
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        //Schedule a job for the event-dispatching thread:
        //creating and showing this application's GUI.
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }
}

Previous Topic:Oxygen IDE question wrt commenting methods
Next Topic:Plugin Development on Windows and Mac
Goto Forum:
  


Current Time: Fri Apr 26 20:09:36 GMT 2024

Powered by FUDForum. Page generated in 0.07404 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top