Cancel table selection when escape key pressed - java

I'm writing a Swing project in Java and I've recently stumbled upon a problem.
I have a JTable full of objects (car park full of cars) and this piece of code changes the position of 2 elements. If no car was selected, set the coordinates of the first car. On next click, if there already was a car selected, set coordinates of another car. Next, swap the elements with each other and erase the coordinates.
Now, I also have to implement a possibility to "cancel" my selection, e.g. after selecting the first car, if a key is pressed, the selection should be erased. Any ideas how could I do it?
jt.addMouseListener(new java.awt.event.MouseAdapter() {
int y1 = -1;
int x1 = -1;
public void mouseReleased(java.awt.event.MouseEvent e) {
if(x1 == -1 && y1 == -1) {
y1 = jt.rowAtPoint(e.getPoint());
x1 = jt.columnAtPoint(e.getPoint());
}
else {
int y2 = jt.rowAtPoint(e.getPoint());
int x2 = jt.columnAtPoint(e.getPoint());
Car tmp = (Car)carpark[y1][x1];
carpark[y1][x1] = carpark[y2][x2];
carpark[y2][x2] = tmp;
model.fireTableDataChanged();
x1 = -1;
y1 = -1;
y2 = -1;
x2 = -1;
}
}
});

See How to Use Key Bindings.
Here is something to get you started:
InputMap im = table.getInputMap();
ActionMap am = table.getActionMap();
im.put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), "cancel");
am.put("cancel", new CancelAction());
CancelAction is defined by:
class CancelAction extends AbstractAction {
#Override
public void actionPerformed(ActionEvent e) {
System.out.println("esc button pressed ");
}
}

You need a GUI component, like a JTextField, that can register a KeyListener and program the proper action: see the documentation.
Something like:
JTextField tf = new JTextField();
tf.addKeyListener(
new KeyListener() {
void keyPressed(KeyEvent e) {
// your stuff here
}
// other methods must be overriden
}
);
You can register a key listener for other components like buttons and panels as well.

Related

How to know on which Label I clicked [Java]

