Reputation: 51
I'm working on a program that allows a user to enter a message. When the user presses on the "message" menu item located in my frame, a JOptionPane
input dialog box pops up prompting them to enter a string. The problem is that I have to now obtain that string and stick it into my panel class. In addition I also allow the user to select a shape and color from a different dialog box when they press on another menu item also located in my frame. The string has to be on top of the shape I've drawn. I've tried
draw string but it's not functioning properly. Here is my code which is not working.
I just want the string to draw on my panel when I hit OK on the JOptionPane
. How would I do that?
In my frame
private void messageItemActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
String message = JOptionPane.showInputDialog("Enter your string");
// my panel
drawP.setMessage(message);
}
in my Panel
public void setMessage(String s) {
message = s;
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
// the user can select two different shapes this is
// a boolean
if (shape == true) {
g.setColor(shapeColor);
g.fillRect(x, y, 40, 40);
g.drawString(message, x, y);
} else {
g.setColor(shapeColor);
g.fillOval(x, y, 40, 40);
g.drawString(message, x, y);
}
}
Upvotes: 1
Views: 860
Reputation: 133
Have you call repaint() after you set the message? I tried before, repaint JFrame doesn't seems to works, but repaint JPanel works.
Upvotes: 1