How to use jTable in java?

JAVA When To Use RePaint();?

  • Here is my code I want to change the color of the points when ever some one gets a correct answer. Thanks in advance. import java.util.Scanner; import java.io.*; import javax.swing.JOptionPane; import java.text.DecimalFormat; import java.util.Random; import java.awt.*; import javax.swing.*; import java.awt.event.*; import java.util.*; // MATH GAME FOR THE KIDS public class MathTutor { private JFrame frame; private JPanel panel; private JButton check; private JLabel problem; private JTextField answer; private JLabel desing; private JPanel someGraphics; private Graphics2D g2d; public MathTutor() { Gui(); } public void Gui() { frame = new JFrame(" MATH GAME "); frame.setVisible(true); frame.setSize(200,300); frame.setDefaultCloseOperation(JFrame.… panel = new JPanel(); panel.setBackground(Color.BLACK); panel.setForeground(Color.RED); desing = new JLabel("THIS WILL TELL YOU RIGHT OR WRONG !"); desing.setForeground(Color.GREEN); desing.setBackground(Color.RED); check = new JButton("CHECK"); check.addActionListener(new checkButtonListener()); problem = new JLabel(NumberMaker()); problem.setForeground(Color.RED); answer = new JTextField(5); answer.setText("0"); answer.setBackground(Color.BLACK); answer.setForeground(Color.GREEN); panel.add(problem); panel.add(answer); panel.add(check); someGraphics = new JPanel(); //need to learn how to add graphics to panel frame.add(panel, BorderLayout.NORTH); frame.add(desing,BorderLayout.SOUTH); frame.add(someGraphics); frame.add(new Points()); frame.pack(); } public String NumberMaker() { String str = " "; Random number1 = new Random(); int x = number1.nextInt(13); Random number2 = new Random(); int y = number2.nextInt(13); str = "What is "+x+" + "+y+" ?"; return str; } public int getNumbersFromString(String str) { StringBuilder sb = new StringBuilder(str); sb.delete(0,8); //you have to put -1 here because other wise it will go 8 spaces the actually numeric lenght instead of the string lenght 0 -7 sb.deleteCharAt(sb.length()-1); String s = sb.toString(); StringTokenizer st = new StringTokenizer(s,"+"); //this turns every number before and after + to an integer and trims off any extra whitespaces int result = Integer.parseInt(st.nextToken().trim()) + Integer.parseInt(st.nextToken().trim()); return result; } public class Points extends JPanel { public void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2d = (Graphics2D) g; g2d.setColor(Color.blue); Dimension size = getSize(); Insets insets = getInsets(); int w = size.width - insets.left - insets.right; int h = size.height - insets.top - insets.bottom; Random r = new Random(); for (int i=0; i<1000; i++) { int x = Math.abs(r.nextInt()) % w; int y = Math.abs(r.nextInt()) % h; g2d.drawLine(x, y, x, y); } } } private class checkButtonListener implements ActionListener { public void actionPerformed(ActionEvent e) { //Write a String that searches for the numbers in STR maybe make it a method for ease of reading int fromAnswerBox = Integer.parseInt(answer.getText()); if(fromAnswerBox == getNumbersFromString(problem.getText())) { desing.setText("!!!!!!!!!!!!!!!!!!!!!C… problem.setText(NumberMaker()); answer.setText("0"); /* Here is what I am trying to do Not sure how to call the repaint(); or when g2d.setColor(Color.red); repaint();*/ } else { desing.setText("Wrong Please Try Again"); } //repaint(); } } public static void main(String[] args) { new MathTutor(); } }

  • Answer:

    You can't with how you have it set up... You are trying to change an attribute of an unnamed object in an entirely different class.... If your actionListener was in the main class like it's supposed to be you wouldn't have this issue. It's as if you make new classes just for the heck of it. This project has two classes MAX. the main class should extend Jframe and implement actionlistener ... everywhere you have frame use "this" this.setTitle("MATH GAME") also check.addActionListener(this); I also added a contructor for the points class since you didn't put one. it's not letting me paste this so http://pastebin.com/aH26jK8y

ABtexas1... at Yahoo! Answers Visit the source

Was this solution helpful to you?

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.