JPanel Class That Creates Left And Right Panel

Feb 12, 2015

I have a JPanel class that creates a left panel and a right panel. the left panel and right panel both have one button each. What I’m trying to accomplish is when I click on the button in the left panel that it updates the button in the right panel. I have to do this without changing anything in the right panel.Basically I have to have LeftPanel have a way to keep track of an instance of RightPanel and I would have to do this through the JPanel class. But I have no clue where to begin on even accomplishing this.

View Replies


ADVERTISEMENT

Draw Panel On A Frame - Can't See JPanel

Apr 14, 2015

I've been trying to draw a panel on a frame with simple drawString drawRect and oval but when i run it the frame shows up with the exit on close stuff but nothing will show inside . this is my code :

import java.awt.Graphics;
import javax.swing.JPanel;
import javax.swing.JFrame;
class Draw extends JPanel{
public void paintcomponents(Graphics g){
super.paintComponent(g);
g.drawLine(10,15,20,30);

[Code] .....

I'm not sure what's wrong , probably something to do from this pc ? since in the lab work at uni it worked fine .

View Replies View Related

Driver Program That Creates 2 Instances Of A Class?

May 10, 2014

public class Car
{
//instance variables ----------------------
private String make;
private String model;
private int year;
private double vehiclePrice;
private double downPayment;
private double milesPerGallon;

[code]....

I created this class "Car" (also not sure if it's correct) and need to write a driver program that creates two instances of the class Car. One must use the default constructor, and the other must use the non-default constructor. It must demonstrate the methods used in the Car class using those instances.

public class DriverCar
{
public static void main(String[] args) {
Car car1 = new Car("Toyota", "Corolla", 2013, 20000, 3000, 35);
Car car2 = new Car("Ford", "Taurus", 2005, 14000, 1500, 25);
System.out.println(car1);

[Code] ....

View Replies View Related

Placing JPanel From A Class Into Frame Of Another Class

Aug 19, 2014

I can't figure out how to place a JPanel from a class(Game) in to a another class(Core). I have bean stuck on this for about three days now I bean doing my research but no luck so now I think I should ask directly so here I am. Now to state clearly I am trying to place a JPanel named Game_Screen witch is in another class named Game into a JFrame named frame witch is in a another class named Core.

Now both of this classes are with in the same package and my IDE Is Eclipse.The JPanel Game_Screen has other swing pieces attached to it that should also be added with the panel on to the frame.But the change is screen should only happen when I click the buttons New Legend(N),Load Legend(L),Help(H) what already happens is that when you click any of this buttons menu.setVisible(false); is activated making everything on screen at the time disappear from the screen but the panel does not a pear.

Here's my code:

//Class - Core-------
package Source;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class Core {
static Game GG = new Game();
public static void main(String[] args) {
JFrame frame = new JFrame();

[Code]...

View Replies View Related

How To Use Data From Login Panel In Another Class

Aug 4, 2014

I am getting data from user and checking it with database (MS SQL ) if it fits it is switching to new window. In new window I need to use again my database so I need to add connecting data again. I would like to get them from login window. How to do that? I know theory but when I am writing code nothing works. Here is working code for login panel :

/*import java.awt.BorderLayout;
import javax.swing.*;
public class Login extends JFrame {

[Code].....

View Replies View Related

Swing/AWT/SWT :: Panel From Nested Class Does Not Display

Sep 26, 2014

I have this panel in a nested class in the MainPanel class and it does not display, when I launch the applet, or more like, the two menus "LineColor" and "BackColor" from the nested class ColorPanel do not display .

This is the code:

package ui.panels;
import interfaces.Resettable;
import java.awt.Color;
import java.awt.Component;
import java.awt.GridLayout;
import java.awt.Panel;

[code].....

View Replies View Related

Swing/AWT/SWT :: How To Use Data From Login Panel In Another Class

Aug 4, 2014

I am getting datas from user and checking it with database (MS SQL ) if it fits it is sweatching to new window. In new window I need to use again my database so I need to add connecting datas again. I would like to get them from login window. How to do that? I know theory but when I am writting code nothing works. Here is working code for login panel :

/*import java.awt.BorderLayout;
import javax.swing.*;
public class Login extends JFrame {
public String url;
public static void main(String[] args) {
Log frameTabel = new Log();

[Code] .....

View Replies View Related

Swing/AWT/SWT :: When Click On Button From Bottom Panel Top Panel Need To Be Redrawn

Apr 1, 2014

When i click on the button from bottom panel, top panel need to be redrawn. But redrawing is not happening. It happens only when resizing the applet.

public class Graphics_Chart(){
public init(){
JScrollPane topPane = new JScrollPane(new ImagePanel2());
topPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
topPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_NEVER);

[code]...

View Replies View Related

How To Make Different Class For Each JPanel

Jan 31, 2014

I have made four apps and I want to put each of them in one project to make a big app.So,I have made a JFrame which has a lot of Jpanels inside.My question is how I can write different class for every Jpanel because I try to put all of them in one class,and it was not a good idea. Also, I do not want to open an other window when I choose an app. I want to have a Jpanel for every app. I use Cardlayout, I have read the tutorials but I can not understand. Also,I thought to make a different JFrame but I do not want to open a different window when I open a different app.

View Replies View Related

Ball Class For Pong Won't Add To JPanel

Jul 28, 2014

I am trying to make a game of pong and am having trouble adding my ball class to a JPanel. Here is what I have so far.

package pong;
import java.awt.*;
public class Ball{
private int xPos,yPos;
public int dx = 5, dy = -5;
public Ball()

[code].....

I want to be able to add this to a JPanel but it is not allowing me to add the ball. Here is my JPanel

package pong;
import java.awt.*;
import javax.swing.JPanel;
public class BallTestPanel

[Code] ....

It reads panel.add(ball); as an error. I says that I need to change the type ball to component.

View Replies View Related

JPanel Class Won't Create JLabel

Jun 1, 2014

So I am making a JPanel with two JLabels and it isn't working right.

import java.awt.*;
import javax.swing.*;
public class IntroPanel extends JPanel
{
public IntroPanel() {
setBackground(Color.green);

[Code] ....

For some reason

JLabel 11 = new JLabel("Layout Manger Demonstration");
JLabel 12 = new JLabel ("Choose a tab to see an example of a layout manger.");

add(11);
add(12);

is not registering the JLabel or add functions.

View Replies View Related

Loading JPanel From Another JPanel When Button Is Pressed

Jun 9, 2014

I am making a game in java and i want to make a title screen. What I am trying to do is make a jpanel and load another jpanel when a button is pressed in one of the japenls. Here is the code for the runner

import java.awt.BorderLayout;
import javax.swing.JFrame;
public class Runner extends JFrame {
// Have these set up so they can be seen everywhere
public static final int GAMEWIDTH = 540;
public static final int GAMEHEIGHT = 710;
static boolean loading = false;

[Code] ....

here is the code for the jpanel that loads the other jpanel

import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Rectangle;

[Code] ...

How do load a jpanel when a button is pressed in another jpanel

View Replies View Related

String Method That Creates A Word Out Of Given Letters?

Jan 25, 2015

I want to make a method that takes a word and then checks if the word can be created from available letters. For example, if a word "johnson" can be created by using letters "jashoqwnon".

Now my goal is to make sure that if available letters contain a letter from the word, that letter is put into a String called result and then erased from the list of given letters. So, "johnson" and "jashoqwn" would produce the result "johns" and leave "aqw" unused.

Now the problem that I am facing is that I can't get Java not to use the same letter twice. So "johnson" and "jashoqwn" still gives "johnson".

I've tried everything in my power but I am missing something. Here is my code.

public static String makeAWord(String word, String letters){
String result = "";
for(int i = 0; i < word.length(); i++){
for(int j = 0; j < letters.length() ; j++){

[Code] ....

View Replies View Related

Creates Two Die Objects And Roll Them Display The Values

May 4, 2014

I've written a die program, and i want the user to type any key in order to continue, how do i achieve this ?

I have already created a Die class, i just want to implement the main method now.

PHP Code:

package test;

/* This program demonstrates the use of a user-defined class

public class RollingDice {
// Creates two Die objects and rolls them display the values.
public static void main (String[] args) {
Die die1 = new Die();
System.out.println("Roll the die ")
}
} mh_sh_highlight_all('php');

What should i add after System.out.println, So that the user types (any key, or types "Enter" for example, for the die to roll). i dont want the die to roll by itself i Want the user to interact with the program in order for it to roll.

View Replies View Related

Working On A Program That Creates Array From A File

Feb 19, 2015

Working on a program that creates an array from a file, organizes it in various ways, and then prints the results. Right now I'm specifically having trouble with the printAry() method - when I try to run it, I receive a NullPointerException for the active line in printAry() (EDIT: line 102). Not sure which sections are important to this, so I will include the client class and most of the service. I realize that the amount of documentation I included is not necessary, but it is required for the class.

class Lab3StudentAryService
{
private int count;
private Lab3StudentData [] studentAry = new Lab3StudentData[50];

/*
@param: none
@return: none
reads file and converts to array.
*/
public void fillAry() //constructor
{
Scanner scan = new Scanner(System.in);

[code]....

View Replies View Related

How To Switch From One Panel To Another Panel

Apr 5, 2014

I have two(panel1 and panel2) panels in a jframe,all three of them are different classes .

In jframe there is a display panel on which the panel1 and panel2 are called and displayed.

now i have a button in panel1 .i want that when i will click on that button panel2 will be displayed or added to the dispalypanel of jframe.

View Replies View Related

NullPointerException Adding JPanel To JPanel

Jun 17, 2014

I have a Main() class that extends JPanel and I'm trying to add Card() that also extends JPanel.When I add a Card() or Main() object to the JFrame directly, it works fine. The problem arises when I try to add a Card() object to the Main() object.

I edited the code a little bit to make it easier to read so hopefully it's fine.

Main() code:
public class Main extends JPanel implements Runnable {
// HARDCODED OPTIONS
public static final int WIN_WIDTH = 1200;
public static final int WIN_HEIGHT = 800;

public static final int GRID_X = 9;
public static final int GRID_Y = 5;

[Code] ...

View Replies View Related

Constructor That Creates A New Object With Data From Array Values?

Feb 11, 2015

So I want to write a constructor that creates a new object with the data from the array values. I don't know where to start. It's the last method in the code:

public class Measurements {
private double[] values;
private double[] newArray;
private int n; //numberofvalues
private double[] ms;
public Measurements(int max) { //constructor

[code]....

View Replies View Related

Declare And Creates Integer Array That Can Hold 15 Integers

Nov 3, 2014

The main Method

-Create a main method declares and creates an integer array called nums that can hold 15 integers.

-Use a for loop to fill that array with multiples of 3: 0, 3, 6, 9, etc.

-Then use similar for loop to print each value in the array on one line, with each value separated by a single space.

-Compile and run the program to see the result:0 3 6 9 12 15 18 21 24 27 30 33 36 39 42

As you write other methods, you'll also modify the main method to make calls to them. The printArray MethodWrite a method called printArray that accepts an integer array as a parameter. This method does not return a value, and must be declared as static so that the main method can call it. Instead of printing the array in the main method, move that loop into this method. Call the printArray method from the main method. Compile and run the program to verify it prints the sam result as before.Add a println statement so that after printing the array values on one line, it then moves to the following line.Finally, modify the loop in the printArray method so that, instead of using a traditional for loop, it instead uses a for-each loop. Compile and run the program again.

Part III: More Array Methods

The linearSearch Method In lecture we looked at a method that performed a binary search on a sorted array. A much simpler (though much less efficient) search is a linear search, that simply starts at the front of the array and looks at each element in turn until it finds it or reaches the end.Create a method called linearSearch that accepts an integer array and a single int value as parameters. The goal of the method is to find the second parameter (the target) in the array. The method should return a single int representing the index of the target value. This method should not print any output itself. In this method, use a traditional for loop to scan through the elements in the array. As soon as you find the target value, return the index of that value.

If you scan through the entire array without finding the target value, return a -1.Modify the main method to call the linearSearch method and print the results. Call it twice, searching for the value 18 (which it should find) and the value 10 (which it should not). Including the previous activity, the output of the main method should now look similar to this:0 3 6 9 12 15 18 21 24 27 30 33 36 39 42

The index of 18 is 6

The index of 10 is -1

The sumArray Method

The sumArray method should take an integer array as a parameter and return a single integer representing the sum of all values in that array.Use a for-each loop to access each value in the array and compute a running sum. After the loop, return the total.Call the method from the main method, producing the following augmented output:0 3 6 9 12 15 18 21 24 27 30 33 36 39 42

The index of 18 is 6

The index of 10 is -1

The sum of this array is 315

The addValue Method...The addValue method should accept an integer array and a single int as parameters. The purpose of the method is to add the second parameter to EACH value in the array. The addValue method does not return a value, but the elements inside the array will be modified. Call the addValue method from the main method, adding 100 to each element in the array. Then call the printArray method again to see the modified array values:0 3 6 9 12 15 18 21 24 27 30 33 36 39 42

The index of 18 is 6

The index of 10 is -1

The sum of this array is 315 100 103 106 109 112 115 118 121 124 127 130 133 136 139 142

Test a Different Array..Finally, duplicate the content of the main method to perform similar tests on another array. Instead of filling it with multiples of 3, fill it with multiples of 4. And instead of using an array size of 15, use an array size of 20.Modify the values search for to include one that is in the array and one that isn't.Rerun the main method and carefully check the results.If you haven't been doing it all along (which you should), make sure the appropriate class and method documentation is included.When you're satisfied that all methods are working correctly, modify the main method to delete the second array tests.

View Replies View Related

Creates Array Of 3 Consecutive Ints (0 - 7) User Has To Guess What Those Numbers Are

Aug 16, 2014

I have the following program. In a nutshell, I creates an array of 3 consecutive ints and the user has to guess what those numbers are, knowing that they are between 0 and 7, this is from Head First Java. In the book, the code has a bug that is made on purpose and they challenge you to fix it. As you can see bellow, every time a user's guess matches a int in the array, the NumOfHits is increased by one. When the NumOfHits is 3 (the length of the array) the game finishes.

The bug is that if you guess 1 and 1 is in the array, if you type in 1, 3 times, I will increase the NumOfHits 3 times and end the game as if you had won. To solve the bug, you need to find a way to tell the program that if the user already guessed that number, it should no longer be taken into account and we shouldn't increase the NumOfHits if the same number is provided.

I "fixed" this by searching for the index of the number that matches an int of the array, and changing that index's value to 100, since the user knows that the numbers are between 0 and 7, they will not submit 100.

The result? If the user submits 2 and 2 is in the array, the value of its indexed is replaced by 100, so that if the user submits 2 again it doesn't count as a match.

Please see the comments in the code

import java.io.*;
import java.util.Arrays;

class DotCom{
int NumOfHits = 0;
int[] LocationCells;
public void setLocationCells(int[] locs){
LocationCells = locs;

[Code] .....

The code works, but for some reason, some times I get a ArrayIndexOutOfBoundsException run time error and it appears to be somehow random. This is what I saw on the command line earlier

C:Userspablo.alvarez>java launchDotCom

Enter a number: 3
missed
Enter a number: 4
hit
Enter a number: 5
hit
Enter a number: 7
missed
Enter a number: 5
missed
Enter a number: 4
missed
Enter a number: 3
missed
Enter a number: 4
missed
Enter a number: 5
missed
Enter a number: 6

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1
at DotCom.checkYourSelf(launchDotCom.java:16)
at DotComGame.startGame(launchDotCom.java:59)
at launchDotCom.main(launchDotCom.java:72)

As you can see here, 3 returned 'missed' but 4 and 5 returned hit. Since the numbers are in sequence and are only 3, it makes sense that the last number is 6 (4,5,6). You will then notice that when I submitted 3 and 4 again it returned 'missed' as expected, because the numbers were already matched earlier, but then when I submitted 6, it returned the error seen above.

Sometimes this doesn't happen at all, sometimes it happens at the 2nd guess or third, it appears to be somehow random. I'm familiar with the error message, but I don't see where I'm using an index that doesn't exist in the array. The only time I'm referencing an array index is on

LocationCells[index] = 100;

but the index variable will only hold a value if the int submitted by the user matches on of the values in one of the indexes, so how is it that I'm going over the number of available indexes?

View Replies View Related

Rotate Left Bitwise Shift?

May 13, 2014

I'm trying to replicate the rol(rotate left) instruction in assembly though can only get as far as shifting the bits with '<<' or doing Long.rotateLeft(var, 5). Both of these method don't wrap around the bits as the rol instruction does.

View Replies View Related

Swing/AWT/SWT :: Moving Image Left Or Right

Mar 11, 2014

I can't find any resource on the net about a simple Java code just to move a gif image left or right. I've already accomplished the up, down, and center and they're working fine, thus, I'm still struggling with moving the image left or right. Here's the code.

public class MoveIt extends Applet implements ActionListener
{
private Image cup;
private Panel keypad;
public int top = 10;
public int left = 10;

[Code] ....

I remember in Visual BASIC it's easily achieved by NameOfImage.left = NameOfImage.left - 10 to move left and NameOfImage.left = NameOfImage.left + 10.

View Replies View Related

Drawing Shapes Bottom Right To Top Left

Mar 17, 2014

I am having issues with drawing shapes from bottom right to top left.

Issue:

- g.drawRect() will show like I am calling g.fillRect()
- other shapes will not even show the shape in that area

Needs:

- g.drawSHAPE needs to show and not be filled unless I have my fill checkbox selected

The Program:

- Create a JFrame with a draw panel and a component panel
- have a combobox with shapes that, when selected, will draw that shape in the draw panel
- have a button that, when clicked, will launch JColorChooser to change the color of the drawn shape (draw panel is set to black)
- have a checkbox that, when checked, fills the shape
- have mouse listeners to adjust X and Y and will instantly update the shapes size to where you drag/click/press/release

Code for my drawRect():

Java Code:

// if statement to check if mouse drag X is less than starting X
if(x2 <= x){
if(emptyORfill.isSelected()) // emptyORfill is my JCheckBox
g.fillRect(x2, y, x-x2, y2-y); // x-x2 is the same as Math.abs(x2-x)
else
g.drawRect(x2, y, x-x2, y2-y);

[Code] .....

This is just for my Rectangle. This will show a filled rectangle when both mouse drag X and Y are less then the starting X and Y. If I take this fully functional code and adapt it to drawRoundRect(), the round rectangle wont even show the shape when mouse drag X and Y are less than the starting X and Y but will be fine if one or the other is less than the starting X or Y. NOTE: This same exact code worked on my classmates laptop in her program, but in my program on her laptop it did not. She took out the "else" in the else if's and just made them if statements all the way down and it worked on her laptop in my program, but the same "fix" did not work on my pc.

My mouse listener just sets X and Y values in my Shape class that updates my shape methods. I have an item listener for my comboBox that sets default values when a new selection is made and enables/disables editable on my fill checkbox for certain shapes. My action listener looks for the button click and the checkBox click.

View Replies View Related

How To Identify Up Down Left And Right Keys With Keylistener

Apr 9, 2014

I Want to identify up, down, left and right keys with the keylistener() WHATE NAME DO I IDENTIFY IT WITH

View Replies View Related

Setting Left And Right Node Values

May 6, 2014

I am getting errors when I try setting left and right values (which are String types) for my tree.I tried doing something like:

Node node = new Node("Is it human?", null, null);
node.getLeft().setNode("Is it Zelda?");//the first left Q
node.getRight().setNode("Is it Kirby?");//the first right Q

but that gives me a runtime error of "java.lang.NullPointerException" which points to the line 2. I also tried this:

Node node = new Node("Is it human?", null, null);
node.setLeft(setNode("Is it Zelda?"));//the first left Q
node.setRight(setNode("Is it Kirby?"));//the first right Q

but that gives me another error, pointing to the setNode for both lines 2 and 3,plus it wouldnt make sense since both setLeft and setRight takes in Node types, not String types.

Here is my Node class:

public class Node {
private Node leftPt, rightPt;//left and right pointers for Node
private String node, left, right;
public Node(String node, Node leftPt, Node rightPt){
this.node = node;
this.leftPt = leftPt;
this.rightPt = rightPt;

[code]....

View Replies View Related

JGrasp - Printf Left Justify?

Sep 6, 2014

We're learning printf with JGrasp as the environment at the moment. Heres the problem.

I have a string as this:

String header1 = new String ("Caswell Catering and Convention Service");

as for the printf line, its this:

System.out.printf ("%-5s/n", header 1);

The output is right justified no matter the use of the "-" or without and I can't seem to find the correct thing to do to make it left justified.

View Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved