Jump to content


 


Register a free account to unlock additional features at BleepingComputer.com
Welcome to BleepingComputer, a free community where people like yourself come together to discuss and learn how to use their computers. Using the site is easy and fun. As a guest, you can browse and view the various discussions in the forums, but can not create a new topic or reply to an existing one unless you are logged in. Other benefits of registering an account are subscribing to topics and forums, creating a blog, and having no ads shown anywhere on the site.


Click here to Register a free account now! or read our Welcome Guide to learn how to use this site.

Photo

Trying to create a GUI events with Java


  • Please log in to reply
14 replies to this topic

#1 sidd0123

sidd0123

  • Banned
  • 81 posts
  • OFFLINE
  •  
  • Local time:02:29 PM

Posted 01 March 2010 - 12:56 PM

I'm trying to create a GUI events using the language Java. I have a working GUI and I'll post the code here.

//This is my package
package package09;

//Imports the necessary files to run the program

import java.awt.*;
import javax.swing.*;
import javax.swing.border.Border;

// This is the class boxDemo which comes from JFrame

class boxDemo extends JFrame {

	//Holds the details of my window

	public boxDemo() {

		// Name of the window
		super("Computer Science E-mail Server by Sid Patel");

		// set the dimensions (width, height) of the frame (in pixels)
		setSize(750, 750);

		// decides what to do when the frame is closed
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		// make it visible
		setVisible(true);

		// create a Content container for the frame
		Container contentArea = getContentPane();
		contentArea.setBackground(Color.CYAN);

		// set up a layoutManager for the Container
		BorderLayout myManager = new BorderLayout();
		contentArea.setLayout(myManager);

		//___________________________________________________ Panel divider

		//South Panel and color
		JPanel South = new JPanel ();
		South.setBackground (Color.GREEN);

		// Create and add a "New User: Sign-Up" button and location
		JButton depositButton = new JButton("New User: Sign-Up");
		South.add(depositButton, BorderLayout.SOUTH);

		// Create and add a "Forgot Password?" button and location
		JButton balanceButton = new JButton("Forgot Password?");
		South.add(balanceButton, BorderLayout.SOUTH);

		//Add the items to the window in that location
		contentArea.add (South, BorderLayout.SOUTH);

		//___________________________________________________ Panel divider

		//North Panel and color
		JPanel North = new JPanel ();
		North.setBackground (Color.BLUE);

		//Create and add a "E-mail Address: " text label and location
		JLabel field1Label = new JLabel("E-mail Address: ");
		North.add(field1Label, BorderLayout.NORTH);

		//Create and add a "Enter E-mail Address" text field and location
		JTextField field1 = new JTextField("Enter E-mail Address",15);
		North.add(field1, BorderLayout.NORTH);

		////Create and add a "Password: " text label and location
		JLabel field2Label = new JLabel("Password: ");
		North.add(field2Label, BorderLayout.NORTH);

		//Create and add a "Enter E-mail Address" text field and location
		JTextField field2 = new JTextField("Enter Password",15);
		North.add(field2, BorderLayout.NORTH);

		//Add the items to the window in that location
		contentArea.add (North, BorderLayout.NORTH);

		//___________________________________________________ Panel divider		

		//Create and add a new panel in that location
		JPanel Center = new JPanel ();
		contentArea.add (Center, BorderLayout.CENTER);

		//Sets the background color and initiates the buttons
		Center.setBackground (Color.YELLOW);
		ButtonGroup sizes = new ButtonGroup();


		// Create radio buttons for the group
		JRadioButton sizeSmall  = new JRadioButton("Small Text");
		JRadioButton sizeLarge  = new JRadioButton("Large Text");

		// Add the sizes to the group

		sizes.add(sizeSmall);
		sizes.add(sizeLarge);


		//Add the items to the window in that location
		Center.add(sizeSmall, BorderLayout.CENTER);
		Center.add(sizeLarge, BorderLayout.CENTER);

		//___________________________________________________ Panel divider

		// Create and add a new panel and color
		JPanel East = new JPanel ();
		East.setBackground (Color.RED);

		//Create a new checkbox in that location
		JCheckBox mySpam = new JCheckBox("Remember my E-mail Address");
		East.add(mySpam, BorderLayout.EAST);

		//Create a new checkbox in that location
		JCheckBox myKindling = new JCheckBox("Remember my password");
		East.add(myKindling, BorderLayout.EAST);

		//Add the items to the window in that location
		contentArea.add (East, BorderLayout.EAST);
		//___________________________________________________ Panel divider

		// Create a new panel and set it a certain color
		JPanel West = new JPanel ();
		West.setBackground (Color.CYAN);

		//Create a new JComboBox and add it to that location
		JComboBox myCustomers = new JComboBox();
		West.add(myCustomers, BorderLayout.WEST);

		// Add items to the ComboBox
		myCustomers.addItem("Previous Users");
		myCustomers.addItem("computerscience@compsci.com");
		myCustomers.addItem("myGuidemo@compsci.com");
		myCustomers.addItem("eclipse@compsci.com");

		//Add the items to the window in that location
		contentArea.add (West, BorderLayout.WEST);

		//Create a new JComboBox in that location
		JComboBox myCustomers2 = new JComboBox();
		West.add(myCustomers2, BorderLayout.WEST);

		// Add items to the ComboBox
		myCustomers2.addItem("Select Server");
		myCustomers2.addItem("Server 1");
		myCustomers2.addItem("Server 2");
		myCustomers2.addItem("Server 3");

		//Add the items to the window in that location
		contentArea.add (West, BorderLayout.WEST);

		//___________________________________________________ Panel divider

		// Create a new panel and set it a color
		JPanel panel1 = new JPanel();
		panel1.setBackground(Color.BLUE);

		//Create a new JButton and add it to the panel
		JButton stopButton = new JButton("Sign In");
		panel1.add(stopButton);

		//Add panel in the center
		Center.add(panel1);

		//Create new panel and set it a color 
		JPanel panel2 = new JPanel();
		panel2.setBackground(Color.RED);

		//Create a Button and add it to the panel
		JButton goButton = new JButton("Clear Form");
		panel2.add(goButton);

		//Add panel to window
		Center.add(panel2);

		// Actually make the changes
		setContentPane(contentArea);
	}
}

