Forte/swing help
I'm making a text editor for practice with java... How do i make a text area with a scrollbar? Horizontil and vertical..
Thanks
JTextArea area = new JTextArea( nRows, nCols );
JScrollPane pane = new JScrollPane( area );
To control how large the client under the scroll pane's viewport is use "setPreferredSize()"
To set which scroll bars get displayed when, use "[set/get][Horizontal/Vertical]ScrollbarPolicy()" on the pane object
add the pane to your panel/frame etc
Similar Messages
-
RE: (forte-users) Help on 'The class has been voided andcannot be insta
PB Solved.
The symtom was that my 'windows' plan that I distrubute call
the StartMethod of a class defined higher in the hierarchy,
and I had to specify that plan as direct supplier plan of my
new project for the 'voided' not to happen.
cheers
j-p
-----Message d'origine-----
De: Jean-Paul Gabrielli [mailto:Jean-Paul.Gabriellisema.fr]
Date: mercredi 5 juillet 2000 12:08
À: Klerk, Theo de
Objet: RE: (forte-users) Help on 'The class has been voided and cannot
be instanciated' plz
the issue is as follows:
I distribute a plan A
I want to rename the plan,
and create a plan B that has A as supplier.
Distributing B wor'ks
Running it fails with that issue.
Actually I recreated supplierplans in B as they were defined in A,
and the problem disappeared.
thansk for your time
j-paul
-----Message d'origine-----
De: Klerk, Theo de [mailto:Theo.de.Klerkcompaq.com]
Date: mardi 4 juillet 2000 22:31
À: 'Jean-Paul.Gabriellisema.fr'
Objet: RE: (forte-users) Help on 'The class has been voided and cannot
be instanciated' plz
Jean-Paul,
It would help if you have a bit more description around thesituation when
this error happens.
Could it be you have two service objects that refer to each other
while the
partition is still starting up? (SO's are not supposed to talk to
each other
during partition startup).
Or was the partition up and running?
Has the service object been in existence and used before itbecame voided?
Is the partition compiled or interpreted?
Theo de Klerk
Architecture & Application Integration
Professional Services
Compaq Computer Corp. - the Netherlands
PGP Fingerprint: 5A70 DD56 F3BA FE04 9DCA 1ACE 8581 0A2F F057 FA6E
-----Original Message-----
From: Jean-Paul Gabrielli [mailto:Jean-Paul.Gabriellisema.fr]
Sent: Tuesday, 04 July, 2000 18:17
To: Forte-Users
Subject: (forte-users) Help on 'The class has been voided and
cannot be
instanciated' plz
Hi,
Here's the stack I get:
SYSTEM ERROR: Failed to create service object
BOS.MyServicesMgrSO.
Class: qqsp_ResourceException
Error Time: Tue Jul 4 18:05:07
Exception occurred (locally) on partition "BC_cl0_Part3",
(partitionId =
AF3B67A0-3335-11D4-BE9D-963C5A7FAA77:0x224, taskId =
[AF3B67A0-3335-11D4-BE9D-963C5A7FAA77:0x224.6]) in application
"BC_cl0", pid 10328 on node MyServerin environment
CentralEnv.
SYSTEM ERROR: The class MyServericesMgr has been voided and cannot
be
instantiated in this partition.
Class: qqsp_UsageException with ReasonCode: SP_ER_INVALIDSTATE
Error #: [201, 14]
Detected at: qqlo_VoidedClassException at 1
Error Time: Tue Jul 4 18:05:07
Exception occurred (locally) on partition
"BC_cl0_Part3",
(partitionId =
AF3B67A0-3335-11D4-BE9D-963C5A7FAA77:0x224, taskId
=
[AF3B67A0-3335-11D4-BE9D-963C5A7FAA77:0x224.6]) in
application
"BC_cl0", pid 10328 on node MyServer in
environment
CentralEnv.
aud Tue Jul 4 18:05:07 : Shutting down partition due to
failure of this
partition.
aud Tue Jul 4 18:05:07 : Shutting down partition as requested.
For the archives, go to: http://lists.xpedior.com/forte-users and use
the login: forte and the password: archive. To unsubscribe,
send in a new
email the word: 'Unsubscribe' to:
forte-users-requestlists.xpedior.comHi Sylvain
Try using window.smallIconImage =
ImageData(<TitleBarIcon>.ImageValue) ;
This should work.
Jairaj Rampershad
System Concultant
--- Sylvain_Porée <Sylvain.Poreesophia.sema.fr>
wrote:
Hi Forte Gurus,
I have tried to set an icon using this statement :
window.SmallIconImage =
aIconFile.<TitleBarIcon>.imagevalue;
where <TitleBarIcon> is a PictureGraphic in my
window workshop.
I have tried 16*16 16 colours as well as many other
arrangements. It does
not work. I use windows NT.
Any ideas ?
Thanks
Sylvain
PS : For info (forte help):
SmallIconImage
Data Type ImageData
Source Class Window
Settable? Yes
The SmallIconImage attribute (ImageData) provides
the small icon to be used
when the window is iconized, for example, in the
task bar. The image size
for a small icon should be 16 by 16 pixels. Although
the image will be
scaled if you provide one of a different size, for
best results, we
recommend that you create your icons in the same
size in which they will be
displayed.
Windows 95/NT only
This feature is available on Windows 95/NT only. On
other platforms, the
attribute is ignored.
For the archives, go to:
http://lists.xpedior.com/forte-users and use
the login: forte and the password: archive. To
unsubscribe, send in a new
email the word: 'Unsubscribe' to:
forte-users-requestlists.xpedior.com -
JFormattedTextField in Forte Swing palette
Recently I upgraded Forte to release 4. I want to start using the new features of JDK 1.4, but I'm surprised to see that eg. JFormattedTextField is not in my Forte Swing palette. I cannot drop a JFormattedTextField in a JPanel now.
Can anyone please tell me why it's not in the palette or how to get it there. I know how to use 'add to component palette' for my own made Swing classes, but not for new JDK 1.4 classes.take the nb3.4 it is there.
www.netbeans.org -
Hi,
I am using Forte IDE first time. Now I am learning how to design th UI with this IDE. It's kind of diffcult to develope the UI with IDE. I might be wroung as I am not used to IDE.
I checked sun's web site for IDE tutorial. IS there any tutorial through which I can learn, developing UI easily with Forte? I wanted to design UI using Swing and AWT.
Thanks,
PadmashreeWell... obviously... look at the Forte Help menu (like "Help Sets"). And for Swing you can look at the Swing tutorial: http://java.sun.com/docs/books/tutorial/reallybigindex.html
You'll probably want to install the newest Forte you can- perhaps an early-availability version. -
Is there some sort of class or something that can allow me to make an expander in swing? Im not really sure the technical name for it so i have done a little example of what i mean below.
+great
+really good
when great is pressed it expands to:
-great
brilliant
+really good
Any help would be much appreciated.
Edited by: markyboos on Mar 30, 2008 7:36 AMHow about a JTree?
[API link here|http://java.sun.com/javase/6/docs/api/javax/swing/JTree.html]
[Tutorial link here|http://java.sun.com/docs/books/tutorial/uiswing/components/tree.html] -
Displaying 4 Text Areas in Swing.Help required.
Folks
I need to define 4 Text Areas in Swing in the following pattern:
1 -- 2
3 -- 4
where 1,2 3 and 4 are TextAreas,
As of now I can get 2 text Areas :1 and 3 only by the following code:
public class 4TextAreas extends JFrame
private Container c;
Box b = Box.createVerticalBox();
msgTextArea = new JTextArea(10,15);
b.add(new JScrollPane(msgTextArea));
msgTextArea_2 = new JTextArea(10,15);
b.add(new JScrollPane(msgTextArea_2));
c = getContentPane();
c.add(b);
setSize(250,250);
this.show();
Please can any one help as to how to get TextArea 2 and 4 please.
Do I need to crate another Box object called b1?
Please reply.You could try creating two vertical boxes, adding two textareas to each and then add the vertical boxes to a horizontal box. ( or two horizontals to a vertical, of course).
Also you could try a different layout manager such as a gridlayout or gridbaglayout.
Cheers
DB -
Displaying a matrix in swing----HELP!! :)
Hello I want to print out values from a matrix in some sort of table.
javax.swing.JTable is almost what I want, but you can't do row labels (only column labels, I think)--
Are there any classes like JTable, but a little more like an Excel spreadsheet where you have labels on the columns and rows???
Or any other suggestions for displaying matrix values with swing would be helpful!!
THANKS!!!!.
-
Hey gang, I am relatively new to Java world, and especially to swing and thread. I am trying to build an application with a main dashboard, where the users can click on a button and parse a text file. However, if there were an error in the file, I would like to stop paring the file, but not exit the application once there is an error. I don?t know quite how to stop the process without using System.exit(0); When I use System.exit(0); needless to say my main dashboard exits also, which I want to avoid.
I was told to use threads by my friends, but I trying reading up on SwingWorker class found on the website to help with threads (at least I think so). I am still very lost. If there is any more clarification needed, please let me knowYour problem has nothing to do with threads. As "kulkarni_ash" wrote, you need to surround code which crashes with try{ }catch(Throwable t}{...}. Most runtime problems are reported by throwing an exception, which is understood as "abrubtly abort code execution and gracefully return to closest enclosing catch". There is many exceptions classes, each tided to specific kind of error. Throwable is most general one, and you should not use it usually.
Threads, on other hand are used only, if you expect to have a lot of work to be done and like to have it done "in background" what leaves user interface alive. Example is printing - without thread user interface will freez until last page is printed - with printing thread user may click cancel to abort printing. -
Java.io and java.swing help
i'm writing a program that write the byte representation of numbers and outputs them to a joptionpane window
it keeps telling me that void functions are not allowed
is there any way that i can use void functions in my output? or would i need to change to a method that returns a value?
here is my code, please help me, it will be greatly appreciated :)
GUI Part
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class zero2nineTest extends JFrame{
zero2nine show;
public static void main(String[] args){
zero2nineTest x = new zero2nineTest();
x.setVisible(true);
public zero2nineTest(){
super("File IO and Byte Representation Tests");
Container cp = getContentPane();
cp.setLayout(new FlowLayout());
JButton readFile1 = new JButton("Create and Read 1st Byte Representations");
readFile1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
zero2nine.writeFile(out);
JOptionPane.showMessageDialog(
zero2nineTest.this,
"Odd numbers: " + zero2nine.readFile(), "Byte Representation of Odd Numbers",
JOptionPane.PLAIN_MESSAGE);
cp.add(readFile1);
JButton readFile2 = new JButton("Create and Read 2nd Byte Representations");
readFile1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
zero2nine.writeFile2(out);
JOptionPane.showMessageDialog(
zero2nineTest.this,
"Even numbers: " + zero2nine.readFile2(), "Byte Representation of Even Numbers",
JOptionPane.PLAIN_MESSAGE);
cp.add(readFile2);
JButton quit = new JButton("Quit Program");
quit.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
int decision;
decision = JOptionPane.showConfirmDialog(
null, "Are you sure?", "Are you sure?", JOptionPane.YES_NO_OPTION);
if(decision == 0){
System.exit(0);
cp.add(quit);
pack();
Class Part
import java.io.*;
class zero2nine{
static String fileName = "zero2nine.txt";
// public static void main(String[] args){
// try{
// FileOutputStream out = createFile();
// writeFile(out);
// writeFile2(out);
// readFile();
// readFile2();
// }catch(IOException io){
// System.out.println("closing program");
static FileOutputStream createFile() throws IOException{
File f = new File(fileName);
FileOutputStream out = new FileOutputStream(f);
return out;
static void writeFile(FileOutputStream out) throws IOException{
DataOutputStream ds = null;
try{
ds = new DataOutputStream(out);
int x = 0;
for(int i = 0; i < 10; i++){
x = i * i;
ds.writeInt(x);
}finally{
System.out.println("File written this far");
static void writeFile2(FileOutputStream out) throws IOException{
DataOutputStream ds = null;
try{
ds = new DataOutputStream(out);
int x = 0;
for(int i = 10; i < 20; i++){
x = i * i;
ds.writeInt(x);
}finally{
System.out.println("File written this far again");
public int readFile() throws IOException{
DataInputStream dataIn = null;
try{
FileInputStream in = new FileInputStream("zero2nine.txt");
dataIn = new DataInputStream(in);
//Output odd numbers
for(int i = 0; i < 10; i++){
if(dataIn.readInt() % 2 == 0){
int b = dataIn.readInt();
System.out.println(b);
}finally{
System.out.println("File read this far");
return(b);
static void readFile2() throws IOException{
DataInputStream dataIn = null;
try{
FileInputStream in = new FileInputStream("zero2nine.txt");
dataIn = new DataInputStream(in);
//Output even numbers
for(int i = 10; i < 20; i++){
if(dataIn.readInt() % 2 == 1){
int b = dataIn.readInt();
System.out.println(b);
}finally{
System.out.println("File read this far again");
}F:\Computer Science\Java\zero2nineTest.java:28: cannot resolve symbol
symbol: variable out
zero2nine.writeFile(out);
^
public void actionPerformed(ActionEvent e){
zero2nine.writeFile(out);
JOptionPane.showMessageDialog(
F:\Computer Science\Java\zero2nineTest.java:31: 'void' type not allowed here
"Odd numbers: " + zero2nine.readFile(), "Byte Representation of Odd Numbers",
^
zero2nineTest.this,
"Odd numbers: " + zero2nine.readFile(), "Byte Representation of Odd Numbers",
JOptionPane.PLAIN_MESSAGE);
F:\Computer Science\Java\zero2nineTest.java:41: cannot resolve symbol
symbol: variable out
zero2nine.writeFile2(out);
^
zero2nine.writeFile2(out);
JOptionPane.showMessageDialog(
zero2nineTest.this,
F:\Computer Science\Java\zero2nineTest.java:44: 'void' type not allowed here
"Even numbers: " + zero2nine.readFile2(), "Byte Representation of Even Numbers",
^
zero2nineTest.this,
"Even numbers: " + zero2nine.readFile2(), "Byte Representation of Even Numbers",
JOptionPane.PLAIN_MESSAGE);
4 errors
Tool completed with exit code 1 -
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();
} -
Hi, I am currently using a program called JPadPro for my java source codes. I am just beginning to learn java and I have just started on Java Swing GUI codings. My problem is that the program compiles, but it doesn't run. It always give me an error saying
"Cannot find main method, Program will exit". there is nothing wrong with my source code because I can run somewhere else. I think I didn't configure my Jpadpro correctly. (but anything without GUI does work)
Thank You for your help :)Even though you are running a swing application, you still need a main method just as in non-GUI applications. The main should create an instance of the JFrame at the base of you swing app. A simple example is as follows:
public class mySwingApp extends JFrame {
public mySwingApp(String title) {
super(title);
// GUI components, layouts, possible listeners here
public static void main(String[] args) {
JFrame mainFrame = new mySwingApp("My first swing app");
mainFrame.setSize(200, 200);
mainFrame.setVisible(true);
}Hope this helps some!
-JBoeing -
I'm having a lot of troble with a java project i am doing for school. We have to make this word matching type game, and I'm unable to get the icons to repaint correctly. More specifically they will not show up on load, but if i resize the window they display correctly, they will also not refresh when the state of the game changes. I've pasted all the code that relates to drawing bellow, any help would be very apreciated. Thank you.
//package Phase2;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
public class Draw extends JFrame
private Loader _loader;
private java.util.LinkedList<GTile> _column1;
private java.util.LinkedList<GTile> _column2;
private java.util.LinkedList<GTile> _column3;
private java.util.LinkedList<GTile> _column4;
private java.util.LinkedList<GTile> _column5;
private java.util.LinkedList<GTile> _column6;
private java.util.LinkedList<GTile> _column7;
private java.util.ArrayList<String> _arrayList;
private java.util.ArrayList<java.util.LinkedList<GTile>> _2DArray;
//private java.util.ArrayList<Picture> _pictures;
private GamePanel GameBoard;
private GameState _gsObj;
public Draw(GameState arg_gsObj)
super("Bookworm Group 4");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
_loader = new Loader();
_gsObj = arg_gsObj;
_column1 = new java.util.LinkedList<GTile>();
_column2 = new java.util.LinkedList<GTile>();
_column3 = new java.util.LinkedList<GTile>();
_column4 = new java.util.LinkedList<GTile>();
_column5 = new java.util.LinkedList<GTile>();
_column6 = new java.util.LinkedList<GTile>();
_column7 = new java.util.LinkedList<GTile>();
_2DArray = new java.util.ArrayList<java.util.LinkedList<GTile>>();
establishArrays();
GameBoard = new GamePanel(_2DArray, _gsObj, _loader);
getContentPane().add(GameBoard, BorderLayout.CENTER);
setVisible(true);
pack();
show();
paintComponents(this.getGraphics());
//resize( 500, 500 );
public void establishArrays()
for(int i=0; i<8; i++)
_column1.add(_loader.Send(1, i));
for(int i=0; i<7; i++)
_column2.add(_loader.Send(2 ,i));
for(int i=0; i<8; i++)
_column3.add(_loader.Send(3, i));
for(int i=0; i<7; i++)
_column4.add(_loader.Send(4, i));
for(int i=0; i<8; i++)
_column5.add(_loader.Send(5, i));
for(int i=0; i<7; i++)
_column6.add(_loader.Send(6, i));
for(int i=0; i<8; i++)
_column7.add(_loader.Send(7, i));
_2DArray.add(_column1);
_2DArray.add(_column2);
_2DArray.add(_column3);
_2DArray.add(_column4);
_2DArray.add(_column5);
_2DArray.add(_column6);
_2DArray.add(_column7);
public void update(Graphics g)
super.update(g);
System.out.println("update called");
Graphics2D g2d = (Graphics2D)g;
GameBoard.update(g2d);
// g2d.drawImage(_image, 0, 0, null);
public void repaint()
super.repaint();
System.out.println("Draw repaint called");
//Graphics2D g2d = (Graphics2D)this.getGraphics();
// g2d.drawImage(_image, 0, 0, null);
public void paint(Graphics g)
super.paint(g);
System.out.println("Draw Paint called");
System.out.println("Draw: "+this.getGraphics());
System.out.println(g);
Graphics2D g2d = (Graphics2D)g;
GameBoard.update(g2d);
// g2d.drawImage(_image, 0, 0, null);
/package Phase2;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
class GamePanel extends JPanel
private GameColumn _column1;
private GameColumn _column2;
private GameColumn _column3;
private GameColumn _column4;
private GameColumn _column5;
private GameColumn _column6;
private GameColumn _column7;
private GameState _gsObj;
private Loader _loader;
java.util.ArrayList<GTile> _selectedTiles;
java.util.ArrayList<java.util.LinkedList<GTile>> _cArray;
public GamePanel(java.util.ArrayList<java.util.LinkedList<GTile>> arg_array, GameState arg_gsObj, Loader arg_loader)
super();
_cArray = arg_array;
_gsObj = arg_gsObj;
_loader = arg_loader;
_selectedTiles = new java.util.ArrayList<GTile>();
GridLayout board = new GridLayout(1,7);
setLayout(board);
buildColumns();
setVisible(true);
this.setBackground(java.awt.Color.WHITE);
public void buildColumns()
_column1 = new GameColumn(_cArray.get(0), 1, this);
_column2 = new GameColumn(_cArray.get(1), 2, this);
_column3 = new GameColumn(_cArray.get(2), 3, this);
_column4 = new GameColumn(_cArray.get(3), 4, this);
_column5 = new GameColumn(_cArray.get(4), 5, this);
_column6 = new GameColumn(_cArray.get(5), 6, this);
_column7 = new GameColumn(_cArray.get(6), 7, this);
add(_column1);
add(_column2);
add(_column3);
add(_column4);
add(_column5);
add(_column6);
add(_column7);
System.out.println("gamePanel: "+this.getGraphics());
public void paint(Graphics g)
GTile temp = new GTile("z");
for(int i=0; i<8; i++)
temp = _column1.getTile(i);
temp.repaint();
for(int i=0; i<7; i++)
temp = _column2.getTile(i);
temp.repaint();
for(int i=0; i<8; i++)
temp = _column3.getTile(i);
temp.repaint();
for(int i=0; i<7; i++)
temp = _column4.getTile(i);
temp.repaint();
for(int i=0; i<8; i++)
temp = _column5.getTile(i);
temp.repaint();
for(int i=0; i<7; i++)
temp = _column6.getTile(i);
temp.repaint();
for(int i=0; i<8; i++)
temp = _column7.getTile(i);
temp.repaint();
public void follow(GTile arg_tile)
System.out.println("heres a tile "+arg_tile.getColumn()+" "+arg_tile.getPos());
if(!_selectedTiles.isEmpty())
GTile lastTile = _selectedTiles.get(0);
System.out.println("last tile "+lastTile.getColumn()+" "+lastTile.getPos());
if(arg_tile.getColumn() == lastTile.getColumn() && arg_tile.getPos() == lastTile.getPos())
if(_gsObj.Submit(_selectedTiles))
this.removeTiles(_selectedTiles);
_selectedTiles.clear();
}else if(this.adjacent(arg_tile))
_selectedTiles.add(0, arg_tile);
}else
_selectedTiles.clear();
_selectedTiles.add(0, arg_tile);
}else
_selectedTiles.add(0, arg_tile);
private void removeTiles(java.util.ArrayList<GTile> arg_deadTiles)
for(int i = 0; i<arg_deadTiles.size(); i++)
GTile tempTile = arg_deadTiles.get(i);
int column = tempTile.getColumn() - 1;
java.util.LinkedList<GTile> tempColumn = _cArray.get(column);
for(int k = tempTile.getPos(); k >0; k--)
tempColumn.set(k, tempColumn.get(k-1));
tempColumn.set(0, _loader.Send(column, 0));
_cArray.set(column, tempColumn);
this.removeAll();
this.buildColumns();
System.out.println(this.getGraphics());
this.update(this.getGraphics());
System.out.println("\n\n\n");
//this.resize(500, 500);
private boolean adjacent(GTile arg_tile)
if(!_selectedTiles.isEmpty())
GTile lastTile = _selectedTiles.get(0);
System.out.println("modulus"+arg_tile.getColumn()%2);
if(arg_tile.getColumn()%2 == 1)
System.out.println("this is a 7 tile column"+arg_tile.getColumn()%2);
if(arg_tile.getColumn() == lastTile.getColumn())
if( arg_tile.getPos()+1 == lastTile.getPos() || arg_tile.getPos()-1 == lastTile.getPos())
return true;
}else
return false;
}else if(arg_tile.getColumn()+1 == lastTile.getColumn())
if( arg_tile.getPos()-1 == lastTile.getPos() || arg_tile.getPos() == lastTile.getPos())
return true;
}else
return false;
}else if(arg_tile.getColumn()-1 == lastTile.getColumn())
if( arg_tile.getPos()-1 == lastTile.getPos() || arg_tile.getPos() == lastTile.getPos())
return true;
}else
return false;
}else
return false;
}else
if(arg_tile.getColumn() == lastTile.getColumn())
if( arg_tile.getPos()+1 == lastTile.getPos() || arg_tile.getPos()-1 == lastTile.getPos())
return true;
}else
return false;
}else if(arg_tile.getColumn()+1 == lastTile.getColumn())
if( arg_tile.getPos()+1 == lastTile.getPos() || arg_tile.getPos() == lastTile.getPos())
return true;
}else
return false;
}else if(arg_tile.getColumn()-1 == lastTile.getColumn())
if( arg_tile.getPos()+1 == lastTile.getPos() || arg_tile.getPos() == lastTile.getPos())
return true;
}else
return false;
}else
return false;
return false;
//package Phase2;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
class GameColumn extends JPanel
private java.util.LinkedList<GTile> _tArray;
private int _cnumber;
private GamePanel _gpObj;
public GameColumn(java.util.LinkedList<GTile> arg_tArray, int arg_cnumber, GamePanel arg_gpObj)
super();
_cnumber = arg_cnumber;
_tArray = arg_tArray;
_gpObj = arg_gpObj;
int height = _tArray.size();
GridLayout cLayout = new GridLayout(height, 1);
setLayout(cLayout);
for(int i=0; i<height; i++)
GTile temp = _tArray.get(i);
temp.setGP(_gpObj);
System.out.println(_tArray.get(i)._gpObj);
add(_tArray.get(i));
if(height == 7)
float tempY = 15;
setAlignmentY(tempY);
setVisible(true);
this.setBackground(java.awt.Color.WHITE);
System.out.println("gamecolumn: "+this.getGraphics());
public GTile getTile(int arg_pos)
return _tArray.get(arg_pos);
public int GetSize()
return _tArray.size();
public void paint(Graphics comp)
super.paint(comp);
GTile tempTile = new GTile("x");
int height = _tArray.size();
for(int i=0; i<height; i++)
tempTile = _tArray.get(i);
tempTile.paintComponent(comp);
//package Phase2;
import java.awt.*;
import java.awt.Graphics2D;
import javax.swing.event.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.geom.AffineTransform;
public class GTile extends JComponent
private String _letter;
private int _column;
private int _pos;
public GamePanel _gpObj;
private Image _image;
private Graphics2D DDGraphic;
public GTile(String arg_letter)
super();
_letter = arg_letter;
_column = 0;
_pos = 0;
_image = new ImageIcon("Pictures/"+_letter+".GIF").getImage();
this.setMinimumSize(new Dimension(30,30));
this.setMaximumSize(new Dimension(30,30));
this.setBackground(java.awt.Color.WHITE);
this.addMouseListener(new MouseAdapter()
public void mouseClicked(MouseEvent e){
handleClick();
public void mousePressed(MouseEvent e){
//setCurrent(e);
public void mouseReleased(MouseEvent e){
//releaseCurrent();
setVisible(true);
public void setPos(int arg_column, int arg_pos )
_column = arg_column;
_pos = arg_pos;
public void setGP(GamePanel arg_gpObj)
_gpObj = arg_gpObj;
public int getColumn()
return _column;
public int getPos()
return _pos;
public String getLetter()
return _letter;
public void handleClick()
GTile temp = this;
System.out.println("we got anything here?");
_gpObj.follow(temp);
public void paint(Graphics g)
super.paint(g);
if(_letter.equals("A"))
System.out.println("\npos "+_pos+" col "+_column);
System.out.println("Paint called");
System.out.println(g);
Graphics2D g2d = (Graphics2D)g;
g2d.drawImage(_image, 0, 0, null);
public void paintComponent(Graphics g)
super.paintComponent(g);
Graphics2D g2d = (Graphics2D)g;
if(_letter.equals("A"))
System.out.println("\npos "+_pos+" col "+_column);
System.out.println("paintComponet called");
System.out.println(g);
System.out.println(this.getGraphics());
g2d.drawImage(_image, 0, 0, null);
//_image.paintIcon(this, DDGraphic, 0, 0);
setVisible(true);
Well, I would start by reading the Swing tutorial on [url http://java.sun.com/docs/books/tutorial/uiswing/misc/icon.html]How to Use Icons. The easiest way is to just add the icon to a JLabel.
You code is full of overrides. There should be no need to override methods like "update, paint, repaint" of your main JFrame. Even the panels you use override the paint method. Although I strongly doubt you need to override the paint() method, the correct way to override the painting is by overriding the paintComponent(..) method.
I would suggest you don't need any of your override logic. Instead you just replace the icon of each JLabel by using setIcon. -
hello,
I am creating one GUI project using swing using netbeans 4.1 in windows environment. It works fine and display the GUI correctly.
But, my problem is when I use that project in linux that is when I open this project folder using netbeans 4.1 linux version, it compile and execute.
But], when GUI open, the label leters are not shown properly. it shows S.... in the GUI window. also, the gui some time struck. the radio buttons not works some time.
what can I do. in linux.
shall i need to change in gui layout setting or others.
pls, help me to solve this problem.
thank you.Maybe you should read the responses to your other threads on this topic!
http://forum.java.sun.com/thread.jspa?threadID=767912&messageID=4377241#4377241
http://forum.java.sun.com/thread.jspa?threadID=767960&messageID=4377468#4377468 -
Hi,
I have developed a Swing-based GUI and although the tool works functionally, I am looking for some improvements in the actual GUI components' layout - basically I want to have some beautification done on the existing GUI. I think this kind of do-it-pretty task can be handled very easily by a Swing expert and so I was checking if anybody is available professionally to take on this task and make this happen? Let me know if there is anyone interested in this and we can go from there.
Thanksdiscuss2008
Please don't advertise for paid help here. I'm blocking your post.
db -
Hi to all i am new to swings concept my problem is
I taken 8 subjects with 100 marks and 5 labs with 75 marks in my application.
Know my problem is when i am calculating subjects and labs i am not getting average .
And but when i am calculate total divided by 13 i am getting average.
For example : 8 subjects total is 560.
5 labs total is 250.
average = total/13
i am getting average.
But i want average like this i.e average = total /1175;
i am taken total = subjects + labs;
help me regarding this topic
Thanks in advanceHello
u r did "average = total/13; " this avg is not correct one .....
bcos 8 subj is respect to 100 and remaining is respect to 75 .
avg = (getting total marks / totalmarks)*100
this is the correct way to calculate avg
i am taken total = subjects + labs;
all the best
regards
Amir
Maybe you are looking for
-
The home button on my ipod 5th gen has stopped working comletely.
I have had my 5th gen ipod touch for almost a year now. I never drop it. i take good care for it. its a 64 GB one so it was heaps of money. LIke 90% of my life is on that thing. Should i ask for a replacemeant or is it possible for it to be fixed? I
-
Finding a specific point on a rendered line
Consider the following class. package com.somemythicalpackage import java.awt.* ; import java.awt.event.* ; import javax.swing.* ; public class DrawrALine extends JPanel implements MouseListener{ private Point origin, terminus ; public Draw
-
I need a way to change the default style of a textfield. For example, if the textfield is required, I want the set the background color to yellow (FFFFCC) so that when the page containing that textfield loads the background color will already be yell
-
Can't delete apps after ios 7.0.3 software update
Please help. I cant delete my apps after software update to ios 7.0.3...waited for the new update,but after the ios 7.0.4 update, still the smae.help Pls
-
Failure - Unable to start Fiori App (MyTimeSheet)
Hello Experts, Currently I'm struggling with a Fiori App, MyTimeSheet. I'm not able to access it from the launchpad. I get "Unable to start app" error all the time. Could you help? Thanks in advance. Best regards P Pawlak