How do I pass values from a JFrame class (that contains the main method) to a JPanel class? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

How do I pass values from a JFrame class (that contains the main method) to a JPanel class?

The values I want to pass to the JPanel class are arrays. I need to use the values stored in the array as parameters in the JPanel class.

21st Aug 2020, 4:49 PM
Jay
16 Answers
+ 3
Look at the second answer; I hope it helps. Make the class that contains the main method a subclass of JPanel.
22nd Aug 2020, 2:24 AM
Rowsej
Rowsej - avatar
+ 3
I’m not 100% sure, but maybe if you add a constructor to the ArcsPanel class so when you initialize it in the DrawArcs() function you can pass values. ...I’m not really sure; I don’t Java, but I hope that that will help you.
22nd Aug 2020, 11:48 PM
Rowsej
Rowsej - avatar
+ 2
You’re welcome! Good luck!
22nd Aug 2020, 3:08 AM
Rowsej
Rowsej - avatar
+ 1
Rowsej what if the values I want to pass to the JPanel are array object?
22nd Aug 2020, 2:22 AM
Jay
+ 1
Rowsej thank you. I'll try it out, because right now the main method is in the class that extends jFrame
22nd Aug 2020, 3:07 AM
Jay
+ 1
@Rowsej so my main class extends JFrame, and I am getting user input using the Scanner class. One of my user input is the percentage of certain subjects (this assignment requires getting the percentage of subjects on one semester). Anyway, so I'm supposed to create a pie chart, and I need to access this user inputs (the percentage) in order to draw the pie chart. I'm using the paintComponent and fillArc method. I stored my user inputs (in the main class) in an array. I'm not supposed to use JavaFx, and I have two classes, one that extends JFrame (has the main method) and one that extends JPanel.
22nd Aug 2020, 3:51 AM
Jay
+ 1
Could you possibly share your code?
22nd Aug 2020, 4:55 AM
Rowsej
Rowsej - avatar
+ 1
import javax.swing.JFrame; import javax.swing.JPanel; import java.awt.Graphics; import java.awt.Color; public class DrawArcs extends JFrame { public DrawArcs() { setTitle("DrawArcs"); add(new ArcsPanel()); } /** Main method */ public static void main(String[] args) { DrawArcs frame = new DrawArcs(); frame.setSize(250, 300); frame.setLocationRelativeTo(null); // Center the frame frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } } // The class for drawing arcs on a panel class ArcsPanel extends JPanel { // Draw four blades of a fan\ protected void paintComponent(Graphics g) { super.paintComponent(g); int xCenter = getWidth() / 2; int yCenter = getHeight() / 2; int radius = (int)(Math.min(getWidth(), getHeight()) * 0.4); int x = xCenter - radius; int y = yCenter - radius; g.fillArc(x, y, 2 * radius, 2 * radius, 0, 30); g.setColor(Color.RED); g.fillArc(x, y, 2 * radius, 2 * radius, 90, 30); g.s
22nd Aug 2020, 4:59 AM
Jay
+ 1
Rowsej this is the template he sent us to modify.. Its not complete, it doesn't give the desired output
22nd Aug 2020, 5:06 AM
Jay
+ 1
// The class for drawing arcs on a panel class ArcsPanel extends JPanel { // Draw four blades of a fan\ protected void paintComponent(Graphics g) { super.paintComponent(g); int xCenter = getWidth() / 2; int yCenter = getHeight() / 2; int radius = (int)(Math.min(getWidth(), getHeight()) * 0.4); int x = xCenter - radius; int y = yCenter - radius; g.fillArc(x, y, 2 * radius, 2 * radius, 0, 30); g.setColor(Color.RED); g.fillArc(x, y, 2 * radius, 2 * radius, 90, 30); g.setColor(Color.YELLOW); g.fillArc(x, y, 2 * radius, 2 * radius, 180, 30); g.fillArc(x, y, 2 * radius, 2 * radius, 270, 30); } }
22nd Aug 2020, 5:06 AM
Jay
+ 1
Jay SoloLearn limits the character count for answers; could you possibly save it in the Code Playground and give a link? Looks great anyway!
22nd Aug 2020, 5:06 AM
Rowsej
Rowsej - avatar
22nd Aug 2020, 5:10 AM
Jay
+ 1
Jay Thanks, I’ll look at it sometime soon.
22nd Aug 2020, 6:04 AM
Rowsej
Rowsej - avatar
+ 1
Rowsej thank you for you assistance. Much appreciated
22nd Aug 2020, 7:30 AM
Jay
+ 1
Rowsej thank you, I'll try that out
23rd Aug 2020, 12:18 AM
Jay