//This is my class
public class myGUIevents {

	//This is the class that will run the Demo
	public static class panelDemo {

		//This is the main method
		public static void main (String[] args) {

			// create a new boxDemo object
			boxDemo empty = new boxDemo( );
		}
	}
}


When you run it and the window pops up, please click maximize so that the whole frame shows up because if you don;t, the middle panel is cut off.


This is what I wanted to do.

I want to enter an email address and password and when someone clicks sign in a progress bar shows up loading. I know that there is a progress bar method somewhere in java docs but i couldnt find it. Can someone advise me on how to proceed with my program?

BC AdBot (Login to Remove)

 


#2 groovicus

groovicus

  • Security Colleague
  • 9,963 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Centerville, SD
  • Local time:12:29 PM

Posted 01 March 2010 - 01:08 PM

I know that there is a progress bar method somewhere in java docs but i couldnt find it.

Perhaps you were looking for this?

#3 sidd0123

sidd0123
  • Topic Starter

  • Banned
  • 81 posts
  • OFFLINE
  •  
  • Local time:02:29 PM

Posted 04 March 2010 - 01:12 PM

actually, it seems i cant use a progress bar for what i am doing. but I did find another interesting container i could use thanks

#4 sidd0123

sidd0123
  • Topic Starter

  • Banned
  • 81 posts
  • OFFLINE
  •  
  • Local time:02:29 PM

Posted 04 March 2010 - 01:16 PM

// Create a new panel and set it a color
JPanel panel1 = new JPanel();
panel1.setBackground(Color.BLUE);

//Create a new JButton and add it to the panel
JButton stopButton = new JButton("Sign In");

//Where member variables are declared:
JProgressBar progressBar;

//Where the GUI is constructed:
progressBar = new JProgressBar(0, panel1.getLengthOfTask());
progressBar.setValue(0);
progressBar.setStringPainted(true);

panel1.add(stopButton);





//Add panel in the center
Center.add(panel1);



could you help me figure out what i need to change to the italicised and underlined part for my code to work? I'm not sure what should go there

#5 groovicus

groovicus

  • Security Colleague
  • 9,963 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Centerville, SD
  • Local time:12:29 PM

Posted 04 March 2010 - 01:26 PM

getLEngthOfTask is not a method associated with a Jpanel, so the code you are using must be extending JPanel. I don't know what code you used, so I have no way of telling you what to do to fix it.

#6 sidd0123

sidd0123
  • Topic Starter

  • Banned
  • 81 posts
  • OFFLINE
  •  
  • Local time:02:29 PM

Posted 04 March 2010 - 09:50 PM

I used java code language. i basically took a part of my code, the area that i wanted the progress bar method utilized, from that huge piece i copied into my first post.

Is that what you needed???

Edited by sidd0123, 04 March 2010 - 09:51 PM.


#7 groovicus

