Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

You can customize all kinds of things about how the UI appears. A lot is possible with the methods provided by the Swing components, but if that isn't sufficient it's possible to use the the pluggable look and feel and custom UI delegates to gain a greater degree of customization. Here is an overview of that architecture: http://java.sun.com/products/jfc/tsc/articles/architecture/

Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

You could define a method like

private void enableLength(boolean enable){
   txtLength.setEnabled( enable );
   lblLength.setEnabled( enable );
}

so your if() code becomes

if (optBox.isSelected() ){
  enableLength(true);
}

That would be a little clearer to follow and a bit less repetition. You would have one method for each of your dimensions.

Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

Window > Palette

Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

Just like any anonymous implemenation

return new Enumeration() {

            public boolean hasMoreElements() {
                throw new UnsupportedOperationException("Not supported yet.");
            }

            public Object nextElement() {
                throw new UnsupportedOperationException("Not supported yet.");
            }
        };

Creating a small private inner class for the enumeration and returning an instance of that would be better though. You can see that idiom used quite a lot in the JDK classes.

Also keep in mind that Iterator is generally preferred to Enumeration these days.

Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

Agreed that this is more of a HTML/CSS question. Moving it over there. Carry on :)

Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

Here is a "bare bones" mockup of how such an event model can be implemented. I've retained your names so it might be a little clearer

import java.util.ArrayList;
import java.util.List;

public class MainFrame implements SelectionListener {

    DataTable dataTable;
    String mainData;

    public MainFrame() {
        dataTable = new DataTable();
        // Add this as a listener to the DataTable objectd
        dataTable.addSelectionListener(this);
    }

    /** simulate the events and notification */
    public void run() {
        // initial state
        mainData = dataTable.getData();
        System.out.println(mainData);

        // data table gets updated (in reality this represents a user action,
        // but I have to simulate here)
        dataTable.updateSomeData();

        // verify our new data
        System.out.println(mainData);
    }

    /** implementation of SelectionListener interface */
    public void selectionChanged() {
        // get the updated data
        mainData = dataTable.getData();
    }

    public static void main(String[] args) {
        MainFrame mFrame = new MainFrame ();
        mFrame.run();
    }
}

class DataTable {

    private String someData = "initial data here";
    
    // list of listeners
    private List<SelectionListener> selectionListeners = new ArrayList<SelectionListener>();

    /** Add a SelectionListener */
    public void addSelectionListener(SelectionListener listener) {
        selectionListeners.add(listener);
    }

    /** notify SelectionListeners of change*/
    private void fireSelectionChanged() {
        for (SelectionListener listener : selectionListeners) {
            listener.selectionChanged();
        }
    }

    public String getData() {
        return someData;
    }

    /** This method just represents some operation that alters the data */
    public void updateSomeData() {
        // doing stuff...
        someData = "new data here now";
        
        // done, so let all the listeners know
        fireSelectionChanged();
    }
}

interface SelectionListener {

    void selectionChanged();
}
BestJewSinceJC commented: Very detailed and quality help +4
Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

That is exactly the point of the models I mentioned above. You need to implement a way that your main frame can register itself as a listener for the selection change.

With the small model I included above, your main frame would implement that SelectionListener interface and register itself with the data panel as a listener (calling addSelectionListener(this); ). Any method that made changes to the selection would use fireSelectionChanged() to notify that things had been changed. That is when your main frame would call the accessor to get it's data. So basically your main frame would have an implementation of the SelectionListener like this

public void selectionChanged(){
   dataTable.getSelections();  // or whatever
}

The listener models are there to handle that "let other things know things changed" part.

Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

I'm struggling a bit to understand the exact behavior you are aiming for, but if you're just saying that the "main frame" needs to take some action when the selection array changes, you can make a small listener model for this yourself very easily

interface SelectionListener {
    void selectionChanged();
}

/** These go in your DataTable class */
List<SelectionListener> selectionListeners = new ArrayList<SelectionListener>();

public void addSelectionListener(SelectionListener listener){
    selectionListeners.add(listener);
}   

private void fireSelectionChanged(){
    for (SelectionListener listener : selectionListeners){
        listener.selectionChanged();
    }
}

Your action listener would call fireSelectionChanged() to notify all listeners of a selection change. Your main frame can then query the info that it needs. If you wanted to send that data with the selection change event, you could make a small SelectionChangeEvent object to pass with that info.

You could also just wire up a ChangeListener or set up your selection list as a bound property of the DataTable.

Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

Keep the point2 assignment and a repaint() in mouseDragged() . Leave mouseReleased() as you have it, but also set point2 to null. Then alter your paintComponent() method to draw a line from point1 to point2 if point2 is not null.

BestJewSinceJC commented: very helpful in this thread (acc. to OP) +4
Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

You're adding a new line for every mouseDragged() event. You only want to add a line when the mouse is released.

Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

So examine your loop and the code in it and try to figure out how it could access an index that is too high for your collection.

Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

thanks Ezzaral : ) that problem is solved,but I got another error in the for loop, can you take a look for me please? thank you

I think you are getting more detailed information than "error message". I can guess what it is, but actually posting those error messages would be helpful. They are usually very specific in telling you the problem.

edit: posted while you were posting this most recent version.

Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

thanks , I revised my code but got a couple of error messages, which I indicated in the following code. what could my problems be? thanks again for any help : )

You're trying to access the elements of lineList like an array, but it's an ArrayList. Use lineList.get(i) instead.

Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

Read carefully what you are doing in the paintComponent() method. You are looping through every point in the array list and each time drawing a line between point1 and point2 - which are completely separate from your list of points.

Perhaps you want to loop the array list by it's index and for each pair of points draw a line and increment by two.

Alternately you could create a Line object when the mouse is released and add that to an ArrayList instead of points. Then your for:each loop could draw each line in the collection.

Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

There are existing wrapper classes for all of the primitive types.
Use Integer. Auto-boxing will handle the casting for you.

Ezzaral 2,714 Posting Sage Team Colleague Featured Poster
JimD C++ Newb commented: Pointed me in just the right direction. Thank you! +2
Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

If you mean task instead of target, yes there is.
Read the documentation.

Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

You may just be missing a call to setVisible().

Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

You declare it as an array of CD objects, like so

CD[] cdList = new CD[10];

and create them in the array like so

cdList[0]=new CD("Kaiser "," up the khazi ", 9.99);
Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

How does the action for point1 differ from point2? That is what matters for the mouse handler.

Do you actually have a frame class called NewWindow? Does it make itself visible in it's constructor?

Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

You already know which point was clicked, because that point itself is the mouse handler, so all you need to do is create and show the new frame. You can pass whatever information you need from the point label to the frame.

Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

You could use something like this in your MapPanel to manage the "spots"

import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.*;

public class MapPanel extends JPanel {

    Image map;
    ImageIcon spot1, spot2, spot3, note;
    JLabel point1, point2;

    public MapPanel() {
        Toolkit kit = Toolkit.getDefaultToolkit();
        map = kit.getImage("cebumap.jpg");

        spot1 = new ImageIcon("spot.gif");
        point1 = new MapLabel(spot1, "p1");
        point1.setBounds(168, 30, spot1.getIconWidth(), spot1.getIconHeight());

        spot2 = new ImageIcon("spot2.gif");
        point2 = new MapLabel(spot2, "p2");
        point2.setBounds(203, 55, spot2.getIconWidth(), spot2.getIconHeight());

        setPreferredSize(new Dimension(457, 540));

        setLayout(null);
        add(point1);
        add(point2);
    }

    public void paintComponent(Graphics comp) {
        Graphics2D comp2D = (Graphics2D)comp;
        comp2D.drawImage(map, 0, 0, this);
    }

    /** Small convenience class to manage all of your special point label functionality in one place */
    class MapLabel extends JLabel implements MouseListener {
        Icon icon;
        String name;

        public MapLabel(Icon icon, String name) {
            this.icon = icon;
            this.name = name;

            setIcon(icon);
            setToolTipText(name);
            addMouseListener(this);
        }

        public void mouseClicked(MouseEvent e) {
            JOptionPane.showMessageDialog(this, "You clicked " + name);
        }

