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 2 of 2

Thread: Methods

  1. #1
    Junior Member
    Join Date
    Mar 2020
    Location
    Wales UK
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Methods

    I have made a Method the problem is that I can't get it to work when I place it in my project.
    Either written the Method wrong or placing it in the wrong place in the project.
    My Method
           void getSecond(int secondNum){
            displayTable.setText("\n"); //Clear the displayTable 
            int firstNum, answerNum;
                for (firstNum = 1; firstNum <=12; firstNum ++){
                    answerNum = firstNum * secondNum;
                    displayTable.append(firstNum + " x " + secondNum + " = " + answerNum +"\n");
                 }
    My Project
    import javax.swing.*;
    import java.awt.Color;
    import java.awt.Font;
    import javax.swing.BorderFactory; 
    import javax.swing.border.Border;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
     
    public class MyTables
    {
     
    	public static void main(String[] args) 
    	{
    		JFrame frame = new JFrame("Tables"); // Creating instance of JFrame
            frame.setSize(800, 600); // Setting the width and height of frame
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    		JPanel panel = new JPanel(); //Creating panel. Inside panels we can add textfields, buttons and other components.
    		panel.setBorder(BorderFactory.createLineBorder(Color.RED));
            frame.add(panel); // Add panel to frame
            placeComponents(panel);  //Call user defined method for adding components to the panel.
    		frame.setVisible(true); // Setting the frame visibility to true
        }
     
    	private static void placeComponents(JPanel panel) 
    	{
    			panel.setLayout(null);
    			JTextArea displayTable = new JTextArea("Text Area");
    			displayTable.setEditable(false);
    			displayTable.setColumns(20);
    			displayTable.setFont(new Font("Times New Roman", 1, 14));
    			displayTable.setLineWrap(true);
    			displayTable.setRows(14);
    			displayTable.setWrapStyleWord(true);
    			displayTable.setBounds(200,20,230,260); 
    			displayTable.setBorder(BorderFactory.createLineBorder(Color.GREEN));
    			displayTable.setMaximumSize(new java.awt.Dimension(5, 22));
    			panel.add(displayTable);
     
    			JLabel userLabel = new JLabel("Enter a Number");  // Creating JLabel
    			userLabel.setHorizontalAlignment(SwingConstants.CENTER);
    			userLabel.setFont(new Font("Verdana", Font.ITALIC, 16)); //set font
    			userLabel.setBounds(10,20,180,25); // Specifies the location and size of component. (x,y,x1,y1)
    			userLabel.setBorder(BorderFactory.createLineBorder(Color.BLUE));	
    			panel.add(userLabel); // Add Label to panel
     
    			JTextField userText = new JTextField(20); //Create a textfield
    			userText.setBounds(10,50,165,25);
    			panel.add(userText); // Add TextField to pane
     
    			JButton loginButton = new JButton("Enter"); // Create button
    			loginButton.setBounds(10, 80, 80, 25);
    			panel.add(loginButton); // Add loginButton to panel
     
     
    		loginButton.addActionListener(new ActionListener()
    		{
    			public void actionPerformed(ActionEvent e)
    				{
    					//getSecond(6);
    					displayTable.setText("Button Pressed");
    				}
     
    		});
        }		
    }

  2. #2
    Junior Member
    Join Date
    Mar 2020
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Methods

    First two things I notice in your code:

    public void actionPerformed(ActionEvent e)
    {
    //getSecond(6);
    displayTable.setText("Button Pressed");
    }

    1. getSecond(6) is commented out but I'm sure this was done on purpose.
    2. setText(...) will delete the previous contents of the displayTable and replace it with the given string.

    Try removing the setText(...) and commenting out getSecond(6)

Similar Threads

  1. Returning methods to main() and other methods
    By jonmackey22 in forum Object Oriented Programming
    Replies: 4
    Last Post: September 25th, 2014, 04:36 PM
  2. Why and where abstract methods & classes and static methods are used?
    By ajaysharma in forum Object Oriented Programming
    Replies: 7
    Last Post: July 14th, 2012, 01:16 AM
  3. How to use Get and Set methods
    By jessenmutta in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 25th, 2012, 09:05 PM
  4. methods help
    By hockey87 in forum AWT / Java Swing
    Replies: 1
    Last Post: March 9th, 2010, 11:57 PM
  5. Re-using methods?
    By Morevan in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 26th, 2010, 05:04 PM