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

Thread: Problem with Jbutton

  1. #1
    Junior Member
    Join Date
    Aug 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Problem with Jbutton

    Hello,

    I am having problem with the program below. I can get the nextJButton to work but the previousJButton won't. previousJButton is supposed to make the page number go down, but it won't reflect on screen(in a label).

    private class ButtonHandler implements ActionListener
    {
          public void actionPerformed(ActionEvent e)
         {
     
                label10.setText("");
                Page++;
                Object src=e.getSource();
     
               System.out.printf("%d",Page);
               if(src==nextJButton) //this button works fine
               {
     
    	        if (Page==1)
                    {
     
                               label1.setText(formatter5.toString());
    			   label2.setText(formatter6.toString());
                               label3.setText(formatter7.toString());					  
     
    			   String labelText = ( "Page number: " + (Page+1) );
    			   label10.setText( labelText );
    	        }
     
                   else
                   {
     
                          label1.setText(formatter5.toString());
                          label2.setText(formatter3.toString());
                          label3.setText(formatter7.toString());
     
    		      String labelText = ( "Page number: " + (Page+1) );
    		      label10.setText( labelText );
     
    		}
     
                }
     
    		if(src==previousJButton)//here is the problem button
                    {
     
                         label10.setText("");
    		     Page--;
    		     String labelText = ( "Page number: " + (Page+1) );
    		     label10.setText( labelText );
     
    		}
     
    	}
        }

    Here is the whole program so you can run it

    import java.util.Vector;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.io.*;
    import java.awt.BorderLayout;
    import java.awt.FlowLayout;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import java.awt.GridLayout;
    import javax.swing.JTextField;
    import javax.swing.JOptionPane;
    import java.util.Formatter;
    import java.util.Random;
     
     
    public class RunnableDataEntry extends JFrame implements FocusListener,KeyListener,ActionListener
    {
    	private JButton nextJButton;
    	private JButton previousJButton;
     
    	private JLabel label1;
    	private JLabel label2;
    	private JLabel label3;
    	private JLabel label10;
     
    	private JPanel Panel1;
    	private JPanel Panel2;
    	private JPanel Panel3;
    	private JPanel Panel4;
    	private JPanel Panel5;
     
    	private BorderLayout layout;
    	private GridLayout gridLayout1;
    	private FlowLayout flowLayout1;
     
    	private int Page;
     
     
     
        private int array[]=new int[24];
        //private int array[]=new int[2];
     
        Formatter formatter=new Formatter();
        Formatter formatter2=new Formatter();
        Formatter formatter3=new Formatter();
        Formatter formatter4=new Formatter();
        Formatter formatter5=new Formatter();
        Formatter formatter6=new Formatter();
        Formatter formatter7=new Formatter();
        Formatter formatter8=new Formatter();
     
        public RunnableDataEntry()
    	{
    		super("Runnable Data Entry");
     
    		layout = new BorderLayout(5,10);
                    nextJButton=new JButton("Next Page");
                    previousJButton=new JButton("Previous Page");
    		flowLayout1= new FlowLayout();
    		setLayout(layout);
    		Panel1 = new JPanel();
    		Panel2=new JPanel();
    		Panel3=new JPanel();
    		Random randomNumber=new Random();
     
    		for(int counter=0;counter<array.length;counter++)
    		{
     
                     array[counter]=randomNumber.nextInt(10);
     
    		}
     
    formatter.format("%d%d%d%d",array[0],array[1],array[2],array[3]);
    		formatter2.format("%d%d%d%d",array[4],array[5],array[6],array[7]);
    		formatter3.format("%d%d%d%d",array[8],array[9],array[10],array[11]);
    		formatter5.format("%d%d%d%d",array[12],array[13],array[14],array[15]);
    		formatter6.format("%d%d%d%d",array[16],array[17],array[18],array[19]);
    		formatter7.format("%d%d%d%d",array[20],array[21],array[22],array[23]);
     
     
     
    		label1=new JLabel(formatter.toString());
    		label2=new JLabel(formatter2.toString());
    		label3=new JLabel(formatter3.toString());
    		label10=new JLabel(formatter8.toString());
     
    		add(Panel3,BorderLayout.WEST);
    			Panel3.add(label10);
     
    		add(Panel1,BorderLayout.NORTH);
    			Panel1.setLayout(flowLayout1);
    			Panel1.add(label1);
    			Panel1.add(label2);
    			Panel1.add(label3);
     
    		add(Panel2,BorderLayout.SOUTH);
    			Panel2.add(nextJButton);
    			Panel2.add(previousJButton);
     
    		ButtonHandler handler=new ButtonHandler();
    		nextJButton.addActionListener(handler);
    		previousJButton.addActionListener(handler);
     
    		label10.setText("");
    		String labelText = ( "Page number: " + (Page+1) );
    		label10.setText( labelText );
     
    	}
     
    		private class ButtonHandler implements ActionListener
    		{
     
     
     
            	public void actionPerformed(ActionEvent e)
            	{
     
                    label10.setText("");
                    Page++;
                    Object src=e.getSource();
     
                    System.out.printf("%d",Page);
                     if(src==nextJButton) //this button works fine
                    {
     
    	              if (Page==1)
                          {
     
                               label1.setText(formatter5.toString());
    			   label2.setText(formatter6.toString());
                               label3.setText(formatter7.toString());					  
     
    			   String labelText = ( "Page number: " + (Page+1) );
    			   label10.setText( labelText );
    		           }
     
                       else
                       {
     
                          label1.setText(formatter5.toString());
                          label2.setText(formatter3.toString());
                          label3.setText(formatter7.toString());
     
    		      String labelText = ( "Page number: " + (Page+1) );
    		      label10.setText( labelText );
     
    		       }
     
    		}
     
    		if(src==previousJButton)//here is the problem button
                    {
     
                         label10.setText("");
    		     Page--;
    		     String labelText = ( "Page number: " + (Page+1) );
    		     label10.setText( labelText );
     
    		}
     
    	}
        }
     
        public void focusGained(FocusEvent e)
        {
     
        }
     
     
        public void focusLost(FocusEvent e)
        {
     
        }
     
     
        public void keyTyped(KeyEvent e)
        {
     
        }
     
        public void keyPressed(KeyEvent e)
        {
     
        }
     
     
        public void keyReleased(KeyEvent e) {
     
        }
     
     
        public void actionPerformed(ActionEvent event)
        {
     
        }
     
    }


    I use this in a separate file to get the program to run
    import javax.swing.JFrame;

    public class RunnableDataEntryTest
    {
    	public static void main(String args[])
    	{
    		RunnableDataEntry runnabledataEntry = new RunnableDataEntry();
    	      runnabledataEntry.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		runnabledataEntry.setSize(500,500);
    		runnabledataEntry.setVisible( true );
            }
    }


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Problem with Jbutton

    Please describe what's not working. What is the previousJButton supposed to do? What is it doing instead, if anything?

    The code you've posted is the kind of (forgive me) crap people get from graphical code builders who have not taken the time to rename the variables, add meaningful comments, etc. People who post code like this give the graphical code builders an even worse rep than they already have which is already well deserved. Please learn to use the graphical code builder to your advantage rather than to your doom and then post THAT code.

  3. #3
    Junior Member
    Join Date
    Aug 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problem with Jbutton

    Sorry for how sloppy the code was, I have fixed it up some.

Similar Threads

  1. Problem with setIcon or something(JButton)
    By leonne in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 6th, 2013, 08:41 PM
  2. problem of creating matrix with JButton or Panel
    By ms_ceng in forum AWT / Java Swing
    Replies: 3
    Last Post: December 16th, 2011, 04:53 PM
  3. JButton constructor problem
    By Nieuwenhuizen-jk in forum AWT / Java Swing
    Replies: 14
    Last Post: September 12th, 2011, 11:35 AM
  4. Problem with java swing JButton in Netbeans IDE
    By vrp in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 12th, 2011, 11:38 PM
  5. JButton set background problem
    By ellias2007 in forum AWT / Java Swing
    Replies: 1
    Last Post: February 25th, 2010, 12:15 AM