        public void mouseEntered(MouseEvent e) {
            setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
        }

        public void mouseExited(MouseEvent e) {
            setCursor(null);
        }

        public void mousePressed(MouseEvent e) {}
        public void mouseReleased(MouseEvent e) {}
    }
}
nagatron commented: Impressive, Great, Good Job +2
Ezzaral 2,714 Posting Sage Team Colleague Featured Poster
nagatron commented: Thank you again for the help. . .eventhough I am anoying. .hehehe +2
Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

- Labels do not have to be opaque. setOpaque(false); - setLayout(null); will allow you to place a component anywhere you like.

Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

Doing those things would be much easier if you use JLabels to hold the "spot" images:
http://java.sun.com/docs/books/tutorial/uiswing/components/icon.html

Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

Welcome back :)

Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

I would recommend reading Code Complete, 2nd Edition.

majestic0110 commented: Good Book! +6
Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

He did tell you how to do it. Read the last two sentences again carefully.

Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

Yes, if the catch expression is appropriate to catch that particular exception type. The exception will be passed up the call stack until it is handled or causes termination.

Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

It's telling you that you can't use constructors that don't exist.

majestic0110 commented: Beat me to it lol :) +6
Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

There is a little red triangle icon in the upper righthand corner to report the private message. It's in the same frame as the title of the PM.

Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

Edit: Nevermind, I misread.

Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

Just add placeholder columns on the smaller query, such as "null as code" or "0 as code".

Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

Just copy everything from lines 3-10 into your method and change the variables to use your method parameters.

KirkPatrick commented: I didn't realize object had to be changed to component. Thanks for the help bud +1
Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

Sure, just use

!str.equals(otherStr);
Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

The post title (not thread title) also says:

Re: CPU Fan Error! Overclocking Failed! Over Voltage Failed!

. Often splits still have that "Re:" carried over.

Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

The axes are just lines you draw like any other and you use drawString() for the labeling as VernonDozier already mentioned. You have to draw everything yourself. There is nothing built in to support charting.

Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

I've not personally had any problems running Firefox 3.5.2 on 64-bit Vista. I've only had that system about a week though.

Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

Take a look at the Robot class.

TheWhite commented: thaks! +2
Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

Here is a small function that will extract a single entry from a jar file. The "jarEntry" parameter is simply a string path like "com/myPackage/MyClass.class" that denotes the entry you want to extract

public static void extractFromJar(String jarEntry, File jar, File outputFile) throws IOException{
        JarFile jarFile = new JarFile(jar);
        JarEntry entry = jarFile.getJarEntry(jarEntry);
        if (entry!=null){
            InputStream jarEntryStream = jarFile.getInputStream(entry);
            FileOutputStream outStream = new FileOutputStream(outputFile);
            byte[] buffer = new byte[1024];
            int bytes;
            while ((bytes = jarEntryStream.read(buffer)) != -1) {
                outStream.write(buffer, 0, bytes);
            }
            outStream.close();
            jarEntryStream.close();
            jarFile.close();
        }
    }

As for your path question, you can use getResource() as you did above to search for a resource location in the class path or you can use System.getProperty("user.dir") to obtain the path that the app is currently executing in.

Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

which of course doesnt work..

So don't do that.

btw, I know the way I did this was all jacked up. Can anyone help me? I'm sure theres a better way. I can try to explain more if I can.

Extract the file before you run it. Look at the JarFile class and specifically the getInputStream(java.util.zip.ZipEntry) will let you get an input stream for a particular entry, which you can then write to an output stream.

Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

From series of PMs on this:

Hi, I have implemented the dragging however because in the example you gave me the paint draws everything in the shapes arraylist so when i drag a shape the orignal position is drawn. Also the path which the circle is dragged is also drawn. I don't know why that is. I'm also not too sure how to use your hitTest method. If it returns the shape under the cursor how would i then use that to select the shape, for example by colouring it a different colour and then implementing the move? Sorry for so many questions. I really hope you can help. Thank you for the help you'v given so far.

My example showed how to select one of the shapes with the mouse click and move it when dragged. Your current code adds a new shape on click. You need to decide which you want to do, because you can't do both at the same time. You could use right/left click or modifiers like shift to separate the operations.

