Hello Everyone,
I am trying to extract integer values from 2 int variables i have created in my database table "mytab". The data or table values i have extracted on console and its working fine. But i want to use the values as x,y coordinate for my label i have created on my frame. I want to display the label "*" at different pixel positions according to the x,y values extracted from sql table. But i am not able to achieve the same. Any help?

import java.sql.*;
import java.io.*;
import java.awt.*;
import javax.swing.JFrame;
import javax.swing.*;
import java.applet.Applet;
import java.awt.Graphics2D;
import java.awt.Point;
import javax.swing.JPanel;
import javax.swing.*;
import java.awt.geom.Point2D;

public class myclass extends JFrame{

public static void main(String[] args)
    {int varx,vary;
	JLabel textLabel;
	JFrame f = new JFrame("Pixel representation");
	f.setSize(500,500);
	f.setVisible(true);
	f.setDefaultLookAndFeelDecorated(true);
	f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	textLabel = new JLabel("*",SwingConstants.CENTER); 
	textLabel.setPreferredSize(new Dimension(300, 100));
        try {
        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
        Connection con = DriverManager.getConnection("jdbc:odbc:mytab","",""); 
        Statement s = con.createStatement();
        s.execute("select * from mytab");
	ResultSet rs = s.getResultSet(); 
        if (rs != null) 
        while (rs.next()) {
	varx=rs.getInt(1);
	vary=rs.getInt(2);
       System.out.println("X: " +varx);
       System.out.println("Y: " +vary);
	setLocation(varx,vary);	
        }
	f.getContentPane().add(textLabel, BorderLayout.CENTER);
        s.close(); 
        con.close(); 
     }
        catch (Exception err) {
        System.out.println("ERROR: " + err);
    }

}
}

ERROR MESSAGE:

37 line: non-static method setLocation(int,int) cannot be referenced from a static context
        setLocation(varx,vary);
        ^

Cheers

Recommended Answers

All 15 Replies

You need to call "setLocation" on f, and there is no reason for your class to extend JFrame, here, especially when you are initiating a separate JFrame. Also, you need to start the Frame in the Event thread (see this). And you should probably do your query before ever instantiating the frame.

it should be something like this
textLabel.setLocation(varx,vary);

But it is not displaying multiple stars on the Frame.

Cheers

Because you are doing it completely wrong. setLocation is not what you want. You probably want to do your query, and get the max "x" and max "y" and create a JPanel with a GridLayout, add that to your JFrame (or change the JFrame's contentPane's layout to GridLayout), then add JLabels to that JPanel(/contentPane).

thanks for writing back.

I didn't got the desired stuff.

There you go:

import java.sql.*;
import java.io.*;
import java.awt.*;
import javax.swing.JPanel;
import javax.swing.*;

public class kmd123 extends JFrame{

public static int x;
public static int y;
public static JFrame f;
public static JPanel p;
public static JLabel l;


kmd123(){
JFrame f = new JFrame("Pixels stuff");
p = new JPanel();
p.setBackground(Color.WHITE);
l = new JLabel("*");
try {
        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
        Connection con = DriverManager.getConnection("jdbc:odbc:mytab","",""); 
        Statement s = con.createStatement();
        s.execute("select * from mytab");
	ResultSet rs = s.getResultSet(); 
        if (rs != null) 
        while (rs.next()) {
	x=rs.getInt(1);
	y=rs.getInt(2);
	System.out.println("X: " +x);
        System.out.println("Y: " +y);
//	l.setLocation(x,y);
	p.setLayout((new GridLayout(x,y)));
	    p.add(l);
	    f.add(p);
	    f.pack();
	    f.setVisible(true); 
}
//	f.setSize(1000,1000);
	f.setDefaultCloseOperation( EXIT_ON_CLOSE );
	s.close(); 
        con.close();
    }
	catch (Exception err) {
        System.out.println("ERROR: " + err);
}


}

public static void main(String args[]) {
	new kmd123();
	}	

}

Thanks for writing back.

Cheers

Like I said, get the max x and y, and create the GridLayout using that, then add all the labels to that one Panel (using the add method that allows you to designate which "cell" the label should be added to), don't recreate the panel everytime.

i cannot set the max x and y just because i want to display the stars (*) according to my x,y coordinate values which i have extracted from sql table which varies for every row in my sql table:

say first row have x,y = 12,20
next row = 24,40
and so on..

I want to work on one Label (*) with the different coordinates.
Anyhow this is how things lined up..
Can you modify it at your end?

Here is the sql table mytab:

create table mytab (
varx int,
vary int,
)

insert into mytab values (6,9)
insert into mytab values (12,14)
insert into mytab values (15,17)
insert into mytab values (18,19)
insert into mytab values (45,77)
import java.sql.*;
import java.io.*;
import java.awt.*;
import javax.swing.JPanel;
import javax.swing.*;

public class kmd123 extends JFrame{

public static int x;
public static int y;
public static JFrame f;
public static JPanel p;
public static JLabel l;


kmd123(){
JFrame f = new JFrame("Pixels stuff");
p = new JPanel();
p.setBackground(Color.WHITE);
l = new JLabel("*");
p.setLayout((new GridLayout(500,500)));
try {
        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
        Connection con = DriverManager.getConnection("jdbc:odbc:mytab","",""); 
        Statement s = con.createStatement();
        s.execute("select * from mytab");
	ResultSet rs = s.getResultSet(); 
        if (rs != null) 
        while (rs.next()) {
	x=rs.getInt(1);
	y=rs.getInt(2);
	System.out.println("X: " +x);
        System.out.println("Y: " +y);
	l.setLocation(x,y);
	    p.add(l);

}
	    f.add(p);
	    f.pack();
	    f.setVisible(true); 
	f.setSize(1000,1000);
	f.setDefaultCloseOperation( EXIT_ON_CLOSE );
	s.close(); 
        con.close();
    }
	catch (Exception err) {
        System.out.println("ERROR: " + err);
}


}

public static void main(String args[]) {
	new kmd123();
	}	

}

