Mike Mike - 1 year ago 74
Java Question

Java graphics add method

I'm trying to create a networked poker server client program, I'm currently writing the client side which includes the graphics part, However when I try to add a component to a JPanel in my code when a certain condition is met in the run method, add method doesn't seem to work, however other methods that manipulates the JPanel under the same condition works.

public class PokerClient {

BufferedReader in;
PrintWriter out;
JFrame frame = new JFrame("Poker");

JPanel playerHandPanel;

String serverAddress = "localhost";
String playerName;

Card playerHand1, playerHand2;

public PokerClient() {

// Layout GUI
frame.setSize(1100, 700);

playerHandPanel = new JPanel(new GridLayout(1, 2));
playerHandPanel.setPreferredSize(new Dimension(600, 300));
playerHandPanel.add(new CardComponent(new Card(3, Suit.CLUB))); //it works here

frame.add(playerHandPanel, BorderLayout.NORTH);

* Prompt for and return the desired screen name.
private String getName() {

return JOptionPane.showInputDialog(
"Choose a screen name:",
"Screen name selection",

private Card constructCard(String line){
int seperator = line.indexOf('/');
int cardNum = Integer.parseInt(line.substring(0, seperator));
Card card;
card = new Card(cardNum, Suit.SPADE);
} else if(line.substring(seperator+1).startsWith("C")){
card = new Card(cardNum, Suit.CLUB);
} else if(line.substring(seperator+1).startsWith("D")){
card = new Card(cardNum, Suit.DIAMOND);
} else{
card = new Card(cardNum, Suit.HEART);
return card;

* Connects to the server then enters the processing loop.
private void run() throws IOException {

Socket socket = new Socket(serverAddress, 9050);
in = new BufferedReader(new InputStreamReader(
out = new PrintWriter(socket.getOutputStream(), true);

// Process all messages from server, according to the protocol.
while (true) {
String line = in.readLine();
if (line.startsWith("SUBMITNAME")) {
// String name = getName();
// playerName = name;
// out.println(name);
} else if (line.startsWith("p1")) {
playerHandPanel.add(new CardComponent(new Card(4, Suit.SPADE)));//this doesn't work i can't figure out why
playerHandPanel.setBackground(Color.WHITE);//this worked
playerHandPanel.add(new JLabel("is this added"));//this doesn't work either

public static void main(String[] args) throws Exception {
PokerClient client = new PokerClient();

Answer Source

Several problems jump out:

  • You're not calling revalidate() on the playerHandPanel after adding or removing a component -- probably a major contributor to your problem.
  • You're constraining the playerHandPanel's size artificially
  • And not putting it into a JScrollPane
  • Your code flaunts Swing threading rules by making major Swing component state changes off of the Swing event thread or EDT
  • You're using a constraining layout, new GridLayout(1, 2)

Possible solutions:

  • Yes, do call revalidate() on the playerHandPanel after adding or removing a component. This will tell its layout managers to do their thing.
  • If you want to use GridLayout, do so in a more flexible way, such as, new GridLayout(1, 0) or new GridLayout(1, 0), depending on if you want to specify the number of columns or rows (the 0 meaning a variable number of columns or rows)
  • Consider using a JList or JTable, two components that are much easier to add things to.
  • Do learn and follow Swing threading rules, including only making Swing state changes (such as adding or removing components, changing background color...) on the Swing event thread.
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download