Results 1 to 5 of 5
Thread: Updating my gui
- 02-08-2011, 11:56 PM #1
Member
- Join Date
- Feb 2011
- Posts
- 3
- Rep Power
- 0
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.
- 02-08-2011, 11:57 PM #2
Member
- Join Date
- Feb 2011
- Posts
- 3
- Rep Power
- 0
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);
}
- 02-09-2011, 01:02 AM #3
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,609
- Rep Power
- 5
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)
- 02-09-2011, 04:30 AM #4
Member
- Join Date
- Feb 2011
- Posts
- 3
- Rep Power
- 0
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
-
Updating a GUI While Loading
By snowman418 in forum AWT / SwingReplies: 10Last Post: 02-06-2011, 05:31 PM -
Updating a .jar File
By Kyx in forum New To JavaReplies: 3Last Post: 08-20-2010, 03:20 AM -
Updating JTabel
By drwk in forum New To JavaReplies: 2Last Post: 01-17-2010, 12:52 PM -
Updating Arrays
By drymsza1234 in forum New To JavaReplies: 3Last Post: 12-06-2009, 02:34 PM -
Updating my GUI
By Catkill in forum AWT / SwingReplies: 6Last Post: 09-01-2009, 05:09 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks