Results 1 to 5 of 5

Thread: Updating my gui

  1. #1
    mrx89_7 is offline Member
    Join Date
    Feb 2011
    Posts
    3
    Rep Power
    0

    Default Updating my gui

    I'm pretty new to GUIs, so bear w/ me. I have this java project that I'm working on and it has some buttons. Well when I press one of these buttons I'd like to change some static variables and update what is visible on the screen. I have successfully done this, but in the most fumblely way I could have. Right now I turn of the visible jFrame the set it equal to a new one then turn it back on. My question is what would be a more elegant way to do this.

    Heres my code:

    public class NewJFrame extends javax.swing.JFrame {
    public static Deck deck = new Deck();
    public static Image[] visable = new Image[5];
    public static PokerHand invisable = new PokerHand();
    public static NewJFrame window;
    public static Comparator currentSort;

    public static void main(String args[]) {
    deck.shuffle();
    currentSort = Deck.randomly;
    getNewHand();
    window = new NewJFrame();

    java.awt.EventQueue.invokeLater(
    new Runnable() {
    public void run() {
    window.setVisible(true);
    }
    });
    }

    /**
    * Action listener for the deck's "By Suit" button.
    * @param evt
    */
    private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton6ActionPerformed
    deck.sort(Hand.bySuit);
    currentSort = Deck.bySuit;
    }


    It might also be relavent to state that i made thegui in netbeans, so the constructor for NewJFrame wasn't coded by me.

  2. #2
    mrx89_7 is offline Member
    Join Date
    Feb 2011
    Posts
    3
    Rep Power
    0

    Default

    Ok so i didn't add the action listener i needed to here's a better example.

    /**
    * Action listener for the hand's "By Value" button.
    * @param evt
    */
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
    invisable.sort(Hand.byRank);
    visable = invisable.getImages();
    NewJFrame j = new NewJFrame();
    window.setVisible(false);
    window = j;
    window.setVisible(true);
    }

  3. #3
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default

    First off, for future reference the code tags make your code much more readable. Next, you have posted a limited amount of code so its hard to guess what exactly to do...but I can give you a bit of input based upon some assumptions. In your actionPerformed method you have changed a variable, typically if the change in this variable requires a GUI update, you would subsequently call repaint on the components within the GUI that need to be repainted. (a much more elegant way is to have the model separated from the GUI and the dependency between the two based upon a listener type of pattern, in which you can register listeners and notify them when the model changes - but this probably goes way beyond what you need at this point and given my poor way of explaining it not very intuitive)

  4. #4
    mrx89_7 is offline Member
    Join Date
    Feb 2011
    Posts
    3
    Rep Power
    0

    Default

    I've run into the word repaint alot, but evidently i have no idea how to use it. Because if I envoke it on the jFrame variable window, and make it happen every time an ActionEvent is performed. When I run the program and click the buttons nothing updates.

    On a side note, Would it help if i posted the code in it's entirety? I just didn't want to post too much code.

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by mrx89_7 View Post
    I've run into the word repaint alot, but evidently i have no idea how to use it. Because if I envoke it on the jFrame variable window, and make it happen every time an ActionEvent is performed. When I run the program and click the buttons nothing updates.

    On a side note, Would it help if i posted the code in it's entirety? I just didn't want to post too much code.
    My guess is that much of your code has been created by NetBeans drag-and-drop code generation and will be large, ugly and useless to us. I suggest avoiding use of this feature and learn to code Swing by hand. This way you'll understand what you're doing much better and we'll be able to read your code and understand it as well.

Similar Threads

  1. Updating a GUI While Loading
    By snowman418 in forum AWT / Swing
    Replies: 10
    Last Post: 02-06-2011, 05:31 PM
  2. Updating a .jar File
    By Kyx in forum New To Java
    Replies: 3
    Last Post: 08-20-2010, 03:20 AM
  3. Updating JTabel
    By drwk in forum New To Java
    Replies: 2
    Last Post: 01-17-2010, 12:52 PM
  4. Updating Arrays
    By drymsza1234 in forum New To Java
    Replies: 3
    Last Post: 12-06-2009, 02:34 PM
  5. Updating my GUI
    By Catkill in forum AWT / Swing
    Replies: 6
    Last Post: 09-01-2009, 05:09 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •