Swing/AWT/SWT :: Java GUI With Separate Class For JList - Adding Items To The List?

Sep 19, 2014

I am trying to create a GUI interface in swing which consists of four classes: a GUI class, which creates a main JPanel, a label, and a JList, which it takes from the second class, a MovesList class that contains a JList and the stuff needed to interface with it. It also has a main class, which basically just creates an object of the GUI class for the main window, and an Other class from which I would like to be able to add an item to the JList. I created methods in the MovesList class to get each component (like getMoveslist, or getMovesListScrollPane), which I then used to create the JList in the GUI class. I also created an addMove method so that I can add an item to the JList from any class through a MovesList object. However, this addMove method only works when called from the GUI or MovesList classes -- it does nothing when I collect from the Other class.

Here is my code:

//Main class
public class TestProject {
public static void main(String[] args) {
GUI mainWindow = new GUI();
mainWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

[Code] ....

When I run this code, I get a window with a JLabel that says "Moves," and a JList that contains five elements -- test 1-test 5, but not a sixth "test from other class." ( using the add move method ) However, when I click on the window, the addMove method is also called, and successfully adds "test 6" to the list.

View Replies


ADVERTISEMENT

Adding Array Of Items To Linked List

Sep 21, 2014

I'm having trouble completing my homework. The problem:

>Suppose that you want an operation for the ADT list that adds an array of items to the end of the list. The header of the method could be as follows:
public void addAll(T[] items)

>Write an implementation of this method for the class LinearLinkedList

At first this seemed like an easy problem to me. This is the solution I came up with:

public void addAll(T[] items) {
int length = items.length;
for(int i = 0 ; i < length; i++){
this.addLast(items[index]);
}
}

However, one of the requirements for the assignment is that we're not allowed to use the addLast method. how else to insert items to the Linked List without this method. These are the user-defined LinearLinkedList and ListNode classes we made during class:

**LinearLinkedList**
// Linear linked list class
import java.util.NoSuchElementException;
public class LinearLinkedList {

[code]....

View Replies View Related

Swing/AWT/SWT :: Adding Images To JLIST

Apr 14, 2015

I am currently creating a Twitter Application within Java Swing using JSON. I have the JList populating with Home feeds but i also want to get Images displaying next to the Text ...

currently all i am getting is [URL] ....

So a am able to get the URL Link but getting that to a Image seems to be the problem

/ArrayLists
final ArrayList<String> TweetArray = new ArrayList<String>(); //Array List for Users Tweets..
final static ArrayList<String> incomingTweets = new ArrayList<String>(); //ArrayList for Incoming Tweets.
ArrayList<String> arrayImages = new ArrayList<String>();//ArrayList for incoming Tweet Profile Images
private Map<String, ImageIcon> imageMap;

[Code] ....

View Replies View Related

Swing/AWT/SWT :: Create A Sub Menu For Every List Item In A Jlist

Oct 8, 2014

I would like to create a sub menu for every list item in a Jlist. I need the UI like avast interface. If we hover over an list item, its sub menu should be shown. I attempted to put an sub menu but didn't work. Is this possible in Swing?

View Replies View Related

Swing/AWT/SWT :: Adding Items To JComboBox Taken From JTextField Listener

Nov 11, 2014

I need to take the value of what's typed in my JTextField, and once the submit button is clicked, I want to add that string to my JComboBox list items.

Depending which RadioButton they selected, I need to put the team in HOME or AWAY teams, and so on and so on, until they continue clicking submit.

View Replies View Related

Adding Elements To Existing JList Under Java 7

Mar 14, 2014

I know there was a change in the later versions of Java where the C++ equivalent of a <template type> was added. Unfortunately the change has 'broken' my older code. If I have a JList and I want to add elements to it then now I should specify the type e.g., the list will store Strings. When I do this and then add data to the list (or actually the list model) the code is ''fixed". However if after adding those new elements to the list I later need to add more elements, which isn't unreasonable for a list...to have elements added dynamically at run time then I again get the same compiler error message that I haven't correctly specified the type:
 
// Error message during compilation
Note: Driver.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
 
// My code
 
// Problem here: I try to add new elements to the list. It's the very last line of the method that results in the error. I tried various things such as:

// model.addElement(<String> s);
// but   so far nothing has worked. How do I add new elements to the list (model)?   
public static void m2(JList <String> list)
    {
    String s;
    int i;
    String [] array = new String[10];

[Code] ....
 
// Code is OK: Create the array of strings to add to constructor of the JList

public static String[]  m1(){    String s;
    int i;
    String [] array = new String[10];
    for (i = 0; i < 5; i++)
    array[i] = i + "*";    return(array);

[Code] ....

// The change I had to make when compiling under the newer version of Java to indicate that the list would store strings
 
   // Things are okay here now but then when I try to add new elements to the model via method 'm2' that's where I get the compiler error

View Replies View Related

Adding Data File Into Two Separate 2D Arrays In Java

Apr 27, 2014

I am having some trouble with a Java program. I have a txt data file, which I will display, that I need to add into two separate arrays. The text file is 8 lines long that is supposed to go into two separate 4x4 matrices. A little background info on this program, reads in two arrays, compares them and outputs the largest elements of the same index and outputs them in a separate array. I somehow cannot seem to figure out how to do this. My code is below:

Data File:

2 7 6 4
6 1 2 4
9 7 2 6
8 3 2 1
4 1 3 7
6 2 3 8
7 2 2 4
4 2 3 1

Code:

public class prog465a {
public static void main(String[] args) {
Scanner inFile = null;
try {
inFile = new Scanner(new File("prog465a.dat.txt"));

[Code] .....

View Replies View Related

Swing/AWT/SWT :: Endless Loop While Calling Dialog Box Via Separate JFrame Class In NetBeans IDE

May 2, 2014

I've used Netbeans IDE to create a simple Swing JFrame container, class NewJFrame.

NewJFrame calls another JFrame with dialog in it. Problem is there is an endless call made to it. Here's the issue relevant piece of code:

//Code for calling class
package p;
public class NewJFrame extends javax.swing.JFrame implements ActionListener {
.........
public NewJFrame() {
initComponents(); //IDE auto generated code for binding

[Code] ....

I searched but i cannot understand why run class is getting called multiple times. I have attached the screen prints for both the screens. 2nd screen print(of called class) is very shallow since screen was blinking unstopped due to multiple calls, but i have attached a faint image of it.

View Replies View Related

Adding Values From Jtextbox To JList

Aug 9, 2014

how to add values from Jtextbox(name) to JList and display the selected values from Jlist.

import java.awt.*;
import java.awt.event.*;
import java.awt.Color;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.border.*;
import javax.swing.DefaultListModel;

[Code] ....

View Replies View Related

Adding JList Object To Load With EnumMap

Sep 20, 2014

I have a program I am creating in Eclipse. In my JFrame window, I am adding a JList object that I want to load with an EnumMap that I have created in a separate 'enum' class.

In my JFrame, I have a method called 'loadJList()' in which I want to iterate through the EnumMap and load the keys & values.

Here's what I've tried

In my EnumMap class, I've done the following ...

Java Code:

public enum MyEnumMap {
.....//created map values & keys, getter() and setter()
public Enum<MyEnumMap> getEnum() {
return this;
}
} mh_sh_highlight_all('java');

I imported my EnumMap program in my JFrame program and tried something like ...

Java Code:

public void loadJList() {
for(Enum<?> e: myEnumMap.values()) {
list.add ....
}
} mh_sh_highlight_all('java');

Which does not work. I've scoured the internet a bit but sources on EnumMap are limited, at least ones I can understand (first time trying to use this).

I also need to elaborate on the key of the EnumMap (they are cities, I'd like to add the state or country). For example

LASVEGAS, 749.99

I would want displayed as

Las Vegas, Nevada .... 749.99

My ultimate goal here is to access my EnumMap in my JFrame program to dynamically load a JList object.

View Replies View Related

Adding Items To The Game

Jan 4, 2015

I made my items class and I am storing my item as a String array to list them all and what I want to be able to do is type pickup, have the game read my location and display the items that are avaible to pickup at current location then type the item and store it in my playerInventory. Then, have it check the slot if it is doesn't equal null go to next.

Use a boolean value of 0 = false and 1 = true. then, have it check if the inv array = 1 or 0. if 1 go to next inv. If all are full then, ask player if they would like to replace an item. Use a 2d array for storage for slots and true or false value.

Here is my code

Player.java
package com.PenguinGaming;
import java.util.Random;
import java.util.Scanner;
public class Player {
public void User(){
Commands commands = new Commands();
Map map = new Map();

[Code] .....

View Replies View Related

ArrayList Only Adding 1 Item When There Are Multiple Items

Oct 25, 2014

I managed to retrieve data, and set data in my own ways in which I like. But my problem is, if the file does not contain anything (fully empty), when I try to use my

set("", "");
method, it only sets the last one that is called.

Example:

set("section1", "section1Item");
set("section2", "section2Item");
set("section3", "section3Item");

Only section3 would get set, and not the others.

Otherwise, if the file contained a section already, then each section (section1, section2, section3) would get set.

Here's how I set the data to the file:

public static void set(String section, String data) {
files.openFileWriter();
files.file.delete();
reCreateFile();
String beforeItem = section + ":" + files.dataList.get(section);
if(files.hasReadData) {

[Code] ....

And here is how a retrieve the data and set them to my arraylist/hashmap:

public void getData() {
String line = null;
openFileReader();
StringBuffer sb = new StringBuffer();

[Code] ....

View Replies View Related

Text Based Game Adding Items

Apr 11, 2014

I am trying to make a text based game. the game has been working perfectly setting up the rooms, first couple of commands, and running it. I am now trying to add items to it but every time it try to run the game it returns :

java.lang.NullPointerException
at Room.addItem(Room.java:107)
at Game.createRooms(Game.java:133)
at Game.<init>(Game.java:28)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)

[Code] .....

Here are the classes that matter for this particular situation

import java.util.HashMap;
public class Item
{
private HashMap<String, Item> itemList;
private String name;
private String itemDescription;

[Code] ....

I know that it is the line

itemList.put(item.getItemName(), new Item(item.getItemName(), item.getItemDescription()));

In the game class that is causing the nullpointer exception i just really cant figure out why that keeps happening and how to add the values correctly....

View Replies View Related

Java 8u40 Application Blocked Even After Adding To Exception List

Mar 30, 2015

Have a user who cannot access a file on website for work he needs to do. He gets these errors:

His Java is the latest and I have already added the site to the exceptions list in, Java and the browser, but I can't get through.

View Replies View Related

Swing/AWT/SWT :: Adding Arraylist To Jtable In Another Class

Jan 23, 2015

I'm trying to fill my jtable with an arraylist. The problem is the jtable is in an extended class and the arraylist in the mainGUI. Now how can I fill the jtable with the arraylist?

That's the arraylist in my MainGUI

BufferedReader in = null;
ArrayList<String> data = new ArrayList<String>();
try {
in = new BufferedReader(new FileReader("1.dat.txt"));
String str;
while ((str = in.readLine()) != null) {
data.add(str);

[Code] ....

View Replies View Related

How Can Items Be Added To List

Feb 13, 2014

I have the following code. how can items be added to that list?

private List<IComponentNode> comp;
interface IAAAView {
  // returns component with given code or null
  IComponentNode findComp(String a);
  // returns component with given renderer or null

[Code] .....

View Replies View Related

ArrayList Only Adding 1 Item Even Though There Are Multiple Items (Setting Data To Files)

Oct 25, 2014

I managed to retrieve data, and set data in my own ways in which I like. But my problem is, if the file does not contain anything (fully empty), when I try to use my

set("", "");

method, it only sets the last one that is called.

Example:

set("section1", "section1Item");
set("section2", "section2Item");
set("section3", "section3Item");

Only section3 would get set, and not the others.

Otherwise, if the file contained a section already, then each section (section1, section2, section3) would get set.

Here's how I set the data to the file:

public static void set(String section, String data) {
files.openFileWriter();
files.file.delete();
reCreateFile();
String beforeItem = section + ":" + files.dataList.get(section);

[code]....

And here is how a retrieve the data and set them to my arraylist/hashmap:

public void getData() {
String line = null;
openFileReader();
StringBuffer sb = new StringBuffer();

[code]....

View Replies View Related

Opening New Forms Related To Items In List

Oct 3, 2014

I want to create a program which contains a list with 5 items and a button on the first display. The desired option is chosen from from the list and the button is pressed. On pressing the button it must open a new form which corresponds to the chosen item from the list.

View Replies View Related

Swing/AWT/SWT :: Using JList Instead Of JTable?

Apr 18, 2014

Can I use Jlist instead of Jtable for showing database table data (select * from employee) in my code?

View Replies View Related

List Interface Class That Has Operations For Linked List And LList Class

Oct 6, 2014

I have this ListInterface class that has operations for my linked list and a LList class. The Llist and ListInterface classes are perfect. My job is to create a driver, or a demo class that showcases these operations. That being said, heres the driver so far:

import java.util.*;
public abstract class DriverWilson implements ListInterface
{
public static void main(String[] args)
{

LList a = new LList();

[code]....

View Replies View Related

Swing/AWT/SWT :: Changing Color Of JList Item

Oct 23, 2014

is there a good way to change the color of a JList item that isn't selected? I'm throwing my hand at building a client/server chat app and on the client side, I need to highlight the name of the person (which is in a JList) who has sent a message to the client GUI.

View Replies View Related

Swing/AWT/SWT :: Alternative To Making Static JList?

Sep 16, 2014

I am currently writing a chess application in Java swing. For the GUI part, I have one MainWindow class, and one ChessBoard class. The MainWindow has a main panel, in which is contained 1) a panel containing the chessboard from the chessboard class (a grid layout array of 64 JLabels), 2) a JLabel status bar, and 3) a JList to hold the moves of the game. I have a movePiece method in the chessboard class, which I would like to add the move and question to the moves list, which is contained in the main window class.

However, since JList is some special type, I can't make it static ( that gives me an error ). I also tried creating a method in the main window class to add a move to the list, which I would then access via the chessboard class. However, I can't make the method static, because it is referencing a non-static variable ( the JList ), and since the Main window class already has an object of the chessboard class, trying to create a main window object in the chessboard class creates a stack overflow error.

View Replies View Related

Java Class Person - Adding Data To Text

May 5, 2014

Your Tester class main method must use an array or an ArrayList of Person objects. It must be populated from a text data file, friends_data.txt. Add the following data to your text file,

Michelle, 12/20/2008, Camilla
Bryan, 3/8/2007, Tom
Camilla, 6/7/2005, Michelle
Tom, 10/15/2007, Bryan
Charlotte, 3/2/2008, Michelle

Each line has the meaning:

-Person name, Person date of birth (MM/DD/YYYY), name of best friend
-Write a Java program that reads in the data from the friends_data.txt file, allocates a new
-Person for each and then stores the newly created object in either an Array or an ArrayList.
-Next print out a report showing the following information for each Person,

1. The Person's name
2. Their popularity counter
3. Their age on May 1, 2014
4. The name of their best friend
5. The age of their best friend on May 1, 2014

Finally, print the name of the most popular Person and the name of the oldest Person.

Person Class

import java.util.ArrayList;
public class Person {
public String personsName;
public String personsFriend;
public String personsBirthday;
public int personsPopularity;
public int popularity = 0;

[code]...

I keep getting this error from the compiler:

System.out.println("Popularity : " + personsOfInterest[i].getPopularityNumber());

"method getPopularityNumber in class Person cannot be applied to given type:
Required: java.lang.String[]; found: no arguments; reason: actual and formal argument lists differ in length.

View Replies View Related

Swing/AWT/SWT :: How To Fill Text Fields With Selection From JList

May 2, 2015

I have an addressBook GUI where I have a JList that populates with the contacts names, and once I double click a contacts name I'm wanting to fill the textfields with the contacts corresponding data.

ex) ContactType: Family (enum), Name: Zoidberg, Address: 111 Space Drive, City: New York City, etc...

I've got it to where I select open from the JMenu, it populates the JList, but once I select a contacts name, all the textfields are populated, but only with the contacts name

ex) Name: Zoidberg, Address: Zoidberg, City: Zoidberg, etc....

What am i doing wrong here, and how can i fix it to where it fills out the correct data?

Here's my code so far:

public class AddressBookGUI extends JFrame {
private final int WIDTH = 450;
private final int HEIGHT = 300;
private JLabel currentlySelected;
private JTextField contactTypeTextField;

[code]...

I'm certain the logic is messed up near the end where i set all text fields to the index, because no matter what field i want to fill it's at it's going to set it to whatever index I select, but I don't know how to fix it.

View Replies View Related

Create Instance Of Class Used To Populate DefaultListModel When JList Item Selected

Jan 28, 2015

New to Java Swing. What I am trying to achieve here is to create an instance of the class used to populate the DefaultListModel when a JList item is selected. All examples I have seen show how to return the text displayed in the JList which really is of no practical use.

I have achieved what I want to achieve in that I am successfuly creating the selected object within my ListCellRenderer class.

For the purpose of testing I am using a call to the JOptionPane.showMessageDialog method to display the id of the selected object.

So, I launch the JFrame, the JList is populated, I select an item from the JList and the id value is displayed in the prompt. All this works fine except for one thing.

When I click OK on the JOptionPane.showMessageDialog prompt, the prompt disappears and reappears. The system seems to be in a loop. It is behaving as if the change or click event handler on the JList keeps firing.

This happens even without the creation of the object:

BookEntry be = (BookEntry)list.getModel().getElementAt(list.getSelectedIndex());

I have scoured the code and object properties to see if I can figure out where this behaviour is coming from without success.

All there is in the JFrame design view is a single JList with a variable name of "list". Everything else is in code as below:

Here is the .java code. Its not that complex

import java.awt.Color;
import java.awt.Component;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.ListCellRenderer;
import javax.swing.DefaultListModel;
 public class NewJFrame3 extends javax.swing.JFrame {
 
[Code] .....

View Replies View Related

Declaration And Initialization On Separate Statement In A Class

Jan 29, 2015

I know that I can declare and initialize an member variable inside a class in a single statement but I can't do them on separate sentence. Why?

For example, I am allowed to do the following,

class A{
int a = 5;
// rest of the class
}

But I am not allowed to do this,
class A{
int a;
a = 5; // it doesn't compile even when the variable 'a' is static
// rest of the class
}

Why java don't allow me to do that?

View Replies View Related







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