Java Swing and Microsoft Excel
How do you open a Microsoft Excel file from swing?
please give a example.
Hi,
try these APIs:
[http://poi.apache.org/]
[http://jexcelapi.sourceforge.net/]
Once you get the content of the files, it's up to you how you display it in your swing application.
Demos and tutorials can be found on these sites as well.
Similar Messages
-
WebUtil and Microsoft Excel: Setting cell border properties
I'm using the CLIENT_OLE2 package shipped with WebUtil to create a Microsoft Excel spreadsheet from an Oracle 10g Form and, so far, have managed to:
- Create multiple worksheets
- Populate cells with values and formulae
- Format cells, including setting font name and size, setting bold italic and underline attributes
The final requirement is to set a border on a cell or group of cells, but this is where I'm stumped. My code thus far looks like this:
DECLARE
l_application CLIENT_OLE2.OBJ_TYPE ;
l_workbooks CLIENT_OLE2.OBJ_TYPE ;
l_workbook CLIENT_OLE2.OBJ_TYPE ;
l_worksheets CLIENT_OLE2.OBJ_TYPE ;
l_worksheet CLIENT_OLE2.OBJ_TYPE ;
l_cell CLIENT_OLE2.LIST_TYPE ;
l_borders CLIENT_OLE2.OBJ_TYPE ;
BEGIN
l_application := CLIENT_OLE2.CREATE_OBJ('Excel.Application') ;
l_workbooks := CLIENT_OLE2.GET_OBJ_PROPERTY(l_application,'Workbooks') ;
l_workbook := CLIENT_OLE2.GET_OBJ_PROPERTY(l_workbooks,'Add') ;
l_worksheets := CLIENT_OLE2.GET_OBJ_PROPERTY(l_workbook,'Worksheets') ;
l_arguments := CLIENT_OLE2.CREATE_ARGLIST ;
CLIENT_OLE2.ADD_ARG(l_arguments,'Sheet1') ;
l_worksheet := CLIENT_OLE2.GET_OBJ_PROPERTY(l_workbook,'Worksheets',l_arguments) ;
CLIENT_OLE2.DESTROY_ARGLIST(l_arguments) ;
-- Select cell A1
l_arguments := CLIENT_OLE2.CREATE_ARGLIST ;
CLIENT_OLE2.ADD_ARG(l_arguments,1) ;
CLIENT_OLE2.ADD_ARG(l_arguments,1) ;
l_cell := CLIENT_OLE2.GET_OBJ_PROPERTY(l_worksheet,'Cells',l_arguments) ;
CLIENT_OLE2.DESTROY_ARGLIST(l_arguments) ;
l_borders := CLIENT_OLE2.GET_OBJ_PROPERTY(p_cells,'Borders') ;
-- What happens next...?
-- Clean up
CLIENT_OLE2.RELEASE_OBJ(l_borders) ;
CLIENT_OLE2.RELEASE_OBJ(l_worksheet) ;
CLIENT_OLE2.RELEASE_OBJ(l_worksheets) ;
CLIENT_OLE2.RELEASE_OBJ(l_workbook) ;
CLIENT_OLE2.RELEASE_OBJ(l_workbooks) ;
CLIENT_OLE2.RELEASE_OBJ(l_application) ;
END ;I'd be obliged for a pointer in the right direction!Well, in spite of 80-odd views, it looks like I've answered my own question.
The borders around a range of cells in Excel are actually separate elements of the Borders object. You need to specify which border you want and set it individually. The code below draws a border around cells A1 to C3. Note the constants defined at the top of the listing; these are the "actual" values of the corresponding Excel constants that are referenced in the VBA code if you draw the border by hand while recording a macro.
Enjoy!
DECLARE
c_automatic CONSTANT NUMBER := -4105 ; -- ColorIndex = xlAutomatic
c_thin CONSTANT NUMBER := 2 ; -- Weight = xlThin
c_medium CONSTANT NUMBER := -4138 ; -- Weight = xlMedium
c_thick CONSTANT NUMBER := 4 ; -- Weight = xlThick
c_continuous CONSTANT NUMBER := 1 ; -- LineStyle = xlContinuous
c_edge_left CONSTANT NUMBER := 7 ; -- Border = xlEdgeLeft
c_edge_top CONSTANT NUMBER := 8 ; -- Border = xlEdgeTop
c_edge_bottom CONSTANT NUMBER := 9 ; -- Border = xlEdgeBottom
c_edge_right CONSTANT NUMBER := 10 ; -- Border = xlEdgeRight
l_application CLIENT_OLE2.OBJ_TYPE ;
l_workbooks CLIENT_OLE2.OBJ_TYPE ;
l_workbook CLIENT_OLE2.OBJ_TYPE ;
l_worksheets CLIENT_OLE2.OBJ_TYPE ;
l_worksheet CLIENT_OLE2.OBJ_TYPE ;
l_range CLIENT_OLE2.LIST_TYPE ;
PROCEDURE draw_border (
p_range IN CLIENT_OLE2.LIST_TYPE,
p_side IN NUMBER,
p_weight IN NUMBER)
IS
l_edge CLIENT_OLE2.LIST_TYPE ;
l_border CLIENT_OLE2.OBJ_TYPE ;
BEGIN
l_edge := CLIENT_OLE2.CREATE_ARGLIST ;
CLIENT_OLE2.ADD_ARG(l_edge,p_side) ;
l_border := CLIENT_OLE2.GET_OBJ_PROPERTY(l_range,'Borders',l_edge) ;
CLIENT_OLE2.DESTROY_ARGLIST(l_edge) ;
CLIENT_OLE2.SET_PROPERTY(l_border,'LineStyle',c_continuous) ;
CLIENT_OLE2.SET_PROPERTY(l_border,'Weight',p_weight) ;
CLIENT_OLE2.SET_PROPERTY(l_border,'ColorIndex',c_automatic) ;
CLIENT_OLE2.RELEASE_OBJ(l_border) ;
END draw_border ;
BEGIN
l_application := CLIENT_OLE2.CREATE_OBJ('Excel.Application') ;
l_workbooks := CLIENT_OLE2.GET_OBJ_PROPERTY(l_application,'Workbooks') ;
l_workbook := CLIENT_OLE2.GET_OBJ_PROPERTY(l_workbooks,'Add') ;
l_worksheets := CLIENT_OLE2.GET_OBJ_PROPERTY(l_workbook,'Worksheets') ;
l_arguments := CLIENT_OLE2.CREATE_ARGLIST ;
CLIENT_OLE2.ADD_ARG(l_arguments,'Sheet1') ;
l_worksheet := CLIENT_OLE2.GET_OBJ_PROPERTY(l_workbook,'Worksheets',l_arguments) ;
CLIENT_OLE2.DESTROY_ARGLIST(l_arguments) ;
-- Select the box with top-left of A1 and bottom-right of C3.
l_arguments := CLIENT_OLE2.CREATE_ARGLIST ;
CLIENT_OLE2.ADD_ARG(l_arguments,'A1:C3') ;
l_range := CLIENT_OLE2.GET_OBJ_PROPERTY(p_worksheet,'Range',l_arguments) ;
CLIENT_OLE2.DESTROY_ARGLIST(l_arguments) ;
-- Draw border along the left edge of cells in range
draw_border(l_range,c_edge_left,c_thick) ;
-- Draw border along the top edge of cells in range
draw_border(l_range,c_edge_top,c_thick) ;
-- Draw border along the right edge of cells in range
draw_border(l_range,c_edge_right,c_thick) ;
-- Draw border along the bottom edge of cells in range
draw_border(l_range,c_edge_bottom,c_thick) ;
-- Clean up
CLIENT_OLE2.RELEASE_OBJ(l_range) ;
CLIENT_OLE2.RELEASE_OBJ(l_worksheet) ;
CLIENT_OLE2.RELEASE_OBJ(l_worksheets) ;
CLIENT_OLE2.RELEASE_OBJ(l_workbook) ;
CLIENT_OLE2.RELEASE_OBJ(l_workbooks) ;
CLIENT_OLE2.RELEASE_OBJ(l_application) ;
END ;It's worth pointing out that the code above has been culled from my specific procedure and has been simplified. It hasn't been tested, although the DRAW_BORDER nested procedure has been copied straight from working code. -
Lost program icons Microsoft word 2008 and Microsoft excel 2008
Hi I accidently turned Microsoft excel 2008 and microsoft word 2008 into wads of trash that disappeared when I accidnetly clicked the icons and moved them from the dock into the internet box. I talked to one person already but can't seem to get back to them and he suggested that I use MIcrosoft Office 2008 but hte problem is is that I don't have it anymore because I deleted the program from my computer a few days ago (yesterday or the day before) I had originally bought hte program from Best Buy and downloaded it from a disc but I can't find the disc anymore. Do you know what I can do to recover the programs I checked the applications, office (of course, and word and excel 2008 aren't there and they aren't in the trash I checked there too. I thought that it might be on my back-up time machine? And I also couldn't go to undo to change what happened. Thank you.
Things just don't disappear. If you didn't put them in the trash, they must be there somewhere. Click the magnifying glass at the top right side of your screen and search for Microsoft Office and see if you can locate the folder. Also, I'm wondering if you actually installed office or if you were just running it from the downloaded .dmg file? When doing the search, keep an eye open for a Microsoft Office 2008 DMG file.
-
Is it possible to create flash components (swf files) and include them in a Swing JFrame and be able to post data from and to it.
For example, if the swf gui is showing a list of items, the list is coming from the Java application and then the SWF file gets included in a Swing JFrame. When user chooses an item from the list and clicks a button then the results is passed back to the Java application.
Has anyone done something similar to this? How is this possible? Code samples would be appreciated.May be you can try out JFlash ...
https://jflash.dev.java.net/
They support only upto Flash 2 player... and it is still under development i guess... -
Java Swing and Socket Programming
I am making a Messenger like yahoo Messenger using Swing and Socket Programming ,Multithreading .
Is this techology feasible or i should try something else.
I want to display my messenger icon on task bar as it comes when i install and run Yahoo Messenger.
Which class i should use.I don't really have an answer to what you are asking. But I am developing the same kind of application. I am using RMI for client-server and server-server (i have distributed servers) communication and TCP/IP for client-client. So may be we might be able to help each other out. My email id is [email protected]
Are you opening a new socket for every conversation? I was wondering how to multithread a socket to reuse it for different connections, if it is possible at all.
--Poonam. -
Java Swing and windows listview control
Hi,
many programming languages have the listview control, to which you can define diferents kinds of data's organizers as Icon, Detail, List, etc.
Somebody knows if java swing have a component as the listview ?
thanksYes. It's called JList. Have a look at the tutorial:
http://java.sun.com/docs/books/tutorial/uiswing/index.html -
Error with Oracle BI Publisher and Microsoft Excel
Hi,
I am using Oracle BI 10.1.3.3.0 in Windows XP. Whenever I am clicking on Login of Oracle BI Publisher Menu of Microsoft Excel, it is throwing me following error
Error: Could not get server version: Invalid procedure call or argument.
Everything is fine with MS Word. I have installed Analyzer for Excel Tool provided with th Oracle BI version mentioned above. I am having network installation for MS Office 2003. Can anybody give a solution please.
Thanks in advance
RajithHi,
I am having the same problem. Any answers?
Regards. -
Java swing and threading issue
i am trying to update swing components from some thread. at my first try, i noticed this was a disaster. upon further research, i found out about the single-threaded model of swing events (i.e. event dispatching thread).
i did more research and could NOT find a good example on how to update swing components from other threads. many examples were showing too much.
can someone post a simple example on here? i just want to see how to properly update a swing component from a non-swing class using threading.I think its a simple example:
http://forum.java.sun.com/thread.jspa?forumID=57&threadID=621226
Let us know what you think. -
Java Swing and WIndows User Localles
HI,
We need to develop a java applet/application which can be switchable at run time to accept and display characters in multilanaguages. This java applet will run on a windows 2000 server with multilanguage pack installed. Is it possible to dynamically switch languages at run time. The machines default localle and operating system installation is US English.
We have tried some tests with traditional chinese and UI components in Swing but with no success in displaying or inputing valid chinese characters. Has anyone attempted a simliar thing and have any insights that may help.
Thanks in Advance,
JonoYes. It's called JList. Have a look at the tutorial:
http://java.sun.com/docs/books/tutorial/uiswing/index.html -
Hi there,
I'm testing a Data Source connection from Crystal to Excel. My error is
"Unknown Database Connector Error".
Any thoughts other than I need to have a full version of Crystal?
Thanks,
gregLanny,
I get exactly the same message when trying to open Microsoft Word (Mac version) documents emailed as attachments to my iPhone, so your problem is not Excel-specific. So far, Apple blames my telco, who blames my ISP, who blames Apple!
I did see somewhere on the net that attachments will not come down if you have your email sender (Mail in my case) set to Rich Text instead of plain text. However, that does not seem to be the issue. I still get this problem, regards of the format setting. The attachments seem to have been downloaded OK but cannot be opened.
Sorry I can't be more help but please let me know if you find the solution.
Michael -
My boss would like me to use data merge to create a catalog with 300+ pages and unique data fields on almost every page. It is an informational catalog that would contain pictures and several unique fitment and specification fields for each product that we sell and manufacture on each page. For years the catalog was made and modified manually in quark express. Is it possible to use data merge to recreate such a complex document? Does anyone have any useful reccomendations or links for tackling this project? I really appreciate any advice or help.
Thank You,
Kevin
Message was edited by: kpalombiOnline video
http://tv.adobe.com/watch/instant-indesign/automating-a-catalog-with-data-merge/
Software
http://www.65bit.com/home/home.shtm
Data Merge Tips
http://www.theindesigner.com/blog/episode-43-data-merge-video -
Java Swing/AWT and FX is so old school! Give me HTML and CSS for GUI!
Dear Java,
I am a seasoned programmer and I feel it's time JAVA implements a GUI system where it uses HTML and CSS for the GUI. For the love of god just look at the interfaces you can make using HTML and CSS alone. I am a big fan of Java Swing and the recent GUI designer for FX is quite cool. But they are just not as simple as HTML and CSS. And JavaFX has some interesting requirements for the graphics.
I know it is possible to use JavaFX and implement the WebView/WebDriver and make it load a HTML page, etc... but why go through all the trouble?
Just imagine... if you make Java where it has powerful back-end to do what it does best and the HTML/CSS powered GUI on the front-end. It will make the lives of many developers much much easier.
I am not sure whether a Swing designed GUI will be faster than a HTML designed GUI... but if you look at a traditional browser and how fast it renders HTML/CSS, I am sure if Java had a native Form where it uses HTML and CSS to render the GUI, Java will make the dreams of many programmers a reality.
Make it happen!!!!Check this i solve problem just now using this
https://wiki.archlinux.org/index.php/Ja … ow_Manager -
How to store grid points in a file using Java Swing?
Please someone help me with any suggestions about how to store the grid points in a file using Java Swing
Actually i have designed a gridlayout in Java Swing and have added some components to it such as buttons or images....My problem is when I click on any of the cell of the grid,the corresponding cell number should be stored in an external file....Do u have any suggestions on how to do it?
-
Chess: java swing help need
Hi, I have just learn java swing and now creating a chess game. I have sucessfully create a board and able to display chess pieces on the board. By using JLayeredPane, I am able to use drag and drop for the piece. However, there is a problem which is that I can drop the chess piece off the board and the piece would disappear. I have try to solve this problem by trying to find the location that the chess piece is on, store it somewhere and check if the chess piece have been drop off bound. However, I could not manage to solve it. Also, I am very confuse with all these layer thing.
Any suggestion would be very helpful. Thanks in advance
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
public class ChessBoard extends JFrame implements MouseListener, MouseMotionListener
Object currentPosition;
Color currentColor;
Dimension boardSize = new Dimension(600, 600);
JLayeredPane layeredPane;
JPanel board;
JPanel box;
JLabel chessPiece;
int xAdjustment;
int yAdjustment;
public ChessBoard()
// Use a Layered Pane for this this application
layeredPane = new JLayeredPane();
getContentPane().add(layeredPane);
layeredPane.setPreferredSize( boardSize );
layeredPane.addMouseListener( this );
layeredPane.addMouseMotionListener( this );
// Add a chess board to the Layered Pane
board = new JPanel();
// set "board" to the lowest layer (default_layer)
layeredPane.add(board, JLayeredPane.DEFAULT_LAYER);
board.setLayout( new GridLayout(8, 8) );
board.setPreferredSize( boardSize );
board.setBounds(0, 0, boardSize.width, boardSize.height);
addShade();
addPiece();
public void addShade()
for (int i = 0; i < 64; i++)
box = new JPanel( new BorderLayout() );
board.add( box, BorderLayout.CENTER );
if( ((i / 8) % 2) == 0)
if( (i % 2) ==0)
box.setBackground(Color.lightGray);
} else {
box.setBackground( Color.white);
} else {
if( (i % 2) ==0)
box.setBackground(Color.white);
} else {
box.setBackground(Color.lightGray);
//Method for adding chess pieces to the board
public void addPiece()
// Add a few pieces to the board
//black pieces
for (int i = 0; i < 64; i++)
//adding black pieces
if (i < 8)
String fileName = i + ".gif";
JLabel blackPieces = new JLabel( new ImageIcon( fileName ) );
JPanel panel = (JPanel)board.getComponent( i );
panel.add( blackPieces );
//adding black pones
if ((i > 7) && (i < 16))
String fileName = "8.gif";
JLabel blackPones = new JLabel( new ImageIcon( fileName ) );
JPanel panel = (JPanel)board.getComponent( i );
panel.add( blackPones );
//jump to white position (Component 48)
if (i == 16) i = 48;
//adding white pones
if(( i > 47 ) && (i < 56))
JLabel whitePones = new JLabel( new ImageIcon("48.gif") );
JPanel panel = (JPanel)board.getComponent( i );
panel.add( whitePones );
//adding white pieces
if (i > 55)
String fileName = i + ".gif";
JLabel whitePieces = new JLabel( new ImageIcon( fileName ) );
JPanel panel = (JPanel)board.getComponent( i);
panel.add( whitePieces );
** Add the selected chess piece to the dragging layer so it can be moved
public void mousePressed(MouseEvent e)
chessPiece = null;
Component c = board.findComponentAt(e.getX(), e.getY());
c.setBackground(Color.red);
if (c instanceof JPanel) return;
Point parentLocation = c.getParent().getLocation();
xAdjustment = parentLocation.x - e.getX();
yAdjustment = parentLocation.y - e.getY();
chessPiece = (JLabel)c;
chessPiece.setLocation(e.getX() + xAdjustment, e.getY() + yAdjustment);
chessPiece.setSize(chessPiece.getWidth(), chessPiece.getHeight());
layeredPane.add(chessPiece, JLayeredPane.DRAG_LAYER);
** Move the chess piece around
public void mouseDragged(MouseEvent me)
if (chessPiece == null) return;
chessPiece.setLocation(me.getX() + xAdjustment, me.getY() + yAdjustment);
** Drop the chess piece back onto the chess board
public void mouseReleased(MouseEvent e)
Component c = board.findComponentAt(e.getX(), e.getY());
int x = (int)c.getBounds().getX();
int y = (int)c.getBounds().getY();
System.out.println(c.getLocation());
System.out.println(x + " "+ y);
c.setBackground(currentColor);
if (chessPiece == null) return;
chessPiece.setVisible(false);
if (c instanceof JLabel)
Container parent = c.getParent();
//remove the piece that is capture
parent.remove(0);
parent.add( chessPiece );
else
Container parent = (Container)c;
parent.add( chessPiece );
chessPiece.setVisible(true);
public void mouseClicked(MouseEvent e) {}
public void mouseMoved(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
private static void createAndShowGUI()
//Make sure we have nice window decorations
JFrame.setDefaultLookAndFeelDecorated(true);
JFrame frame = new ChessBoard();
frame.setDefaultCloseOperation( JFrame.DISPOSE_ON_CLOSE );
//Display the window
frame.pack();
frame.setResizable( false );
frame.setLocationRelativeTo( null );
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();
}import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
public class ChessBoardRx extends JFrame implements MouseListener, MouseMotionListener
Object currentPosition;
Color currentColor;
Dimension boardSize = new Dimension(600, 600);
JLayeredPane layeredPane;
JPanel board;
JPanel box;
JLabel chessPiece;
int xAdjustment;
int yAdjustment;
Container lastParent;
public ChessBoardRx()
// Use a Layered Pane for this this application
layeredPane = new JLayeredPane();
getContentPane().add(layeredPane);
layeredPane.setPreferredSize( boardSize );
layeredPane.addMouseListener( this );
layeredPane.addMouseMotionListener( this );
// Add a chess board to the Layered Pane
board = new JPanel();
// set "board" to the lowest layer (default_layer)
layeredPane.add(board, JLayeredPane.DEFAULT_LAYER);
board.setLayout( new GridLayout(8, 8) );
board.setPreferredSize( boardSize );
board.setBounds(0, 0, boardSize.width, boardSize.height);
addShade();
addPiece();
// populateBoard();
public void addShade()
for (int i = 0; i < 64; i++)
box = new JPanel( new BorderLayout() );
board.add( box, BorderLayout.CENTER );
if( ((i / 8) % 2) == 0)
if( (i % 2) ==0)
box.setBackground(Color.lightGray);
} else {
box.setBackground( Color.white);
} else {
if( (i % 2) ==0)
box.setBackground(Color.white);
} else {
box.setBackground(Color.lightGray);
//Method for adding chess pieces to the board
public void addPiece()
// Add a few pieces to the board
//black pieces
for (int i = 0; i < 64; i++)
//adding black pieces
if (i < 8)
String fileName = i + ".gif";
JLabel blackPieces = new JLabel( new ImageIcon( fileName ) );
JPanel panel = (JPanel)board.getComponent( i );
panel.add( blackPieces );
//adding black pones
if ((i > 7) && (i < 16))
String fileName = "8.gif";
JLabel blackPones = new JLabel( new ImageIcon( fileName ) );
JPanel panel = (JPanel)board.getComponent( i );
panel.add( blackPones );
//jump to white position (Component 48)
if (i == 16) i = 48;
//adding white pones
if(( i > 47 ) && (i < 56))
JLabel whitePones = new JLabel( new ImageIcon("48.gif") );
JPanel panel = (JPanel)board.getComponent( i );
panel.add( whitePones );
//adding white pieces
if (i > 55)
String fileName = i + ".gif";
JLabel whitePieces = new JLabel( new ImageIcon( fileName ) );
JPanel panel = (JPanel)board.getComponent( i);
panel.add( whitePieces );
private void populateBoard() {
int[][] pos = {
{ 9, 7, 5, 1, 3, 5, 7, 9 },
{ 8, 6, 4, 0, 2, 4, 6, 8 }
for(int j = 0; j < 8; j++) {
String fileName = "chessImages/" + pos[0][j] + ".jpg";
JLabel label = new JLabel( new ImageIcon( fileName ) );
JPanel panel = (JPanel)board.getComponent( j );
panel.add( label );
for(int j = 8; j < 16; j++) {
String fileName = "chessImages/" + 11 + ".jpg";
JLabel label = new JLabel( new ImageIcon( fileName ) );
JPanel panel = (JPanel)board.getComponent( j );
panel.add( label );
for(int j = 56, k = 0; j < 64; j++, k++) {
String fileName = "chessImages/" + pos[1][k] + ".jpg";
JLabel label = new JLabel( new ImageIcon( fileName ) );
JPanel panel = (JPanel)board.getComponent( j );
panel.add( label );
for(int j = 48; j < 56; j++) {
String fileName = "chessImages/" + 10 + ".jpg";
JLabel label = new JLabel( new ImageIcon( fileName ) );
JPanel panel = (JPanel)board.getComponent( j );
panel.add( label );
** Add the selected chess piece to the dragging layer so it can be moved
public void mousePressed(MouseEvent e)
chessPiece = null;
Component c = board.findComponentAt(e.getX(), e.getY());
c.setBackground(Color.red);
if (c instanceof JPanel) return;
lastParent = c.getParent();
Point parentLocation = c.getParent().getLocation();
xAdjustment = parentLocation.x - e.getX();
yAdjustment = parentLocation.y - e.getY();
chessPiece = (JLabel)c;
chessPiece.setLocation(e.getX() + xAdjustment, e.getY() + yAdjustment);
chessPiece.setSize(chessPiece.getWidth(), chessPiece.getHeight());
layeredPane.add(chessPiece, JLayeredPane.DRAG_LAYER);
** Move the chess piece around
public void mouseDragged(MouseEvent me)
if (chessPiece == null) return;
chessPiece.setLocation(me.getX() + xAdjustment, me.getY() + yAdjustment);
** Drop the chess piece back onto the chess board
public void mouseReleased(MouseEvent e)
Component c = board.findComponentAt(e.getX(), e.getY());
if(c == null) {
layeredPane.remove(chessPiece);
lastParent.add(chessPiece);
Rectangle r = lastParent.getBounds();
chessPiece.setLocation(r.x+xAdjustment, r.y+yAdjustment);
lastParent.validate();
lastParent.repaint();
return;
int x = (int)c.getBounds().getX();
int y = (int)c.getBounds().getY();
System.out.println(c.getLocation());
System.out.println(x + " "+ y);
c.setBackground(currentColor);
if (chessPiece == null) return;
chessPiece.setVisible(false);
if (c instanceof JLabel)
Container parent = c.getParent();
//remove the piece that is capture
parent.remove(0);
parent.add( chessPiece );
else
Container parent = (Container)c;
parent.add( chessPiece );
chessPiece.setVisible(true);
public void mouseClicked(MouseEvent e) {}
public void mouseMoved(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
private static void createAndShowGUI()
//Make sure we have nice window decorations
JFrame.setDefaultLookAndFeelDecorated(true);
JFrame frame = new ChessBoardRx();
frame.setDefaultCloseOperation( JFrame.DISPOSE_ON_CLOSE );
//Display the window
frame.pack();
frame.setResizable( false );
frame.setLocationRelativeTo( null );
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();
} -
Chess: Java Swing problem!! Help need
Hi, I have just learn java swing and now creating a chess game. I have sucessfully create a board and able to display chess pieces on the board. By using JLayeredPane, I am able to use drag and drop for the piece. However, there is a problem which is that I can drop the chess piece off the board and the piece would disappear. I have try to solve this problem by trying to find the location that the chess piece is on, store it somewhere and check if the chess piece have been drop off bound. However, I could not manage to solve it. Also, I am very confuse with all these layer thing.
Any suggestion would be very helpful. Thanks in advanceI wonder if you are biting off a little more than you can chew. This seems like a big project for a beginning coder. Anyway, my suggestions can only be general since we only have general information:
1) Break the big problem down into little pieces. Get the pieces working, then put them together.
2) Make sure your program logic is separate from your GUI.
3) If still having problems, post an SSCCE. See this link to tell you how:
http://homepage1.nifty.com/algafield/sscce.html
4) And as camikr would say (and flounder just said): next time, post this in the Swing forum.
Good luck!
Maybe you are looking for
-
How to connect Hard Disk with my MacBookPro
Hello Everyone, I have recently bought MacBookPro, But i am unable to transfer the data from MAcBook to Hard Disk... Can anyone please help??
-
I've loaded about 100 photos into a slideshow that I would like to burn into a DVD to share with friends at a 50th birthday celebration. It keeps going black at a certain point but I can see the pictures in the cue. Any ideas on how to correct this a
-
Batch IDOCs to single message in XI
Hi Experts, I have situation wherein I am getting a batch IDOCs of same type and needs to fed in XI as a single message. I would like to know a) How a batch IDOCs can be made to single message? b) How XI can check the number of IDOCs in a batch? c) W
-
Does Oracle Database Standard Edition One 10.2 has HTML DB ?
RDBMS Version: 10.2 Operating System and Version: Red Hat Enterprise AS/ES 3 Does Oracle Database Standard Edition One has HTML DB ? Hi, My company plans to buy Oracle Database Standard Edition One for a small project. Does this have HTML DB option ?
-
Nokia Lumia 800 Strange Behavior
Hi all, I do have problem, often my lumia it seems on and fully working but it is impossible to receive call, it is enough to touch a button and all come back to work normally. This is really annoying, Any idea what will be the reason? Phone is updat