Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Combine Prime Factor Code into JPanel

  1. #1
    Junior Member
    Join Date
    Jun 2012
    Posts
    10
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Combine Prime Factor Code into JPanel

    I have a working JFrame GUI with my JPanel all setup. I am trying to combine two different codes that I've got setup and working. The first code was a text converter toUpperCase in a JPanel, and the second is a Prime Factor (not prime numbers) code. I've been trying to get the JPanel to give an output of Prime Factors for any number that a user inputs. Here is what I have....

    public class Prime extends JPanel { 
        private JLabel formattedText;
     
        public static void main(String[] args) {
            JFrame frame = new JFrame();
            frame.getContentPane().add(new Prime());
            frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
            frame.setVisible(true);
        }
     
        public Prime(){
            setLayout(new BoxLayout(this,BoxLayout.Y_AXIS));
            JPanel panel = new JPanel(); panel.setPreferredSize(new Dimension(640,100));
            JLabel label = new JLabel("Enter a number to check for it's prime number(s).");
            JTextField field = new JTextField("0");
            field.addActionListener(new FieldListener());
            add(label);
            add(field);
            add(panel);
     
     
            panel = new JPanel(); panel.setPreferredSize(new Dimension(640,380));
            formattedText = new JLabel();
            panel.add(formattedText);
            add(panel);
        }
     
        private class FieldListener implements ActionListener {
            public void actionPerformed(ActionEvent event) {
                JTextField field = (JTextField)event.getSource();
                    formattedText.setText(field.getText().toUpperCase()); // I know this is wrong... I can't figure out what to change here to get it to pull the code below.
                }
        }
        public class PrimeFactors {
     
        }
    }

    and here is the Prime Factor code

    public class Temp {
     
        static int primeCheck = 1;
     
        public static void main(String[] args) {
            System.out.println("Enter a number whose Prime factors are desired: ");
            Scanner numS = new Scanner(System.in);
            int numPriFac = 0;
            if (numS.hasNextInt()) {
                numPriFac = numS.nextInt();
            }
     
            System.out.println("All the Prime Factors of the entered number are:");
     
            for (int tap = 1; tap <= numPriFac; tap++) {
                if (numPriFac % tap == 0) {
                    for (int primeTest = 2; primeTest < tap; primeTest++) {
                        if (tap % primeTest == 0) {
                            primeCheck = 1;
                            break;
                        } else {
                            primeCheck = 0;
                        }
                    }
                    if (primeCheck == 0 || tap == 2) {
                        System.out.print(tap + " ");
                    }
                }
            }
        }
    }

    That last PrimeFactors code in the bottom is just something left over from when I was trying to get it working on my own. Thanks so much for any help!!!


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Combine Prime Factor Code into JPanel

    JPanel to give an output of Prime Factors for any number
    Not sure what a JPanel has to do with computing PFs.
    Does the code correctly compute the numbers you want to see but there is some problem displaying them in the JPanel?
    Or is the problem in the creation of the numbers (nothing to do with displaying them in a JPanel)?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Jun 2012
    Posts
    10
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Combine Prime Factor Code into JPanel

    The code for the GUI works and the code for the JPanel works, but I jwant to combine the two codes so that I can have a GUI for the user to input a number to have it's Prime Factors returned.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Combine Prime Factor Code into JPanel

    You could use a JTextField for receiving the user input.
    You could use a JTextArea for displaying the computed numbers.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. [SOLVED] I'm trying to combine 2 files into 1. Please help
    By mickey2012 in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: March 7th, 2012, 08:31 PM
  2. Prime Number Code Help!
    By aandcmedia in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 7th, 2012, 12:07 AM
  3. Need help correcting my code for calculating sexy prime pairs
    By BinaryPenguin14 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: November 23rd, 2011, 04:05 AM
  4. help factor
    By gonfreecks in forum What's Wrong With My Code?
    Replies: 13
    Last Post: November 7th, 2010, 03:49 AM
  5. Need Help - Factoring & Prime Finding Code
    By prodigytoast in forum Algorithms & Recursion
    Replies: 5
    Last Post: November 5th, 2009, 07:38 AM