Netbeans swing help!

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

Similar Messages

  • Three NetBeans/Swing Newbie Questions

    Re: Netbeans/Swing
    1. The Background properties of the form only presents me with options for changing the color. Is there a way to set the form background to an Image (.jpeg, .bmp...etc like I can in Visual Basic)?
    2. Is there an Image container for .jpeg, .bmp...etc?
    3. Also, how can I stop the startup from asking me to Join/Never/Remind Me Later? I already joined but keep getting this panel...
    Thanks,

    outwest99 wrote:
    Thanks for the response.
    I'm not in the code generator - I'm in the form Design (using Swing). The properties panel is available to me. I actually have many years of programming experience (30) and, while I've never used Java (hence, the "Newbie") I do use VB6.0 and Swing is quite similar in the drag/drop and properties setups.Ya, so do I, 20+ now professionally. Don't use the code generator--Form Designer--until you know what you are doing, it will only cripple your progress in Java. Get out the Java Tutorial and go through how to do it manually and then you'll understand the Form Designer's limitations and strong points.
    Java is not VB, it took me a while to get that through my head too.
    I looked at the forums (many others ask the same question about images) but see lot's of "you gotta do this" with examples but have not seen a simple solution that doesn't involve inserting many lines of code. Certainly it can't be that laborious or difficult. I'm reminded of having pancakes in a hotel - when I asked for syrup, the server said: "We don't have syrup, but everybody asks for it". It seems to me that if so many others are looking for the same solution, that maybe developers should make a simple solution.Once again, Java is not VB.
    Regarding my item #3 - I get the message to join Sun's developer program when I start Netbeans. I used the "Register" button to register and followed up by clicking the link in the email Sun sent me - I figure that, like with other programs, the message would go away once I registered - it didn't.That part I cannot help you with. I was registered long before I downloaded my versions of NetBeans and Java.

  • How to display multiple tables from database using netbeans swing gui

    plz reply asap on how to display multiple tables from database using netbeans swing gui into the same project

    Layered Pane with JTables or you can easily to it with a little scripting and HTML.
    plzzzzzzzzzzzzzzzzz, do not use SMS speak when posting.

  • Trying to create Netbeans Swing/GUI component

    Hello,
    I'm trying to create a GUI component from the following code. The main idea is to create a component consisting of a checkbox and a panel. The panel can contain several other swing components. By checking or unchecking the checkbox I want all by components in the panel to be enabled or disabled.
    The important thing is that this component should work in the Netbeans GUI designer. I'm using Netbeans 5.5.
    My problem is the following:
    When adding my swing component to the Netbeans swing component palette and dragging onto a new form, I cannot assign other components to the main panel of my component. Even if I use the Inspector tree to drag components to be children of my component, the mouse icon shows a denying icon.
    If I add swing components programmatically, it works.
    Can somebody give me an advice what I should change in my code to make it work with the Netbeans GUI designer?
    Below you can find the current code of my standalone component. It includes a main() method so one can give it a try.
    Many thanks in advance!
    package test.swing;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    public class JEnabler extends JPanel {
        private JCheckBox checkEnabler = new JCheckBox();
        private JPanel contentPane = new JPanel();
        public JEnabler() {
            super.setLayout(new BorderLayout());
            checkEnabler.setSelected(true);
            checkEnabler.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
            checkEnabler.setMargin(new Insets(0, 0, 0, 0));
            checkEnabler.setVerticalAlignment(SwingConstants.TOP);
            checkEnabler.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    checkEnablerActionPerformed(evt);
            super.addImpl(checkEnabler, BorderLayout.WEST, -1);
            super.addImpl(contentPane, BorderLayout.CENTER, -1);
        private void checkEnablerActionPerformed(ActionEvent evt) {
            Component[] list = contentPane.getComponents();
            for (int i=0; i<list.length; i++)
                list.setEnabled(checkEnabler.isSelected());
    protected void addImpl(Component comp, Object constraints, int index) {
    contentPane.add(comp, constraints, index);
    public LayoutManager getLayout() {
    return contentPane.getLayout();
    public void setLayout(LayoutManager mgr) {
    if (contentPane!=null)
    contentPane.setLayout(mgr);
    public void remove(Component comp) {
    contentPane.remove(comp);
    public void remove(int index) {
    contentPane.remove(index);
    public void removeAll() {
    contentPane.removeAll();
    public static void main(String args[]) {
    SwingUtilities.invokeLater(new Runnable() {
    public void run() {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JEnabler enabler = new JEnabler();
    JTextField tf = new JTextField("Test");
    enabler.add(tf);
    JButton test = new JButton("Click");
    enabler.add(test);
    frame.getContentPane().add(enabler);
    frame.pack();
    frame.setVisible(true);

    Can anyone tell me what I am missing?
    public void paintComponents(Graphics g)you're not 'missing' anything, in fact you've gained an 's'

  • Netbeans odbc help

    Im new to doing databases in general and never used a ide to help me. I have connected to a few without the help of a ide in the past. I basicly have a really small access database that i want to connect to called inventory.mdb
    I have searched but have not found a solution to my problem.
    I regestered the inventory data base as a user dsn in windows.
    I go to the runtime window in netbeans and rightclick new connection on the database tab.
    for the driver field it states: sun.jdbc.odbc.JdbcOdbcDriver
    for the database url it states:jdbc:odbc:inventory
    when i press connect i get error message that follows:
    Unable to obtain schema.
    There is insufficent information to establish a connection to obtain list of schemas. Check database URL, user and password.
    Driver message:[Microsoft][ODBC Microsoft Access Driver]Optional feature not implemented
    sorry for such a noob question its prob something very simple to fix

    BaGo wrote:
    Help me to understang Nb IDE.
    I go through swing tutorial. I picked a few demo programs source out of example and put them into a new component folder. Using NetBeans 6.5 create a project, component.
    I can compile all of them without any problem. I can run it from any one of them.IDE.
    I compile TextAreaDemo ran fine. Next I compile TextSampleDemo and ran. Out put was TextAreaDemo.
    I found out compiler put in project\.....\dist\project.jar. So I delete it out
    and compile and ran again. The same out put of TextAreaDemo.
    I edited some invalid codes in TextSampleDemo and compile it. It caught as error. So it compile the right source.
    Will some one educate me please.
    BaGofirst of all "corlettk"... i see you liked my idea in your "noob fourm" =)
    Here is your problem BaGo....
    Netbeans is not doing what you think it should. (i use it alot)
    Since you probably put "TextAreaDemo" and "TextSampleDemo" in the same project... when you hit the "complie main project" it compiles BOTH of them, and puts the jar file into into the /src folder. (thats why you get the right source)
    I am assuming you are testing your program by clicking "the big button at the top (run main project)"... The thing about that, is that it runs the "main" class of ALL the classes in your Main project. If you want to run just 1 of the classes, right-click that class in the project viewer and select "run file".
    If you REALLY like to use the "run main project" button... you can right-click the PROJECT in the project viewer.. go to properties... click the "run" tab, and change the "Main Class" to "TextSampleDemo".

  • Swing Help!!

    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 AM

    How 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!!!!

    .

  • Thread in Swing help please

    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 know

    Your 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.

  • NetBean Swing or Swing without NetBean

    In previous posts I have mentioned about me using BlueJ. Ive tried NetBeans IDE and find it better than BlueJ so I am using NetBeans now. One area of NetBeans uses Swing GUI components which allows you to build GUIs and creates the source code for you. There seems to be alot of information in the Oracle tutorials on Swing outside Netbeans. and at the moment I am still learning the Java programming language and do not know how important learning Swing is. This leads me to the question: Is using Swing in NetBeans enough to get by in creating GUIs or what aspects in Swing are the most important to use in conjunction with Swing in NetBeans?

    You can do desktop or web application development (there are others). I suggest you do web application development instead of desktop since I believe there are more employment opportunites there. If you decide to go that route, I suggest you dump Swing and go with JSF. Its part of the recommended JEE technology stack (http://www.oracle.com//technetwork/java/javaee/tech/index.html). I also suggest you use either Netbeans IDE or Eclipse IDE since they are the most popular with experienced developers. You'll also need a (free) database. I suggest Oracle Express. For a server: Tomcat or Glassfish.
    I suggest the first book you read is on Java, then on Servlets, then on  JSF. I suggest you keep away from JSP, its older technology. However, you will eventually need to read up on it since so many web applications are written with it. You will probably need to maintain those types of web applications when employed. Which other technologies to study and in which order in the above mentioned link is a subject for another day.

  • 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();
    }

  • Java Swing Help

    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

  • Need swing help

    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.

  • Professional Swing Help

    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.
    Thanks

    discuss2008
    Please don't advertise for paid help here. I'm blocking your post.
    db

Maybe you are looking for

  • My app wont open on windows! How can I open an app on windows?

    My Dragonvale wont open on my windows. I can't find out how to open it on my windows. Please if you know tell me how!

  • Blackberry Tour disappearing applications and games issue

    I received my new BB Tour on Monday.  When I got  it, it had about 5 games as well as Word To Go, Excel to Go and Powerpoint To go.  Now they are gone.  I don't know what I did; however, I would like to get them back.  Can anyone assist me in doing s

  • # The Application Name that you provided has already been used.

    "# The Application Name that you provided has already been used. Please provide a unique Application Name." Anyone else getting this when trying to update their apps description on iTunes Connect?

  • What's with the all of the JOptionPanes?

    What is it with people and JOption panes? I posted this in the "New to Java" since it seems to mostly be newbies that have this strange desire to pop up JOptionPane all over the place. I get it when there is some large GUI of which some JOptionPanes

  • Extending HGrid VO

    We are facing an issue in our project with a VO extension where the VO is being used in an HGrid in a Seeded OAF screen. We did the VO extension to add two columns that we needed to be displayed in the HGrid. This requirement is implemented and it is