groovicus

  • Security Colleague
  • 9,963 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Centerville, SD
  • Local time:12:29 PM

Posted 05 March 2010 - 10:30 AM

Ok, show me in that first piece of code you copied in where getLengthOfTask() came from? I have searched through the code half a dozen times, and I am just not seeing it there. And since I don't see it there, I have to assume that you got it from somewhere else.

#8 sidd0123

sidd0123
  • Topic Starter

  • Banned
  • 81 posts
  • OFFLINE
  •  
  • Local time:02:29 PM

Posted 05 March 2010 - 12:45 PM

yeah, i got it from the link that you gave me. This is the code in context, the getLengthOfTask() is in bold in the context


Below is the code from ProgressBarDemo.java that creates and sets up the progress bar:
//Where member variables are declared:
JProgressBar progressBar;
...
//Where the GUI is constructed:
progressBar = new JProgressBar(0, task.getLengthOfTask());
progressBar.setValue(0);
progressBar.setStringPainted(true);

The constructor that creates the progress bar sets the progress bar's minimum and maximum values. You can also set these values with setMinimum and setMaximum. The minimum and maximum values used in this program are 0 and the length of the task, which is typical of many programs and tasks. However, a progress bar's minimum and maximum values can be any value, even negative. The code snippet also sets the progress bar's current value to 0.
The call to setStringPainted causes the progress bar to display, within its bounds, a textual indication of the percentage of the task that has completed. By default, the progress bar displays the value returned by its getPercentComplete method formatted as a percent, such as 33%. Alternatively, you can replace the default with a different string by calling setString. For example,

if (/*...half way done...*/)
progressBar.setString("Half way there!");

When the user clicks Start, an instance of the inner class Task is created and executed.

public void actionPerformed(ActionEvent evt) {
startButton.setEnabled(false);
setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
done = false;
task = new Task();
task.addPropertyChangeListener(this);
task.execute();

#9 groovicus

groovicus

  • Security Colleague
  • 9,963 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Centerville, SD
  • Local time:12:29 PM

Posted 05 March 2010 - 01:23 PM

Yes, except that you tried to call that method on a panel object, which can't be done. In the sample code, it is called on a task object.

I'm sorry I can't really help you with this because you are trying to do something that you don't yet know how to do. The only way you will be able to follow those examples is is you have an understanding of how objects and methods work, and I don't really have that kind of time to teach you java. You can't just cut and paste code together and expect it to work without understanding some fundamentals, like why one can't apply methods associated with a 'Task' object to a 'Jpanel' for instance. You should take the time to create a standalone project that simply displays a status indicator for 5 seconds or something, and then once that works, incorporate it into your application. That's how it works in the real world, and it makes it easier to concentrate on your problem rather than trying to figure the problem while trying to incorporate it into production code.

#10 sidd0123

sidd0123
  • Topic Starter

  • Banned
  • 81 posts
  • OFFLINE
  •  
  • Local time:02:29 PM

Posted 11 March 2010 - 01:00 PM

ok, i read a bunch of tutorials on JAVA website on the basics and i think that i got it.

i found something that i would like to do that stays with in the Panel parameters.

In the following code:

[codebox]// Create a new panel and set it a color
JPanel panel1 = new JPanel();
panel1.setBackground(Color.BLUE);

//Create a new JButton and add it to the panel
JButton stopButton = new JButton("Sign In");

panel1.add(stopButton);

//Add panel in the center
Center.add(panel1);

//Create new panel and set it a color
JPanel panel2 = new JPanel();
panel2.setBackground(Color.RED);

//Create a Button and add it to the panel
JButton goButton = new JButton("Clear Form");
panel2.add(goButton);

//Add panel to window
Center.add(panel2);

// Actually make the changes
setContentPane(contentArea);
}[/codebox]

I would like to put in an action listener that will change the background color when a button is pushed. I have tried integrating both pieces of code, but i am having trouble getting it to work correctly

This is what I want to add to the code.

[codebox]contentArea = getContentPane();
contentArea.setBackground(Color.YELLOW);

// set up a layoutManager for the Container

FlowLayout myManager = new FlowLayout();
contentArea.setLayout(myManager);

// Create and add a "Red" button

redButton = new JButton("Red");
redButton.addActionListener(this);
contentArea.add(redButton);

// Create and add a "Green" button

greenButton = new JButton("Green");
greenButton.addActionListener(this);
contentArea.add(greenButton);

// Create and add a "Blue" button

blueButton = new JButton("Blue");
blueButton.addActionListener(this);
contentArea.add(blueButton);

// Create a counter area