That is the problem I am having. I need to be able to draw the circle where the mouse is clicked. Then be able to select the circle and move it to a new position. I don't know how to do that.

And I alrady explained that you need to separate the two actions into two different mouse events, unless you want to first do the hitTest to check for a …

Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

Here's a small example of a custom JPanel class that keeps a List of Shape objects and lets you drag them with the mouse. Perhaps it will clear up some of your uncertainties

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Shape;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Rectangle2D;
import java.awt.geom.RectangularShape;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class DraggableGraphic extends JFrame {
    
    private JPanel paintPanel;

    public DraggableGraphic() {
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setMinimumSize(new Dimension(300, 300));

        paintPanel = new  PaintPanel();
        getContentPane().add(paintPanel, BorderLayout.CENTER);
        pack();
    }

    class PaintPanel extends JPanel implements MouseMotionListener,MouseListener {
        private List<Shape> shapes;
        private Shape mouseOverShape=null;
        
        public PaintPanel(){
            super();
            addMouseListener(this);
            addMouseMotionListener(this);

            shapes = new ArrayList<Shape>();
            shapes.add(new Ellipse2D.Float(25, 15, 60, 30));
            shapes.add(new Ellipse2D.Float(75, 35, 60, 30));
        }
        
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2 = (Graphics2D)g;
            for (Shape s : shapes){
                g2.draw(s);
            }
        }

        public void mouseDragged(MouseEvent e) {
            if (mouseOverShape!=null){
                if(mouseOverShape instanceof RectangularShape){
                    // Move the shape center to the mouse location
                    RectangularShape rShape = (RectangularShape)mouseOverShape;
                    float width = (float)rShape.getWidth();
                    float height = (float)rShape.getHeight();
                    rShape.setFrame(new Rectangle2D.Float(e.getX()-width/2, e.getY()-height/2, width, height));
                }
                repaint();
            }
        }

        public void mouseMoved(MouseEvent e) {}

        /** returns the first Shape that contains Point p or null if none contain p */
        private Shape hitTest(Point p){
            Shape hitShape = null;
            for (Shape testShape : shapes){
                if (testShape.contains(p)){
                    hitShape = testShape;
                    break;
                }
            }
            return hitShape;
        }


        public void mousePressed(MouseEvent e) {
            // figure out what shape, if any, that we are clicking on
            mouseOverShape = hitTest(e.getPoint());
        }

        public void mouseReleased(MouseEvent …
Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

You should restructure your code a bit to override the paintComponent() method of the JPanel objects you wish to draw to, rather then calling getGraphics() to obtain the graphics context. I gave an example above of how paintComponent() can loop through and render the shapes. Your mouse methods just need to create or modify shapes in the List and call repaint to update the graphics.

I'd recommend looking through this short tutorial on custom painting in Swing to see how to override paintComponent().
http://java.sun.com/docs/books/tutorial/uiswing/painting/index.html

Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

In your mouse listener methods, create the Shape objects instead of drawing them directly to the screen.

Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

If you maintain a list of shapes

List<Shape> shapes = new ArrayList<Shape>();

You can override the paintComponent method to paint them like so

public void paintComponent(Graphics g){
    Graphics2D g2 = (Graphics2D)g;
    for (Shape s : shapes){
        g2.draw(s);
    }
}

If you want to move one of the shapes, your mouse routines first need to determine which shape is under the cursor. You can do that easily by checking the contains() method of each Shape against your mouse coordinates. Here is a method you could use to check them

private Shape hitTest(Point p){
    Shape hitShape = null;
    for (Shape testShape : shapes){
        if (testShape.contains(p)){
            hitShape = testShape;
            break;
        }
    }
    return hitShape;
}
Ezzaral 2,714 Posting Sage Team Colleague Featured Poster
Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

HTML is a subcategory under Web Design, for future reference in finding it :)
Moving this there.

Ezzaral 2,714 Posting Sage Team Colleague Featured Poster
Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

Moving an actual component would trigger a repaint automatically, but it looks like he is "dragging" a shape that's been painted on the component, which would require a repaint() to update the location.