public class TableModel extends AbstractTableModel {
public int page;
public TableModel(Integer p) {
this.page=p;
System.out.println("mm"+page);
}
public void pudata() {
System.out.println(page);
}
//System.out.println("model "+page);
private String[] columnNames = {"groupName","membersCount","previliage"};
public ArrayList<GroupData> data = (new DatabaseLayer ()).getGroup(page);
#Override
public int getRowCount() {
return data.size() ;
}
Can not access variable page in getgroup() method it passes 0 to getgroup() method.
public ArrayList<GroupData> data = (new DatabaseLayer ()).getGroup(page);
Your question is unclear, but I suspect the problem is just that all the instance initializers are being run before the constructor body, so you're seeing the default value for page. You should have something like:
public class TableModel extends AbstractTableModel {
private static final String[] columnNames =
{"groupName","membersCount","previliage"}; // TODO: Fix spelling!
private final int page;
private final List<GroupData> data;
public TableModel(int page) {
this.page = page;
this.data = new DatabaseLayer().getGroup(page);
}
...
}
It's generally a good idea to keep all your instance/static variable declarations in one place (I prefer to keep them at the top, but YMMV) and make them all private to make it easier to reason about how they're used. The main change, however, is moving the new DatabaseLayer ().getGroup(page) code into the constructor.
public class TableModel extends AbstractTableModel {
public int page;
public ArrayList<GroupData> data;
public TableModel(Integer p) {
this.page=p;
this.data = (new DatabaseLayer ()).getGroup(page);
System.out.println("mm"+page);
}
public void pudata() {
System.out.println(page);
}
//System.out.println("model "+page);
private String[] columnNames = {"groupName","membersCount","previliage"};
#Override
public int getRowCount() {
return data.size() ;
}
Refresh your data field every time when you assign a new value to the page field.
public TableModel(int p) {
setPage(p);
}
public void setPage(int p) {
this.page = p;
this.data = new DatabaseLayer ().getGroup(page);
}
This is absolute correct because:
public int page;
default value for page is 0 because its int.
public ArrayList<GroupData> data = (new DatabaseLayer ()).getGroup(page);
Is a variable initialization so before initialization of page you are passing it into .getGroup(page) so default value will pass in that case.
So you have to call getGroup(int) method after page being initialized, one way can be following:
private final List<GroupData> data;
public TableModel(Integer p) {
this.page = p;
this.data = new DatabaseLayer().getGroup(page);
System.out.println("mm"+page);
}
Related
I'm trying to create some methods for my object but the outside of the constructor, the object isn't recognized by it's name
public class Playlist extends SongRecord {
final int maxSongs = 50;
public Playlist() {
SongRecord[] list = new SongRecord[maxSongs];
}
public int size(){
return list.length();
}
}
The error message says list isn't recognized
Because your list scope is in the constructor if u want to access list every where u need to move your list to class scope
public class Playlist extends SongRecord {
final int maxSongs = 50;
SongRecord[] list;
public Playlist() {
list = new SongRecord[maxSongs];
}
public int size(){
return list.length();
}
}
Just move list to class scope:
public class Playlist extends SongRecord {
final int maxSongs = 50;
private SongRecord[] list;
public Playlist() {
list = new SongRecord[maxSongs];
}
public int size(){
return list.length();
}
}
I have read quite a few pages of stackoverflow but I wasn't able to get my ArrayList to get copied unto another class. Here's the scenario, I'm building a quick book saver app, similar to what you would have in a library but simpler (for school).
I have my main library class (with the main) that has the swing set up for the main menu/options.
I have the book class with the constructor for new books as follows:
public class Livre {
private String titre;
private String soustitre;
private String auteur;
private String editeur;
private String collection;
private String isbn;
private long cup;
private double prixDeVenteSuggere;
private double prixVente;
private int nbPages;
private boolean disponible;
public Livre(String titre, String soustitre, String auteur, String editeur, String collection, String isbn, long cup, double prixDeVenteSuggere, double prixVente, int nbPages, boolean disponible){
this.titre = titre;
this.soustitre = soustitre;
this.auteur = auteur;
this.editeur = editeur;
this.collection = collection;
this.isbn = isbn;
this.cup = cup;
this.prixDeVenteSuggere = prixDeVenteSuggere;
this.prixVente = prixVente;
this.nbPages = nbPages;
disponible = true;
}
public Livre() {
}
public String getTitre() {
return titre;
}
public void setTitre(String titre) {
this.titre = titre;
}
public String getSoustitre() {
return soustitre;
}
public void setSoustitre(String soustitre) {
this.soustitre = soustitre;
}
public String getAuteur() {
return auteur;
}
public void setAuteur(String auteur) {
this.auteur = auteur;
}
public String getEditeur() {
return editeur;
}
public void setEditeur(String editeur) {
this.editeur = editeur;
}
public String getCollection() {
return collection;
}
public void setCollection(String collection) {
this.collection = collection;
}
public String getIsbn() {
return isbn;
}
public void setIsbn(String isbn) {
this.isbn = isbn;
}
public long getCup() {
return cup;
}
public void setCup(long cup) {
this.cup = cup;
}
public double getPrixDeVenteSuggere() {
return prixDeVenteSuggere;
}
public void setPrixDeVenteSuggere(double prixDeVenteSuggere) {
this.prixDeVenteSuggere = prixDeVenteSuggere;
}
public double getPrixVente() {
return prixVente;
}
public void setPrixVente(double prixVente) {
this.prixVente = prixVente;
}
public int getNbPages() {
return nbPages;
}
public void setNbPages(int nbPages) {
this.nbPages = nbPages;
}
public boolean isDisponible() {
return disponible;
}
public void setDisponible(boolean disponible) {
this.disponible = disponible;
}
}
Option #1 on the Library class (built with WindowBuilder) has the "New" button which opens a second JFrame to input all the info in regards to the book.
in this JFrame class, I've added an actionListener on the confirm button to confirm the input on the JTextFields to be added as an object as follows:
public void confirmerLivre(){
l = new Livre(txtTitre.getText(), txtSousTitre.getText(), txtAuteur.getText(),
txtEditeur.getText(), txtCollection.getText(), txtISBN.getText(),
Long.parseLong(txtCodebar.getText()), Double.parseDouble(txtPrixMSRP.getText()),
Double.parseDouble(txtPrix.getText()), Integer.parseInt(txtPages.getText()), true);
confirmerLivre.add(l); /// confirmerLivre is defined as an ArrayList
}
What I can't wrap my head around is being able to take the ArrayList confirmerLivre from the 2nd JFrame class and push it unto my main JFrame class to be manipulated further with other options.
Any help will be greatly appreciated.
Thank you
Probably the quickest fix is to create/expose these methods in your main JFrame class:
getBookList()
setBookList()
When you create your popup JFrame, you need to pass an instance of your main JFrame class to it in its constructor:
public PopupFrame extends JFrame {
private MainFrame main;
public PopupFrame(MainFrame main) {
this.main = main;
}
}
Now that you have access to your main JFrame class from your popup, you can just go main.getBookList() to get the list (I'd recommend reading this question also)
If you create your ArrayList as a public variable in the second JFrame class (outside any of the methods) then it can be used in the first class such as:
SecondJFramesName.confirmerLivre()
In this code SecondJFramesName is the name of your second JFrame class. Now that your ArrayList is a public variable it can be accessed outside the class.
Note: your second JFrame's name is the one you use to create it in a way such as this:
JFrame SecondJFramesName = new JFrame("My title");
If you need any more specific details please comment!
Hopefully this helps!
Maybe observer pattern could help you:
public interface ConfirmerLivreMonitor{
void onConfirmerLivreChange(List<...> confirmerLivre);
}
then
//...
private ConfirmerLivreMonitor confirmerLivreMonitor;
public void setConfirmerLivreMonitor(ConfirmerLivreMonitor confirmerLivreMonitor ){
this.confirmerLivreMonitor = confirmerLivreMonitor
}
//....
public void confirmerLivre(){
l = new Livre(txtTitre.getText(), txtSousTitre.getText(), txtAuteur.getText(),
txtEditeur.getText(), txtCollection.getText(), txtISBN.getText(),
Long.parseLong(txtCodebar.getText()), Double.parseDouble(txtPrixMSRP.getText()),
Double.parseDouble(txtPrix.getText()), Integer.parseInt(txtPages.getText()), true);
confirmerLivre.add(l); /// confirmerLivre is defined as an ArrayList
if(confirmerLivreMonitor != null){ //notify confirmerLivre change
confirmerLivreMonitor.onConfirmerLivreChange(confirmerLivre);
}
}
make the Main JFrame implemnents ConfirmerLivreMonitor,so you can:
sencondJFrame.setConfirmerLivreMonitor(this);
or just pass a anonymous class:
sencondJFrame.setConfirmerLivreMonitor(new ConfirmerLivreMonitor(){
public void onConfirmerLivreChange(List<...> confirmerLivre){
//display in Main JFrame,maybe
}
});
once the confirmerLivre change, the main frame can display(or something else)
the first time,very cool
I trying to access a device using JNA
The first command return a statusCode 263 insteadof zero.
I searched and found this code, means "INVALID_DEVICE_NAME". The device's manual say:
The MCI_OPEN_TYPE flag must be specified and the lpstrDeviceType member of the structure identified by lpOpen must contain the device
name (in our case “DictCtrl”).
Is there any error in my code?
The "same" code works in a compiled delphi application, my device is recognized.
public class Main {
public static final int MCI_OPEN = 0x0803;
public static final int MCI_OPEN_TYPE = 0x00002000;
public static final int MCI_STATE_STOP = 0x00000001;
public static final int MCI_OPEN_SHAREABLE = 0x00000100;
public static Winmm winmmLib = Winmm.INSTANCE;
public interface Winmm extends StdCallLibrary {
Winmm INSTANCE = (Winmm) Native.loadLibrary("winmm", Winmm.class);
int mciSendCommandA(int IDDevice, int uMsg, int fdwCommand, Structure dwParam);
}
public static class TagmciOpenParmsa extends Structure {
public int dwCallback;
public int wDeviceID;
public String lpstrDeviceType;
public String lpstrElementName;
public String lpstrAlias;
#Override
protected List getFieldOrder() {
return Arrays.asList(new String[] { "dwCallback", "wDeviceID", "lpstrDeviceType", "lpstrElementName", "lpstrAlias" });
}
}
public static void main(String[] args) {
TagmciOpenParmsa tagmciOpenParmsa = new TagmciOpenParmsa();
tagmciOpenParmsa.lpstrDeviceType = "DictCtrl";
int ret = winmmLib.mciSendCommandA(0, MCI_OPEN, MCI_OPEN_TYPE, tagmciOpenParmsa); // | MixerConstants.MCI_OPEN_SHAREABLE
System.out.println(ret);
}
}
Consider this:
public class Model{
private Map<Vector, Vector> vertices;
public Model(Vector v){
vertices.put(v, v);
}
}
I was expecting an NPE since vertices is uninitialized; at least I was expecting an error as Map is abstract and I'm working with an object.
Can somebody shed a light here?
EDIT:
public class World{
public static void init(){
Model cube = new Model(someVector);
}
}
I have a Main class containing main(); in main() I'm calling World.init();
The code is simplified for readability.
EDIT 1:
public class Model extends Positionable{
public static Map<String, Model> map = new HashMap<>();
private Map<Vector3f, Vector3f> vertsAndNormals;
private Set<Face> faces;
public Model(String name_, Map<Vector3f, Vector3f> vertsAndNormals_){
super(); // \!/ passing `this`; may not have been entirely initialized
vertsAndNormals = new HashMap<>(vertsAndNormals_);
map.put(name_, this);
}
public Model(String name_, Set<Vector3f> vertices_){
super(); // \!/ passing `this`; may not have been entirely initialized
for(Vector3f vertex : vertices_)
vertsAndNormals.put(vertex, new Vector3f(0, 0, 0)); // \!/ why does this NOT cause an NPE?
map.put(name_, this);
}
public Model(String name_){
this(name_, new HashMap<Vector3f, Vector3f>());
}
}
and where I call:
public class World{
public static Set<Model> modelsInWorld = new HashSet<>();
public static void init(){
Model cube = new Model("gugu");
}
}
In main():
World.init();
EDIT 2:
public abstract class Positionable{
public static Set<Positionable> set = new HashSet<>();
public float x = 0;
public float y = 0;
public float z = 0;
public float xRol = 0;
public float yPit = 0;
public float zYaw = 0;
public Positionable(){
set.add(this);
}
}
You're not actually calling the constructor that has the line you're concerned about. You call the constructor that takes a String, which forwards to the constructor that takes a String and a Map. The constructor that takes a Set will cause an NPE at runtime.
consider the below code:
public class Bid {
private double pe;
private List<ResChar> resourceList;
protected Map<Integer,Integer>scheduleOfSeller ;
public Map<Integer, Integer> getScheduleOfSeller() {
return scheduleOfSeller;
}
public void setScheduleOfSeller(Map<Integer, Integer> scheduleOfSeller) {
this.scheduleOfSeller = scheduleOfSeller;
}
private int bidId;
public int getBidId() {
return bidId;
}
public void setBidId(int bidId) {
this.bidId = bidId;
}
public double getPe() {
return pe;
}
public void setPe(double pe) {
this.pe = pe;
}
public List<ResChar> getResourceList() {
return resourceList;
}
public void setResourceList(List<ResChar> resourceList) {
this.resourceList = resourceList;
}
public Bid(int bidId,double pe, List<ResChar> resourceList){
setBidId(bidId);
setPe(pe);
setResourceList(resourceList);
this.scheduleOfSeller = new HashMap<Integer,Integer>();
}
}
I want to make a copy constructor of the bid like this :
public class BidCopy{
public Bid bid;
public BidCopy(Bid bidBuyer){
List<ResChar> resList = new LinkedList<ResChar>();
for (ResChar elt : bidBuyer.getResourceList()){
ResCharCopy eltCopy = new ResCharCopy(elt);
resList.add(eltCopy.elt);
}
this.bid = bidBuyer;
this.bid.setResourceList(resList);
}
}
The only solution that I know to make such copy is to proceed like follows :
public class BidCopy{
public Bid copy;
public BidCopy(Bid bid){
List<ResChar> resList = new LinkedList<ResChar>();
for (ResChar elt : bid.getResourceList()){
ResCharCopy eltCopy = new ResCharCopy(elt);
resList.add(eltCopy.elt);
}
this.copy = new Bid(bid.getBidId(), bid.getPe(), resList);
}
}
So I want to know if there is any other solution to make a copy of "Bid" Object more effectively ?
I would suggest making a copy constructor for your Bid object (and not a specific class for copying), a Bid is made out of its fields and not methods, like so:
public class Bid {
int ID;
String description;
Object bidStuff;
// ...as before
public Bid(Bid bid) {
this.ID = bid.ID;
this.description = bid.description;
this.bidStuff = bid.bidStuff;
}
public static void main(String[] args) {
List<Bid> original = new ArrayList<>();
// ..populate it
List<Bid> copy = new ArrayList<>(original.size());
for (Bid b : original) {
copy.add(new Bid(b));
}
}
}
You can even make the copy constructor protected or package-protected if you don't want anyone else to mess around with making multiple copies of bids.
There is not. Even though some collections have "copy constructors", these constructors will copy the elements' references, they will not create new elements for you.
You can however "optimize" the list creation itself by submitting the size of the initial list to the constructor:
List<X> newList = new LinkedList<X>(oldList.size());