clickCounter = new JTextField(Integer.toString(counter), 15);
clickCounter.setHorizontalAlignment(JTextField.CENTER);
contentArea.add(clickCounter);

// Create and add a "Reset" button

resetButton = new JButton("Reset");
resetButton.addActionListener(this);
contentArea.add(resetButton);

setContentPane(contentArea);
}

/*
* This event handler handles clicking of the buttons.
* It changes the color of the screen accordingly and manages the click counter.
*
* (non-Javadoc)
* @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
*/

public void actionPerformed(ActionEvent event) {

// Find the label of the button generating the event
// for now, we do nothing with it.

System.out.println("getActionCommand returned '" + event.getActionCommand() + "'.");

// enabled all color buttons

redButton.setEnabled(true);
greenButton.setEnabled(true);
blueButton.setEnabled(true);

// Look for the Reset button first.

if (event.getSource() == resetButton) {
counter = 0;
clickCounter.setText("Counter reset to " + counter);
contentArea.setBackground(Color.YELLOW);
} else {

// otherwise, must have been a color button... color screen and disable the button
if (event.getSource() == redButton) {
contentArea.setBackground(Color.RED);
redButton.setEnabled(false);
}
if (event.getSource() == greenButton) {
contentArea.setBackground(Color.GREEN);
greenButton.setEnabled(false);
}
if (event.getSource() == blueButton) {
contentArea.setBackground(Color.BLUE);
blueButton.setEnabled(false);
}

// increment and display the current click counter value

counter++;
clickCounter.setText("Over " + Integer.toString(counter) + " clicks served.");
}
}
}[/codebox]

#11 groovicus

groovicus

  • Security Colleague
  • 9,963 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Centerville, SD
  • Local time:12:29 PM

Posted 11 March 2010 - 01:09 PM

In addition to the code, it would be nice if you let us now what happened specifically. "i am having trouble getting it to work correctly" really doesn't tell us anything. Is there an error message? Does nothing at all happen?

#12 sidd0123

sidd0123
  • Topic Starter

  • Banned
  • 81 posts
  • OFFLINE
  •  
  • Local time:02:29 PM

Posted 11 March 2010 - 09:33 PM

srry, i fixed the problem. I left a piece of code that i want supposed to keep in.


in this piece of code:

// Create a new JButton and add it to the panel
JButton stopButton = new JButton("Blue");
stopButton.addActionListener();

panel1.add(stopButton);


What parameter goes in the stopButton.addActionListener(what goes in here)

i was looking through my teachers sample code an he has the word "this" in it.

#13 paton

paton

  • Members
  • 10 posts
  • OFFLINE
  •  
  • Local time:07:29 PM

Posted 26 March 2010 - 04:55 PM

When you are inside a method, the word this refers to the method itself, so by putting this in the add.ActionListener(this) then you are calling the stopButton as a parameter in your case so that you can then write the code for the stop button in the method that handles that.

#14 groovicus

groovicus

  • Security Colleague
  • 9,963 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Centerville, SD
  • Local time:12:29 PM

Posted 26 March 2010 - 05:06 PM

Actually, in Java, the 'this' keyword refers to the object itself.

Used to represent an instance of the class in which it appears. this can be used to access class members and as a reference to the current instance. The this keyword is also used to forward a call from one constructor in a class to another constructor in the same class.

Source

#15 sidd0123

sidd0123
  • Topic Starter

  • Banned
  • 81 posts
  • OFFLINE
  •  
  • Local time:02:29 PM

Posted 01 April 2010 - 07:54 PM

Hey, I still need help on the GUI.


I wanted to add an actionlistener (I think thats right). I have two radio buttons, one is small window, the other large window. When someone selects the radiobutton, i want it to resize the window to a predetermined size. This is a snippet of my radio buttons code. By the way, i have different panels in my code. If you need to see the whole think please tell me.
[codebox]//Create and add a new panel in that location

JPanel Center = new JPanel ();

contentArea.add (Center, BorderLayout.CENTER);

//Sets the background color and initiates the buttons

Center.setBackground (Color.YELLOW);

ButtonGroup sizes = new ButtonGroup();



// Create radio buttons for the group

JRadioButton sizeSmall = new JRadioButton("Small Window");

JRadioButton sizeLarge = new JRadioButton("Large Window");

// Add the sizes to the group

sizes.add(sizeSmall);

sizes.add(sizeLarge);



//Add the items to the window in that location

Center.add(sizeSmall, BorderLayout.CENTER);

Center.add(sizeLarge, BorderLayout.CENTER);[/codebox]







Could you guide me through the process.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users