I have a problem since yesterday.
I'v created a jTable with the WindowBuilder in Eclipse and currently are trying to get the number of the selected row, by using the .getSelectedRow() function, but it is always returning -1 (no row selected), even when I have selected something.
This is my current code for testing the output:
public void checkActiveItem() {
System.out.println(tableBills.getSelectedRow());
}
I try to let it run this way trough a timer and at least that seems working:
Timer time = new Timer();
time.schedule(new TimerTask() {
public void run() {
Frontend f = new Frontend();
f.checkActiveBill();
f.checkActiveItem();
}
}, 250, 250 );
The table currently has just one entry, but even the first row dosn't get returned on selection.
I can create new rows by clicking add (and name them by entering a name in the textfield next to the add button before)
To create a new row I use this code, maybe there is the problem?
public void addBill() {
//maybe need this value somewhere else to, so let -1 and +1 as it is
int numbersOfBills = tableBills.getRowCount() - 1;
Bill newBill = new Bill(txtBillName.getText(), numbersOfBills + 1);
DefaultTableModel billModel2 = (DefaultTableModel) tableBills.getModel();
int billNb = numbersOfBills + 1;
billModel2.addRow(new Object[] {newBill.getBillNr(),newBill.getBillName(), newBill.getItemsInBill()});
}
The reason why you are getting -1 from getSelectedRow() even though you seem to have "added a row" is, because the Frontend object you are calling checkActiveItem() on is a completely different Frontend object than the one you are seeing.
The issue is here, inside of your Timers run():
Frontend f = new Frontend();
You create a new Frontend object for each timer iteration. And you call checkActiveItem() on exactly this object, not on the frontend you are seeing and pressing buttons on. Hence, the incorrect output.
As a solution, don't create new Frontends, instead, call checkActiveItem() on your original frontend object, which you made visible.
Related
Im using a JTable , loading on it a different data depending on the button pressed.
The problem is : when one of the data is loaded, if i try to load the other one, and pass ther mouse over the header or a cell, it updates the header/cell with the data from the first input, if there is data on the header/cell selected.
Any ideas on how to solve it? That's the code im using.
private static void setCompromissosTable(Object[][] data, Object[] header){
try{
compromissosTable.removeAll();
} catch(Exception e){
e.printStackTrace();
}
compromissosTable = new JTable(data, header);
compromissosTable.setRowSelectionAllowed(true);
// Make the entire row selectable, but not editable
int columnMax = compromissosTable.getColumnCount();
for(int column = 0; column < columnMax; column++){
Class<?> col_class = compromissosTable.getColumnClass(column);
compromissosTable.setDefaultEditor(col_class, null);
}
scrollPane = new JScrollPane(compromissosTable);
pane.add(scrollPane);
scrollPane.setBounds(btnAddCompromisso.getX(),
btnAddCompromisso.getHeight() + btnAddCompromisso.getY() + 5
, frame1.getWidth() - 20
, frame1.getHeight() - 20);
compromissosTable.revalidate();
compromissosTable.repaint();
compromissosTable.addMouseListener(new MouseAdapter() {}
//Change mouse behavior.
);
}
This is suspicious...
compromissosTable = new JTable(data, header);
//...
scrollPane = new JScrollPane(compromissosTable);
pane.add(scrollPane);
Basically, assuming that each time you want to switch data sets, you are calling this method, you are creating a new JTable and JScrollPane each time and then are adding it onto the UI...
What about the previous JTable?
Next is this...
scrollPane.setBounds(btnAddCompromisso.getX(),
btnAddCompromisso.getHeight() + btnAddCompromisso.getY() + 5
, frame1.getWidth() - 20
, frame1.getHeight() - 20);
This looks like you're using a null layout. Basically what it "looks" like is happening, is you're just stacking the JScrollPanes ontop of each other, which would explain, in part, the graphics glitches, as the components are actually been added at the same z-deepthness (essentially) and are competing with each other then they are updated.
Two simple answers...
Don't use null layouts. Sure they "seem" like a good idea, but they have a tendency to turn around and bite you in strange and wonderful ways which are hard to diagnose and fix. Use the layout management API which Swing was designed around
Update the JTables model instead of creating a new JTable/JScrollPane each time
See How to use tables and Laying Out Components Within a Container
I am making a simple sequence from randomly generated numbers. Each number will show an image linked to it.
for example,
the value 1 will show a picture of a cat.
value 2 a dog
and value 3 a mouse.
Each image has it's own dedicated imageview, the layout looks like this by default, i.e image views that store black until it's number is called:
Each time the sequence increments. so on the second run two images will show, on the third 3 will show and so on.
The problem I am having is that all the images show at once. So for sequence one just the one image flashes (which is what I want). but on the second run both images show at once together instead of showing the first image then the second.
So to clarify let's say on the four runs the stored array is 1,2,3,3 I would want
image 1 to show, and disappear.then
image 2 show and disappear. then
image 3 show and disappear
and then image 3 to show and disappear.
But what I actually get is on the fourth run 1,2&3 will show at once and disappear at the same time together. How can I break this up to achieve what I want. I have tried many methods and the same result happens. I can't get my head around this problem.
Here is my code:
ArrayList<Integer> stored_vals = new ArrayList<Integer>();
public void Gen() {
int i=0 ;
Random rand = new Random();
int rndInt = rand.nextInt(3)+ 1 ;
list.add(rndInt);
int totalElements = list.size();
Log.d("LOOK", Integer.toString(rndInt));
Log.i("VALUE LIST ", list.toString()+" <<<<LIST HERE");
while(i < totalElements) {
retval =list.get(i);
Log.d("RETVAL", Integer.toString(retval));
String imgName = "flash" + retval;
int id = getResources().getIdentifier(imgName, "drawable", getPackageName());
if (retval==1){
Cat.setImageResource(id);
Reset_View();
}
else if (retval==2){
Dog.setImageResource(id);
Reset_View();
}
else if (retval==3){
Mouse.setImageResource(id);
Reset_View();
}
i++;
}
}
To try and delay the images showing at one at a time and to reset to it's default after showing for a few seconds I call Reset_View(); which is the following code:
CountDownTimer Reset_View = new CountDownTimer(1000 , 0010){
public void onTick(long millisUntilFinished) {
}
public void onFinish() {
Centre.setImageResource(R.drawable.i1);
upperRight.setImageResource(R.drawable.i2);
lowerRight.setImageResource(R.drawable.i3);
lowerLeft.setImageResource(R.drawable.i4);
upperLeft.setImageResource(R.drawable.i5);
}
};
So how can I achieve what I want. I have been stuck on this idea for weeks.
This is a high level idea of what you could do.
Put all the showing and hiding in the countdowntimer. So basically you would need to handle the 'onTick' and hide or show the images when needed. Something like 'time > 0 and < 1000' hide all but the first image. 1000 to 2000, show only second image. 2000 to 3000, show only third image. On finish, hide all.
When you instantiate the timer after the for loop, if retval is 1 - then 1000 would be how long to run, retval = 2, then 2000, retval = 3, then 3000.
There is probably a better way to do it but this is the first thing that comes to my mind.
One problem might be that Reset_View(); is not a method. The line even states public CLASS Reset_View(); extends CountDownTimer. ( I don't know ho that can be a valid class name but whatever) Therefore, calling Reset_View(); will not actually do anything.
You need to change the name of the class to something else, I suggest ResetTimer. Then to reset the Views you must create an instance of ResetTimer and use its start() method like this:
new ResetTimer(1000, 1000).start();
Note: the first number in the constructor is the total time (in milliseconds) until the timers onFinish() is called. The second one it the time between the onTick()s. Since you don't use this method, the number you put in there doesn't matter.
I have a JTable that tracks the amount of time a person has been waiting to be seated at a restaurant. My problem is that every second, when the timer 'ticks', the selection that is on a row is removed. In other words, if you click a row it becomes highlighted with a blue background and outline, but then when the timer ticks the blue goes away.
ActionListener actListner = new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
aTable.updateTime();
}
};
Timer timer = new Timer(1000, actListner);
timer.start();
This is in the main class
public void updateTime()
{
data.updateTime();
fireTableDataChanged();
}
This is in the table model
public void updateTime()
{
Date newTime = new Date();
for (int i = 0; i < startTime.size(); i++)
{
this.setTimeWaiting(i, hoursMin.format(new Date(newTime.getTime() - startTime.get(i).getTime())));
}
}
This is in the data model.
fireTableDataChanged() tells the table that the data may have changed in any way, and essentially you use it when the entire data set has completely changed (e.g. you've replaced all of the previous contents with new information). The table will redraw itself from scratch and may also clear the selection.
Instead, use the more conservative fireTableCellUpdated(int,int) and specify each cell that may have been modified due to the time change (presumably, everything in the "wait time" column).
You could also use fireTableRowsUpdated(int,int) and specify the entire range of rows that have been updated in one call, but generally it's better to stick to the conservative side to minimize unnecessary redraws.
I have an ArrayList called conditionList which stores condition names.
Whenever one is added/edited or deleted, the Lists on my GUI update no problems.
You can see below that i use 2 models... a DefaultListModel called condListModel and a DefaultComboBoxModel called conditionModel.
The code i have below is for the method editCondition(), at this stage the text is already changed on the GUI and is being submitted here. On my GUI, after ive submitted the change, the ComboBox and JList change no problem so im sure that the model changes are correct.
HOWEVER MY PROBLEM IS: When i save the ArrayList conditionList through serialization, and then load it back up, the change is gone. SO I think there is problem in my code with changing the String Value in the ArrayList(named conditionList), can anyone have a look and see if u notice an issue
String conString = jListCondition.getSelectedValue().toString();
for(String c: conditionList)
{
if(conString.compareTo(c) == 0)
{
String temp = entConName.getText();
c = temp;
//edit the Condition jList model
int x = condListModel.indexOf(conString);
condListModel.setElementAt(temp, x);
jListCondition.setModel(condListModel);
//edit the Condition comboBox model
int i = conditionModel.getIndexOf(conString);
conditionModel.insertElementAt(temp, i);
conditionModel.removeElement(conString);
entCondition.setModel(conditionModel);
//reset buttons
editConConfirm.setEnabled(false);
editCon.setEnabled(false);
deleteCon.setEnabled(false);
entConName.setText("");
addCon.setEnabled(true);
}
}
1) I am using a ListView to populate some 2 labels from database. The table has 100 rows so I get 100 <TD>s. This works fine.
this.selectView = new PageableListView("selectedBG", new PropertyModel(this, "selectedList"), 10) {
private static final long serialVersionUID = 1L;
#Override
protected void populateItem(final ListItem item){
selParentGclOrg = new Label("selParGclOrgId", new PropertyModel(gclOrg, "parentGclOrgId"));
selParentGclOrg.setOutputMarkupId(true);
final AjaxLink ajl = new AjaxLink("clickMe"){
public void onClick(AjaxRequestTarget target){
chilgGcl = gclOrg.getGclOrgId();
selectPopUp.show(target);
}
};
ajl.add(selParentGclOrg);
final Label lblGclOrg = new Label("selGclOrgId", Integer.valueOf(gclOrg.getGclOrgId()).toString());
item.add(ajl);
item.add(lblGclOrg);
}
}
2) One label is hyperlinked and opens a popup window from which I can select possible values for label 2. The popup window opens perfectly.
selectPopUp = new select_popUP("showModal",container){
#Override
public void onSelect(AjaxRequestTarget target, int gclOrgId){
selParentGclOrg.setModelObject(Integer.toString(gclOrgId));
target.addComponent(selectView);
close(target);
}
#Override
public void onCancel(AjaxRequestTarget target){
close(target);
}
};
3) In the popup window, possible values are hyperlinked. Clicking on it closes the popup window and sends the possible value to the main page. This works ok... I think.
4) The new value is assigned to Label 2 using:
target.addComponent(selectView);
This is where I am getting stuck. Wicket is supposed to change the label on the same row (at least, I think) but it's updating the Label 2 of the last row.
What am I doing wrong?
I haven't run through your logic for selecting rows, but I believe my solution to a similar issue will help you. The problem is that ListViews don't automatically refresh each of their rows individually when AJAX requests come in. The solution I used was to wrap the whole shebang in a WebMarkupContainer. When that's done, everything will get regenerated on the request.
I think the relevant parts of your code are missing from the post...
Like:
where gclOrg is set
where selParentGclOrgis set for the ModalWindow
Judging from what I can see, I would put my bet on an issue with populateItem beeing run way after the rest of the code in your class... Most of your code will be run on constructor time or onBeforeRender, populateItem will be called sometimes during rendering... But that's just a wild guess.