I'm trying to create Button objects in a dynamic way. I'm not really sure if this is possible. The following code has been used before (and failed):
public class GraphManager{
private Vector<Button> machineButtons = null;
public Vector<Button> getMachineButtons(){
return machineButtons;
}
public void setMachineButtons(int machines, Context context){
for(int i = 0; i < machines; i++){
Button machineButton = new Button(context);
machineButtons.add(machineButton);
}
}
public Button getMachineButton(int index){
return machineButtons.elementAt(index);
}
}
Are there any solutions for my problem?
Related
I'm coding a chess engine using bitboards and I wanna make an extensible API for the bitboards initializer, that would allow me to add more variants like chess960 in future.
So i came up with the following abstract superclass, which gives an uniform interface for all kind of initializers: it allocates the arrays used to store the bitboards and then calls the abstract method init() that has to be implemented by any subclass, inside there the bitboards should be created and assigned to the respective array
public abstract class BitboardInitializer {
private static final int NUMBER_OF_PLAYERS = 2;
protected long[] pawnsPositions, knightsPositions, bishopsPositions,
rooksPositions, queenPositions, kingPositions;
protected BitboardInitializer() {
pawnsPositions = knightsPositions = bishopsPositions =
rooksPositions = queenPositions = kingPositions = new long[NUMBER_OF_PLAYERS];
init();
}
protected abstract void init();
public long getPawnsPositionsAs(int side) {
return pawnsPositions[side];
}
public long getKnightsPositionsAs(int side) {
return knightsPositions[side];
}
public long getBishopsPositionsAs(int side) {
return bishopsPositions[side];
}
public long getRooksPositionsAs(int side) {
return rooksPositions[side];
}
public long getQueenPositionsAs(int side) {
return queenPositions[side];
}
public long getKingPositionsAs(int side) {
return kingPositions[side];
}
}
An implementation to initialize standard chess bitboards, it simply assign the hard-coded bitboards values because standard chess starts always in one way. Side.White and Side.Black are two static final fields used as array indexes to avoid inconsistence. white = 0, black = 1:
public final class StandardChessInitializer extends BitboardInitializer {
public StandardChessInitializer() {
super();
}
protected void init() {
pawnsPositions[Side.WHITE] =
0b00000000_00000000_00000000_00000000_00000000_00000000_11111111_00000000L;
pawnsPositions[Side.BLACK] =
0b00000000_11111111_00000000_00000000_00000000_00000000_00000000_00000000L;
knightsPositions[Side.WHITE] =
0b00000000_00000000_00000000_00000000_00000000_00000000_00000000_01000010L;
knightsPositions[Side.BLACK] =
0b01000010_00000000_00000000_00000000_00000000_00000000_00000000_00000000L;
bishopsPositions[Side.WHITE] =
0b00000000_00000000_00000000_00000000_00000000_00000000_00000000_00100100L;
bishopsPositions[Side.BLACK] =
0b00100100_00000000_00000000_00000000_00000000_00000000_00000000_00000000L;
rooksPositions[Side.WHITE] =
0b00000000_00000000_00000000_00000000_00000000_00000000_00000000_10000001L;
rooksPositions[Side.BLACK] =
0b10000001_00000000_00000000_00000000_00000000_00000000_00000000_00000000L;
queenPositions[Side.WHITE] =
0b00000000_00000000_00000000_00000000_00000000_00000000_00000000_00010000L;
queenPositions[Side.BLACK] =
0b00010000_00000000_00000000_00000000_00000000_00000000_00000000_00000000L;
kingPositions[Side.WHITE] =
0b00000000_00000000_00000000_00000000_00000000_00000000_00000000_00001000L;
kingPositions[Side.BLACK] =
0b00001000_00000000_00000000_00000000_00000000_00000000_00000000_00000000L;
}
}
The problem is that by calling any superclass' getter method I get binary 1000 for white (index 0) and 100000000000000000000000000000000000000000000000000000000000 for black (index 1) instead of the assigned values
An explanation of this strange behaviour would be highly appreciated, thanks in advance.
The problem is in the BitboardInitializer constructor, where you are initializing all of your array references to point to the same array:
protected BitboardInitializer() {
pawnsPositions = knightsPositions = bishopsPositions =
rooksPositions = queenPositions = kingPositions = new long[NUMBER_OF_PLAYERS];
init();
}
Should be:
protected BitboardInitializer() {
pawnsPositions = new long[NUMBER_OF_PLAYERS];
knightsPositions = new long[NUMBER_OF_PLAYERS];
bishopsPositions = new long[NUMBER_OF_PLAYERS];
rooksPositions = new long[NUMBER_OF_PLAYERS];
queenPositions = new long[NUMBER_OF_PLAYERS];
kingPositions = new long[NUMBER_OF_PLAYERS];
init();
}
I'm trying to build a simple java application using Spring Boot and Vaadin.
I need to add a table on UI like this: https://www.screencast.com/t/1c4xkr4IE
It could be extended by periods.
Looks like Vaadin Grid element perfectly fits my requirements, but it adds my rows as columns. Is it possible to reverse grid or maybe there is another way to build needed table?
UPDATE
Here are my code:
#SpringComponent
#UIScope
public class MyDataEditor extends VerticalLayout {
private final MyDataRepository repository;
private MyData myData;
TextField month = new TextField("Period");
TextField numberOfWorkers = new TextField(" Number of workers");
TextField numberOfNewcomers = new TextField("Number of newcomers");
TextField numberOfDismissals = new TextField("Number of dismissals");
Button save = new Button("Save");
Button cancel = new Button("Cancel");
Button delete = new Button("Delete");
CssLayout actions = new CssLayout(save, cancel, delete);
Binder<MyData> binder = new Binder<>(MyData.class);
#Autowired
public MyDataEditor(MyDataRepository repository) {
this.repository = repository;
addComponents(month, numberOfWorkers, numberOfNewcomers, numberOfDismissals, actions);
binder.bindInstanceFields(this);
setSpacing(true);
actions.setStyleName(ValoTheme.LAYOUT_COMPONENT_GROUP);
save.setStyleName(ValoTheme.BUTTON_PRIMARY);
save.setClickShortcut(ShortcutAction.KeyCode.ENTER);
save.addClickListener(e -> repository.save(myData));
delete.addClickListener(e -> repository.delete(myData));
cancel.addClickListener(e -> editInputData(myData));
setVisible(false);
}
public interface ChangeHandler {
void onChange();
}
public final void editMyData(MyData c) {
if (c == null) {
setVisible(false);
return;
}
final boolean persisted = c.getMonth() != null;
if (persisted) {
myData = repository.findOne(c.getMonth());
} else {
myData = c;
}
cancel.setVisible(persisted);
binder.setBean(myData);
save.focus();
periodId.selectAll();
}
public void setChangeHandler(ChangeHandler h) {
save.addClickListener(e -> h.onChange());
delete.addClickListener(e -> h.onChange());
}
}
#SpringUI
#Theme("valo")
public class VaadinUI extends UI {
private final MyDataRepository repo;
private final MyDataEditor editor;
final Grid<MyData> grid;
private final Button addNewBtn;
#Autowired
public VaadinUI(MyDataRepository repo, MyDataEditor editor) {
this.repo = repo;
this.editor = editor;
this.grid = new Grid<>(MyData.class);
this.addNewBtn = new Button("Add new month");
}
#Override
protected void init(VaadinRequest request) {
grid.setHeight(300, Unit.PIXELS);
grid.setColumns("month", "numberOfWorkers", "numberOfNewcomers", "numberOfDismissals");
grid.asSingleSelect().addValueChangeListener(e -> {
editor.editMyData(e.getValue());
});
addNewBtn.addClickListener(e -> editor.editMyData(new MyData()));
editor.setChangeHandler(() -> {
editor.setVisible(false);
grid.setItems(repo.findAll());
});
}
}
So what I mean by this question is that I set
grid.setColumns("month", "numberOfWorkers", "numberOfNewcomers", "numberOfDismissals");
and do not find out method like setRows, so my table looks like: https://www.screencast.com/t/ndDY6tXp, but should be like on first picture.
I do believe there is no way to solve it elegantly without CSS or extending the client grid component.
What you could do though is add your data using
List<MyData> data = repo.findAll();
for(int i = 0; i < data.size(); i++)
grid.addColumn(i)
//String[] months = data.map(x -> x.month).collect(Collectors.toArray)
//String[] nrWork = data.map(x -> x.nrWork).collect(Collectors.toArray)
grid.addRow(months)
grid.addRow(nrWork)
I believe the Vaadin grid (or table) component was designed having the table concept as a starting point. Hence you'd have a unified structure defined by the columns and display any number of same-type data elements, 1 per row. And as far as I know, up to 8.0.4, you can't rotate the structure.
Furthermore, from the user experience perspective, if you have multiple time periods, it'll be easier to scroll them vertically (with the mouse wheel) than horizontally, so I'd suggest discussing the possibility of displaying them just as you started, with the "month", "numberOfWorkers", "numberOfNewcomers" and "numberOfDismissals" columns, and supplying rows of MyData. This also makes it easier to sort, filter, add or edit selected items, whereas for the workaround below, you'd have to do something extra.
If for some reason that's not acceptable at all, you should be able to fake the feature you want with a bit of work (see below sample), but performance and usability wise, there's no guarantees... after all, this is not what it's been designed for.
Code
package com.example.grid;
import com.vaadin.data.ValueProvider;
import com.vaadin.ui.Grid;
import com.vaadin.ui.Notification;
import com.vaadin.ui.VerticalLayout;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.function.Function;
public class HorizontalGrid extends VerticalLayout {
private static final String ROW_CAPTION = "row-caption";
public HorizontalGrid() {
// basic grid setup without column header
Grid<HorizontalDisplayAdapter> grid = new Grid<>();
grid.setSizeFull();
grid.setSelectionMode(Grid.SelectionMode.NONE);
grid.removeHeaderRow(0);
// load some data from the DB or someplace else
List<PeriodSummary> periods = loadPeriods();
// add row headers
grid.addColumn(HorizontalDisplayAdapter::getCaption).setId(ROW_CAPTION).setWidth(150);
// add a column for each period
for (int i = 0; i < periods.size(); i++) {
// save the column index so we ca figure out what to edit later
grid.addColumn(new AdapterValueProvider(i)).setId(String.valueOf(i)).setWidth(60);
}
// wrap the data in "horizontal display adapters"
grid.setItems(
new HorizontalDisplayAdapter("Period", periods, PeriodSummary::getPeriod),
new HorizontalDisplayAdapter("Workers", periods, PeriodSummary::getWorkers),
new HorizontalDisplayAdapter("Newcomers", periods, PeriodSummary::getNewcomers),
new HorizontalDisplayAdapter("Dismissals", periods, PeriodSummary::getDismissals)
);
// retrieve the correct period summary to edit, based on the column that was clicked (unless it's the header)
grid.addItemClickListener(event -> {
if (!ROW_CAPTION.equals(event.getColumn().getId())) {
Integer columnIndex = Integer.valueOf(event.getColumn().getId());
Notification.show("Editing " + event.getItem().getSummary(columnIndex), Notification.Type.ERROR_MESSAGE);
}
});
// freeze first column for scrolling purposes
grid.setFrozenColumnCount(1);
addComponent(grid);
setSizeFull();
}
// generate some dummy data to simulate loading from the DB
private List<PeriodSummary> loadPeriods() {
Random random = new Random();
ArrayList<PeriodSummary> periodSummaries = new ArrayList<>();
for (int i = 0; i < 100; i++) {
periodSummaries.add(new PeriodSummary(i, random.nextInt(100), random.nextInt(100), random.nextInt(100)));
}
return periodSummaries;
}
// adapter to display data in a "horizontal format"
public class HorizontalDisplayAdapter {
// row caption
private final String caption;
// periods for each column
private final List<PeriodSummary> periods;
// used for brevity, a class hierarchy is probably more elegant
private Function<PeriodSummary, Integer> valueExtractor;
public HorizontalDisplayAdapter(String caption, List<PeriodSummary> periods, Function<PeriodSummary, Integer> valueExtractor) {
this.caption = caption;
this.periods = periods;
this.valueExtractor = valueExtractor;
}
public String getCaption() {
return caption;
}
public PeriodSummary getSummary(int columnIndex) {
return periods.get(columnIndex);
}
// extract the data for a certain column
public Integer getValue(int columnIndex) {
return valueExtractor.apply(periods.get(columnIndex));
}
}
// basic bean
public class PeriodSummary {
int period;
int workers;
int newcomers;
int dismissals;
public PeriodSummary(int period, int workers, int newcomers, int dismissals) {
this.period = period;
this.workers = workers;
this.newcomers = newcomers;
this.dismissals = dismissals;
}
public int getPeriod() {
return period;
}
public void setPeriod(int period) {
this.period = period;
}
public int getWorkers() {
return workers;
}
public void setWorkers(int workers) {
this.workers = workers;
}
public int getNewcomers() {
return newcomers;
}
public void setNewcomers(int newcomers) {
this.newcomers = newcomers;
}
public int getDismissals() {
return dismissals;
}
public void setDismissals(int dismissals) {
this.dismissals = dismissals;
}
#Override
public String toString() {
return "PeriodSummary{" +
"period=" + period +
", workers=" + workers +
", newcomers=" + newcomers +
", dismissals=" + dismissals +
'}';
}
}
// value provider for the horizontal display adapters
private class AdapterValueProvider implements ValueProvider<HorizontalDisplayAdapter, Integer> {
// column index is used to retrieve data from the correct summary
private int columnIndex;
public AdapterValueProvider(int columnIndex) {
this.columnIndex = columnIndex;
}
#Override
public Integer apply(HorizontalDisplayAdapter horizontalDisplayAdapter) {
return horizontalDisplayAdapter.getValue(columnIndex);
}
}
}
Result
I have declared an array of objects in the main activity of my Android app. I initialise the array (so far no problem) but as soon as I attempt to initialise one of the elements, my app breaks.
Here is the code from the main activity:
private cClubMemberDetails[] PlayerNames;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
PlayerNames = new cClubMemberDetails[4];
PlayerNames[0] = new cClubMemberDetails();
It's that last line, setting PlayerNames[0], that causes the problem. If I comment out this line, the app works. If I leave it in, the app crashes before showing its opening screen (and long before I make any further use of the PlayerNames array). It's clearly something specific to the cClubMemberDetails class. I can initialise arrays of other types of object no problem. But the class is as simple as can be. Here it is in its entirety:
package uk.org.writerman.scoresheet;
/**
* Created by keith on 18/07/2016.
*/
public class cClubMemberDetails {
private int m_ClubNumber;
private String m_MemberName;
private int m_EBUNumber;
public cClubMemberDetails() {
m_ClubNumber = -1;
m_MemberName = "";
m_EBUNumber = 0;
}
public cClubMemberDetails(int ClubNumber, String MemberName, int EBUNo) {
setTo(ClubNumber, MemberName, EBUNo);
}
public int getClubNumber() { return m_ClubNumber; }
public String getMemberName() { return m_MemberName; }
public int getEBUNumber() { return m_EBUNumber; }
// public void setClubNumber(int ClubNumber) { m_ClubNumber = ClubNumber; }
// public void setMemberName(String MemberName) { m_MemberName = MemberName; }
public void setTo(int ClubNumber, String MemberName, int EBUNumber) {
m_ClubNumber = ClubNumber;
m_MemberName = MemberName;
m_EBUNumber = EBUNumber;
}
public boolean collatesAfter(cClubMemberDetails Another) {
return (m_MemberName.compareToIgnoreCase(Another.getMemberName()) > 0);
}
public boolean collatesBefore(cClubMemberDetails Another) {
return (m_MemberName.compareToIgnoreCase(Another.getMemberName()) < 0);
}
}
Any suggestions what could be going on? I'm at my wits' end (but maybe my wits don't amount to much)
Keith
Thanks for your input. Logcat revealed the problem and it was nothing to do with the code as posted. The program crashed in onSaveInstanceState when writing the contents of my array to the output bundle. Of course, no entries in the array = no opportunity for onSaveInstanceState to go wrong saving them. Doh!
Thanks again. I am slowly getting the hang of this Android stuff and the dev environment.
Keith
I have a ShortestPath class with a Dijkstra algorithm in it and a method called computeRoutes. I also have a form with a search button - I want to call the computeRoutes method from this button but can't figure out how to do this.
public class ShortestPath {
public static void computeRoutes(Node source){
source.minimumDistance = 0;
PriorityQueue<Node> nodeQueue = new PriorityQueue<Node>();
nodeQueue.add(source);
while(!nodeQueue.isEmpty()){
Node u = nodeQueue.poll();
for(Edge e : u.neighbours){
Node n = e.goal;
int weight = e.weight;
int distThruU = u.minimumDistance + weight;
if(distThruU < n.minimumDistance){
nodeQueue.remove(n);
n.minimumDistance = distThruU;
n.previousNode = u;
nodeQueue.add(n);
}
}
}
}
public static List<Node> getShortestRouteTo(Node goal){
List<Node> route = new ArrayList<Node>();
for(Node node = goal; node != null; node = node.previousNode)
route.add(node);
Collections.reverse(route);
return route;
}
}
public class BPForm extends javax.swing.JFrame {
....
private void btnSearchActionPerformed(java.awt.event.ActionEvent evt) {
(I want to call the computeRoutes method here)
In netbeans designer double click this button. It will open code for ActionListener(If you don't know what this is. you should have a look at event handling for buttons) for this button. Just call computeRoutes() here using an Object(Have you already created an object?) of ShortestPath Class.
i suppose you've implemented ActionListener, and in the code you have to overwrite
public void actionPerformed(ActionEvent e) {
if (e.getSource() == computeRoutes) {
// put the logic here
}
.....
}
You just need to implement OnClick Listener for button click event
and then just call your method
http://www.roseindia.net/java/example/java/awt/MouseClick.shtml
here is an example which will helps you
I'm a newbie in java and I have a small problem. I want to access a variable in one class from another. I have three classes and I want to be able to access a variable in the main class to enable me read the array.
The error I am getting is
java.lang.SecurityException: MIDlet not constructed by createMIDlet
Please see the example below. Please bear in mind they're all in the same package.
package tungPackage;
import com.sun.lwuit.*;
import com.sun.lwuit.animations.CommonTransitions;
import com.sun.lwuit.events.ActionEvent;
import com.sun.lwuit.events.ActionListener;
import javax.microedition.midlet.MIDlet;
public class TungMidlet extends MIDlet implements ActionListener {
private Command back = new Command("Back");
private Command ok = new Command("Ok");
public ActionListener commandlistListener = new ActionListener() {
public void actionPerformed(ActionEvent cmd) {
// check which command cliked
if (cmd.getCommand() == back) {
// go back to previous form
mainForm.show();
} else if (cmd.getCommand() == ok) {
// go forward
}
}
};
private List list;
private Form mainForm;
private Label promptLabel;
private housesClass houseClassObject = new housesClass();
public int counter; //this is the variable I want to access in a class called calculate class object.
private int sumAmmt;
public TungMidlet tungMidletObject;
public calculateClass calculateClassObject;
public TungMidlet() {
Display.init(this);
}
private ActionListener applistListener = new ActionListener() {
public void actionPerformed(ActionEvent ae) {
if(list.getSelectedIndex()==0){
counter++;
if (counter>5)
{
//check sum price.
sumAmmt = calculateClassObject.calculateSum();
Dialog x = new Dialog("info");
Label label = new Label("Maximum reached.");
Label label2 = new Label("Sum ammt = "+sumAmmt);
x.addComponent(label);
x.addComponent(label2);
x.addCommand(ok);
x.show();
}
else
{
//calculate the price
String info = houseClassObject.randomHouse();
Dialog x = new Dialog("info");
Label label = new Label(info);
x.addComponent(label);
x.addCommand(ok);
x.show();
}
}
}
};
public void startApp() {
//calculateClassObject = new calculateClass();
//sumAmmt = calculateClassObject.calculate(sumAmmt);
mainForm = new Form("Investment Categories");
promptLabel = new Label("choose category");
list = new List();
list.addItem("House");
list.addItem("Cars");
list.addItem("Schools");
list.addItem("Schools");
list.addItem("Supermarkets");
list.addItem("Stocks");
list.addItem("Land");
list.addActionListener(applistListener);
mainForm.addComponent(promptLabel);
mainForm.addComponent(list);
mainForm.addCommand(back);
mainForm.addCommandListener(commandlistListener);
mainForm.setTransitionInAnimator(CommonTransitions.createSlide(CommonTransitions.SLIDE_HORIZONTAL, true, 1000));
mainForm.show();
}
public void pauseApp() {}
public void destroyApp(boolean unconditional) {}
public void actionPerformed(ActionEvent ae) {
throw new UnsupportedOperationException("Not supported yet.");
}
}
The class I want to access the "counter" variable using is shown below.
package tungPackage;
import java.util.Random;
public class housesClass {
public Random generator = new Random();
public String[] houseArray = new String[5];
public housesClass housesClassObject;
public calculateClass calcobj;// = new calculateClass();
public housesClass()
{
}
public String randomHouse() {
housesClassObject = new housesClass();
houseArray[0] = "Bungalow - 20,000,000 Shillings";
houseArray[1] = "Microhouse - 10,000,000 Shillings";
houseArray[2] = "Flat - 200,000,000 shillings";
houseArray[3] = "Garage apartment - 7,000,000 shillings";
houseArray[4] = "Studio apartment - 13,000,000 shillings";
int rnd = generator.nextInt(houseArray.length);
housesClassObject.housePrices(rnd);///noma
String house = houseArray[rnd];
return house;
}
void housePrices(int houseNumber) {
calcobj = new calculateClass();
TungMidlet tungmidobj = new TungMidlet();
int counter = tungmidobj.counter;
int[] housePriceArray = new int[5];
housePriceArray[0] = 20000000;
housePriceArray[1] = 10000000;
housePriceArray[2] = 200000000;
housePriceArray[3] = 7000000;
housePriceArray[4] = 13000000;
int price = housePriceArray[houseNumber];
calcobj.storePrice(counter,price);
}
}
The other supporting class is shown below.
package tungPackage;
public class calculateClass {
int[] storeArray = new int[5];
public calculateClass()
{
}
public void storePrice(int counter, int number2)
{
storeArray[counter] = number2;
}
public int calculateSum()
{
int sum =0;
for(int i=1; i<6; i++){
sum= sum+storeArray[i];
}
return sum;
}
}
Are you getting an error? It looks like your access code should work.
I can't seem to find anywhere that you actually initialise counter though, so maybe your problem is that you need to put counter = 0; somewhere in your code.
Java is also object oriented so you should avoid accessing like the above and make some 'getter and setter' methods:
public int getCounter() {
return counter;
}
and then call int counter = tungmidobj.getCounter();
remove TungMidlet constructor. If there was something useful to do there, you could also declare it protected - but this is not the case with your code snippet, see below.
Wherever you try to invoke that constructor directly, remove code that does this and find another way to do what you need. If needed, study code examples provided in LWUIT Tutorial - Introduction for how typical things are done in LWUIT.
put statement Display.init() in the beginning of the startApp method,
just like it is done in LWUIT Tutorial - Hello, LWUIT! example code
The reason why you are getting SecurityException is because you invoke TungMidlet constructor directly. Don't do that.
MIDP API documentation for MIDlet constructor states:
Throws:
SecurityException - unless the application management software is creating the MIDlet.
one way is
TungMidlet tungMidlet=new TungMidlet();
System.out.println(tungMidlet.counter);
but know encapsulation
second way is
you can make counter private variable and provide setter and getters.
private int counter;
public void setCounter(int counter){
this.counter=counter;
}
public int getCounter(){
return counter;
}
second way is preferred way as it achieves encapsulation