I tried .equals() and ==, but nothing help. All labels I storage in ArrayList of my own class, which have JLabel.
How can I get the index of the label in ArrayList or something else?
Can my problem in using ArrayList?
MouseListener
private static MouseListener clicklvl1=new MouseListener() {
#Override
public void mouseClicked(MouseEvent e) {
for (int i=0;i<shahtars.size();i+=1){
if (e.getSource()==shahtars.get(i).uiShahtar){
IDofClickedObject=i;
}
}
if (IDofClickedObject!=-1){
if (counter == 0) {
shahtars.get(IDofClickedObject).uiShahtar.setIcon(new ImageIcon("E:\\aaa\\ShahtarLvL1(Clicked).png"));
counter = 1;
} else if (counter == 1) {
// uiShahtar.setIcon(new ImageIcon("E:\\aaa\\ShahtarLvL1.png"));
shahtars.get(IDofClickedObject).uiShahtar.setIcon(new ImageIcon("E:\\aaa\\ShahtarLvL1.png"));
counter = 0;
}
}
System.out.print("x "+shahtars.get(IDofClickedObject).uiShahtar.getX()+" y "+shahtars.get(IDofClickedObject).uiShahtar.getY());
}
My class
class FillShahtar implements Cloneable {
static JLabel uiShahtar;
static int energy;
static double power;
static double speed;
String name;
And the last
FillShahtar(int chose) {
switch (chose){
case 1:{
plankaDown = 1;
plankaUp = 11;
int xRand = (int) ( 0+Math.random()*1000);
int yRand =(int) (0+Math.random()*600);
int counter=0;
/////////////////////////debug/////////////////
System.out.print(xRand+" "+yRand+"\n");
//////////////////////////////////////////////
energy = (int) (plankaDown + Math.random() * plankaUp );
power = (int) (plankaDown + Math.random() * plankaUp );
speed = (int) (plankaDown + Math.random() * plankaUp );
uiShahtar = new JLabel();
uiShahtar.setIcon(new ImageIcon("E:\\aaa\\ShahtarLvL1.png"));
uiShahtar.setLayout(new FlowLayout());
uiShahtar.setSize(50,50);
uiShahtar.setLocation(xRand,yRand);
uiShahtar.setVisible(true);
uiShahtar.addMouseListener(clicklvl1);
// mainPanel.add(shahtars.get(counter).uiShahtar);
counter+=1;
break;
}
Clicked image must change the image, but change only last Label.
Since JLabel inherits from swing's Component class, you can add a MouseListener (or MouseAdapter) to each of your clickable labels. Using this EventListener you can find the clicked label like this:
JLabel l = new JLabel();
l.addMouseListener(new MouseAdapter() {
#Override
public void mouseReleased(MouseEvent e) {
JLabel clickedLabel = (JLabel) e.getComponent();
}
});
In order to get the index of the clicked label within the ArrayList, use the indexOf(Object) method provided by the ArrayList:
int index = list.indexOf(clickedLabel);

Java Swing: Mouse cursor misbehaves when held over a rectangle

I need to change the cursor while it moves over an array-list of rectangles by the contain(p) method.The problem is
My first algorithm to use an iterator to iterate through the rectangles doesn't work as expected.The cursor only changes when hovering over the first rectangle,in the other rectangles neither does it respond by showing the cursor changing nor indicate through the console that the cursor is hovering above them?!!
My second solution also refuses to work properly.I use a for loop to iterate over the rectangles, although the rectangles indicate through the console that the mouse is hovering above them, the cursor refuses to change with the exception of the last rectangle.
I use a JPanel in this SSCCE ,only because it reproduces the problem am encountering using a JTextPane...assuming my coding approach is what is in question.
I am thinking may be I may need to a thread to improve response and performance but not sure about the approach.Thanks in advance people.
public class UnstableCursor extends JPanel{
Rectangle2D rec;
ArrayList<Rectangle2D> recList = new ArrayList<>();
public UnstableCursor(){
}
public static void main(String[] args) {
UnstableCursor uc = new UnstableCursor();
JFrame frame = new JFrame();
Mover mv = new Mover(uc);
uc.addMouseListener(mv);
uc.addMouseMotionListener(mv);
JScrollPane jx = new JScrollPane(uc);
frame.getContentPane().add(jx);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500,500);
frame.setVisible(true);
}
#Override
public void paintComponent(Graphics g) {
super.paintComponents(g);
Graphics2D g2d = (Graphics2D)g;
int x = 5;
for(int i = 0;i < 4;i++){
g2d.setColor(Color.red);
rec = new Rectangle2D.Double(20,x,100,5);
g2d.draw(rec);
recList.add(rec);
x += 50;
}
System.out.println("RecList is: " +recList.size());
}
}
class Mover extends MouseInputAdapter{
UnstableCursor uc;
Rectangle2D rec;
ArrayList<Rectangle2D> reList;
public Mover(UnstableCursor ucc){
uc = ucc;
}
#Override
public void mouseDragged(MouseEvent e) {
super.mouseDragged(e);
Point p = e.getPoint();
System.out.println("xxxx");
}
#Override
public void mouseMoved(MouseEvent e) {
Point p = e.getPoint();
reList = uc.recList;
//System.out.println("List is: "+reList.size());
Iterator <Rectangle2D> recs = reList.iterator();
//--------------------- First Algorithm ----------------------//
if(recs.hasNext()){
rec = recs.next();
if(rec.contains(p)){
System.out.println("inside the rectangle....");
uc.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
}
else{
uc.setCursor(Cursor.getDefaultCursor());
}
}
//--------------------- Second Algorithm ---------------------//
int r = 0;
for(int i = 0;i<(reList.size());i++){
rec = reList.get(r);
//System.out.println("Rect No: "+r+"X: "+rec.getX()+"Y: "+rec.getY());
r++;
if(rec.contains(p)){
System.out.println("inside the rectangle....");
uc.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
}
else{
uc.setCursor(Cursor.getDefaultCursor());
}
}
}
}
the cursor refuses to change with the exception of the last rectangle.
Your basic search algorithm is wrong. Once you find a rectangle that contains the point you should set the cursor and break out of the loop, otherwise the next rectangle you check will not be a match and the cursor will be reset again.
Also...
for(int i = 0;i < 4;i++){
g2d.setColor(Color.red);
rec = new Rectangle2D.Double(20,x,100,5);
g2d.draw(rec);
recList.add(rec);
x += 50;
}
... A painting method is for painting only.
You should NOT be creating rectangles and adding them to the array, since the paintComponent() method is continually called when Swing determines the panel needs to be repainted.
The rectangles should be added to the List in the constructor of your class so each rectangle is only added once.

Java to C# conversion. How do i draw a rectangle on my bitmap?

Firstly, i am a complete noob at both C# and Java.
So i have been given this assignment to convert a java applet into C#, i have managed to do everything apart from drawing a rectangle on the screen via drag and drop using mouse events.
Whats supposed to happen is when i click and drag my mouse across the screen a rectangle with no fill and white border should appear. The code i have below is just a white screen with a red cross through it, if i comment out the if(action) statement in the form1_Paint then it works but no rectangle so it must be that code that messing it up.
http://gyazo.com/b2506b8c2ea9b304e34172c42ce98aab <-- what it should look like
http://gyazo.com/a8764ac9f5380f0109623d7a7750ddb6 <-- what it actually looks like
[update]
I have now got a rectangle do display but it happens on the MouseUp event rather than creating it as i am dragging my mouse. The obvious next step was to move it to a different mouse event like mouseMove but then it really messes up and created rectangles constantly as i make it bigger. How can i make it constantly resize the rectangle as i drag my mouse and not keep creating rectangles constantly?
The code
private void Form1_Paint(object sender, PaintEventArgs e)
{
Graphics g1 = e.Graphics;
g1.DrawImage(bitmap, 0, 0, x1, y1);
}
//added load method
private void Form1_Load(object sender, EventArgs e)//runs functions on load
{
init();
start();
}
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
if (action)
{
xe = e.X;
ye = e.Y;
}
}
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
action = true;
// e.consume();
xs = xe = e.X;
ys = ye = e.Y; // starting point y
Form1_MouseMove(sender, e);
this.Invalidate();
}
private void Form1_MouseUp(object sender, MouseEventArgs e)
{
using (Graphics g = this.CreateGraphics())
{
Pen pen = new Pen(Color.White);
g.DrawRectangle(pen, xs, ys, Math.Abs(xs - xe), Math.Abs(ys - ye));
}
int z, w;
//e.consume();
//xe = e.X;
//ye = e.Y;
if (xs > xe)
{
z = xs;
xs = xe;
xe = z;
}
if (ys > ye)
{
z = ys;
ys = ye;
ye = z;
}
w = (xe - xs);
z = (ye - ys);
if ((w < 2) && (z < 2)) initvalues();
else
{
if (((float)w > (float)z * xy)) ye = (int)((float)ys + (float)w / xy);
else xe = (int)((float)xs + (float)z * xy);
xende = xstart + xzoom * (double)xe;
yende = ystart + yzoom * (double)ye;
xstart += xzoom * (double)xs;
ystart += yzoom * (double)ys;
}
xzoom = (xende - xstart) / (double)x1;
yzoom = (yende - ystart) / (double)y1;
mandelbrot();
this.Invalidate();
//Repaint();
}
The biggest problem in your code is this statement in the Form1_Paint() method:
g1.Dispose();
You should never be disposing the Graphics instance passed to you. It belongs to the framework, not your code. But you should especially never dispose an object that you plan to use later. When you dispose it here, then the Graphics instance isn't valid later on when you try to draw the rectangle.
Note that this is the same as in Java. I hope the original Java code didn't call Graphics.dispose() too!
Some other suggestions:
when creating a new Pen object, add a using statement to ensure the Pen instance you create is disposed properly (you do own that one! :) ). In this case though, you don't need to create a new Pen object...just use the stock Pen provided by .NET. I.e. Pens.White.
you don't appear to be calling Invalidate() in the MouseDown and MouseMove event handlers. You won't get any visual feedback unless you do that, because the Paint event handler won't be called.
Fix the code so it looks like this:
// Little helper method :)
private static void Swap<T>(ref T t1, ref T t2)
{
T temp = t1;
t1 = t2;
t2 = t1;
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
Graphics g1 = e.Graphics;
g1.DrawImage(bitmap, 0, 0, x1, y1);
if (action)
{
//g.setColor(Color.White);
if (xe < xs)
{
Swap(ref xs, ref xe);
}
if (ye < ys)
{
Swap(ref ys, ref ye);
}
g1.DrawRectangle(Pens.White, xs, ys, (xe - xs), (ye - ys));
}
}
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
// e.consume();
if (action)
{
xe = e.X;
ye = e.Y;
Invalidate();
//repaint();
}
}
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
action = true;
// e.consume();
if (action)
{
xs = xe = e.X;
ys = ye = e.Y;
Invalidate();
}
}
I've had a look here, this doesn't seem to fix my problem, the Invalidate();'s make it stutter and when I have it in Form1_Paint it doesn't draw correctly, either draws before straight onto the form, straight after I've zoomed but doesn't actually appear when I'm dragging in my zoom!