Do you know what a grid is?

If so, then you know why you need the max x and y to create the GridLayout with.

Doing new GridLayout(4,4) creates a grid as follows:

---------
| | | | |
---------
| | | | |
---------
| | | | |
---------
| | | | |
---------

and doing add(label, 2, 2) (or something like that, check the API docs) adds a label to it as follows:

---------
| | | | |
---------
| | | | |
---------
| | |x| |
---------
| | | | |
---------

Now do you understand why you need to get the max x and y and why you need to add all labels to the same panel?

Edit: And, no, I am not going to edit your code for you.

Store the x and y coordinates in a list (or do the query twice) so that you can get the max x and y and then start creating the labels to place in the grid.

I did this:
p.add(l,x,y);
and it gave me:
after displaying the first row
X: 18
Y: 19
ERROR: java.lang.IllegalArgumentException: illegal component position

Sorry, first of all, you can't designate the cell with gridlayout (that's what I'm sorry about, I don't use GridLayout very often, I usually use GridBagLayout, so I was "imagining" something, but GridLayout is still correct for your purposes), you have to actually fill all of the cells, in order, so you are going to need to order your results, sort by x, then by y, and add labels with an empty String (or a single space) for every cell for which you have no entry.

If you want to do this differently, then look at the API for canvas and use that (but remember to allow the text to have a size, i.e. you will have to multiply the coordinates by something), or use a "null" layout (Google that as I hate that "non-layout" and all newbies seem to insist on trying to use it, but, again, remember that the labels take up more than a single pixel of space), or look at GridBagLayout (which might, or might not, work, depending on how you did it, but I don't think you're ready for this is any case).

Edit: Like I said, see the API docs for GridLayout.

This link will probably be of inestimable worth to you to.

Haha.. thanks for helping though i need to get back to have some food then i will try to lay my hands on it again with GridBagLayout for sure..

Cheers

This is what it gave me:

X: 18
Y: 19
ERROR: java.lang.IllegalArgumentException: illegal component position

Here is the code:

import java.sql.*;
import java.io.*;
import java.awt.*;
import java.awt.*;
import javax.swing.*;

public class GridLayoutDemo {
    public final static boolean RIGHT_TO_LEFT = false;
public static int x;
public static int y;

    public static void addComponentsToPane(Container contentPane) {
        if (RIGHT_TO_LEFT) {
            contentPane.setComponentOrientation(
                ComponentOrientation.RIGHT_TO_LEFT);
        }
//        Any number of rows and 2 columns

        String asterisk = "*";        

        contentPane.setLayout(new GridLayout(50,50));
try {
        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
        Connection con = DriverManager.getConnection("jdbc:odbc:mytab","",""); 
        Statement s = con.createStatement();
        s.execute("select * from mytab");
	ResultSet rs = s.getResultSet(); 
        if (rs != null) 
        while (rs.next()) {
	x=rs.getInt(1);
	y=rs.getInt(2);
	System.out.println("X: " +x);
        System.out.println("Y: " +y); 
       contentPane.add(new JLabel(asterisk),x,y);}
	s.close(); 
        con.close();
    }
	catch (Exception err) {
        System.out.println("ERROR: " + err);
}
 
 //       contentPane.add(new JLabel(asterisk));
 //       contentPane.add(new JLabel(asterisk));
 //       contentPane.add(new JLabel(asterisk));
    }

    private static void createAndShowGUI() {
        JFrame.setDefaultLookAndFeelDecorated(true);

        JFrame frame = new JFrame("GridLayout Source Demo");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //Set up the content pane and components in GridLayout
        addComponentsToPane(frame.getContentPane());

//        frame.pack();
        frame.setSize(1000, 1000);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }
}

Cheers

If your first coordinate (sorted by y then x as you add hoizontally) and you have a 3 x 3, is 2,2 (first cell coordinates as 1,1) you need to do add(new JLabel(" ")) four times then add(new JLabel("*")) to produce the following (I will use "O" instead of an empty space so that you can actually see the effect)

-------
|O|O|O|
-------
|O|*| |
-------
| | | |
-------

As I said, there is no add(comp, x, y) (except with null, which is an anti-layout). There is an anlternative with GridBagLayout, but that is very hard to use. You need to add something to every cell. Otherwise, as I said, look into Canvas, GridBagLayout, or use a null layout (and Google for how to do that last as I will not recommend it), but with the first and third option, the x and y coordinates are in pixels, so have to remember to account for the size of the label being added.

Ok, so I've read through this and I don't see anywhere in the blog consideration of the error message you're getting. I'm not sure if this will help you out because I can't make much sense of your code, but the setLocation is an object/instance method so you have to invoke it on an object,

(object).setLocation(x, y)

in this case I think the object you're interested in is f.setLocation(x, y)

Ok, so I've read through this and I don't see anywhere in the blog consideration of the error message you're getting.

Then re-read the first reply.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.