Update new position for Jlabel

I'm new to Java and I am having some trouble with my assignment.
I have a Panel containing 100 JLabels:
for(int i=0;i<100;i++)
{
num[i] = new JLabel(""+i, JLabel.CENTER);
mainPanel.add(num[i]);
}
And a button to set image icon for the label when clicked
public void actionPerformed(ActionEvent ae)
{
int a = ran.nextInt(6) +1;//random number
int b +=a;
if(b>=100)
{
b=99;
num[b].setIcon(icon);
}
else
{
num[b].setIcon(icon);
}
}
How can i remove the icon from the last position and update it to a new position?
You can try to remember the index of the array of the label, for which you tried to set the icon.
For example-
int b = 0; // make b an instance variable
public void actionPerformed(ActionEvent ae)
{
int a = ran.nextInt(6) +1;//random number
num[b].setIcon(null); //remove the icon from from previously set label
b=a; //since b already has some value, b+=a might create unexpected result, hence just assigned a
if(b>=100)
{
b=99;
num[b].setIcon(icon);
}
else
{
num[b].setIcon(icon);
}
}

Issue setting instance variables

I am passing some coordinates from one class to another using the following code
**edited to show more detail
At start of class1:
public class BattleGui implements ActionListener {
private int xCoordinate;
private int yCoordinate;
public void coordinateHandler(int xCoord, int yCoord) {
xCoordinate=xCoord;
yCoordinate=yCoord;
System.out.println("Coordinates Received "+xCoord + " " +yCoord);
System.out.println("Test "+xCoordinate + " " +yCoordinate);
}
public void actionPerformed(ActionEvent e) {
String classname = getClassName(e.getSource());
JComponent component = (JComponent)(e.getSource());
cellState cs = new cellState();
if (classname.equals("JMenuItem")) {
JMenuItem menusource = (JMenuItem)(e.getSource());
String menutext = menusource.getText();
// Determine which menu option was chosen
if (menutext.equals("Load Game")) {
/* BATTLEGUI Add your code here to handle Load Game **********/
System.out.println(cs.turnFeedback());
LoadGame();
}
else if (menutext.equals("Save Game")) {
/* BATTLEGUI Add your code here to handle Save Game **********/
SaveGame();
}
else if (menutext.equals("New Game")) {
/* BATTLEGUI Add your code here to handle Save Game **********/
NewGame();
}
}
// Handle the event from the user clicking on a command button
else if (classname.equals("JButton")) {
JButton button = (JButton)(e.getSource());
int bnum = Integer.parseInt(button.getActionCommand());
int row = bnum / GRID_SIZE;
int col = bnum % GRID_SIZE;
//col=yCoord;
//row=xCoord;
//System.out.println(e.getSource());
//System.out.println(bnum / GRID_SIZE);
fireShot(row, col);
if (row==xCoordinate){
if (col==yCoordinate){
button.setBackground(cellColour[cs.turnFeedback()]);
}
else {
//Remember to change the 1 to whatever is reported back from cell class cell state
//button.setBackground(cellColour[cs.turnFeedback()]);
button.setBackground(Color.BLUE);
}
}
else {
button.setBackground(Color.BLUE);
}
}
}
From class2:
public void shipDeploy() {
int gridLength;
int lengthDraw;
int winningNumbers = 0;
int xCoord;
int yCoord;
xCoord=99;
yCoord=100;
System.out.println(xCoord + "\n" + yCoord);
BattleGui bGui = new BattleGui();
//Passing the coordinates back to the battlegui coordinate handler class
bGui.coordinateHandler(xCoord, yCoord);
}
This passes these two values to a coordinate handler method within the first class.
within this class I have an xCoordinate variable used throughout a variety of methods, the problem is that I dont seem to be able to set this, 0 is always being returned outside of this method for xCoordinate and yCoordinate and I dont understand why, as they seem to be ok in the line System.out.println("Test "+xCoordinate + " " +yCoordinate); above.
just figured it out on my own, quite simple really, actionlistener was basically re-instantiating the variable on every "event" explaining the zero values, as soon as i took this process outside of the listener it worked and set the values as intended. thanks for the input guys and hope this helps someone along the way!

Categories