I'd like to add a checkbox to a column. I am using a tableViewer. The user shouldn't be able to edit the checkbox.
Google wasn't helpful so far, so I came here.
my labelprovider looks like this:
this.tableViewer2 = new TableViewer(table1);
this.tableViewer2.setContentProvider(new ArrayContentProvider());
this.tableViewer2.setLabelProvider(new ITableLabelProvider() {
#Override
public String getColumnText(Object element, int columnIndex) {
Platz p = (Platz) element;
switch (columnIndex) {
case 0:
return p.getReihe().getReihenfolge().toString();
case 1:
return p.getNummer().toString();
case 2:
return p.getKategorie().getPreisstd().toString();
}
return null;
}
});
I'd like to add a fourth column with a checkbox but I don't know how.
Thanks in advance!
ITableLableProvider has a method getColumnImage().
Just override it and return an image of the checkbox.
this.tableViewer2 = new TableViewer(table1);
this.tableViewer2.setContentProvider(new ArrayContentProvider());
this.tableViewer2.setLabelProvider(new ITableLabelProvider() {
#Override
public Image getColumnImage(Object element, int columnIndex) {
//do magic here and return an image :)
}
#Override
public String getColumnText(Object element, int columnIndex) {
Platz p = (Platz) element;
switch (columnIndex) {
case 0:
return p.getReihe().getReihenfolge().toString();
case 1:
return p.getNummer().toString();
case 2:
return p.getKategorie().getPreisstd().toString();
}
return null;
}
});
Related
Hello I want to show the index number of each row in the table I tried with for loop but I had no success can you tell me how to do that ?
I need a logic that return the number of each row in the line "case 0: return getRowCount();" it return the total of rows only
public class TableModel extends AbstractTableModel{
UsersDao ud = new usersDao();
private List<Users> users;
public TableModel() throws Exception {
this.users = (ArrayList<Users>)ud.getUsersList();
}
private DateFormat df = new SimpleDateFormat("dd-MM-yyyy");
#Override
public int getRowCount() {
return users.size();
}
#Override
public int getColumnCount() {
return 10;
}
#Override
public Object getValueAt(int rowIndex, int columnIndex) {
Users u = users.get(rowIndex);
switch(columnIndex){
case 0: return getRowCount();
case 1: return u.getName();
case 2: return u.getAge();
case 3: return u.getGender();
default: return "";
}
}
public String getColumnName(int column){
switch(column){
case 0: return "NO";
case 1: return "NAME";
case 2: return "AGE";
case 3: return "GENDER";
default: return "";
}
}
public void addUser(Users u){
users.add(u);
fireTableRowsInserted(users.size()-1, users.size()-1);
}
public void deletePatient(Users u){
users.remove(p);
fireTableRowsInserted(users.size()-1, users.size()-1);
}
}
I think you have a problem in getValueAt
#Override
public Object getValueAt(int rowIndex, int columnIndex) {
Users u = users.get(rowIndex);
switch(columnIndex){
case 0: return rowIndex; // return rowIndex rather than the total number of rows
case 1: return u.getName();
case 2: return u.getAge();
case 3: return u.getGender();
default: return "";
}
}
I have a TabLayout that uses a custom adaptor (TabPagerAdapter.java), it's initialised with this function (I previously had the problem that things didn't update when replaced so it removes everything before initializing):
public boolean setPagerViewContents(int mode, ArrayList<String> titles) {
try {
mTabLayout.removeAllTabs();
mViewPager.setAdapter(null);
mViewPager.removeAllViews();
for (int i = 0; i < titles.size(); i++) {
mTabLayout.addTab(mTabLayout.newTab().setText(titles.get(i)));
}
mAdapter = new TabPagerAdapter(getSupportFragmentManager(), titles.size(), mode);
mViewPager.setAdapter(mAdapter);
mViewPager.invalidate();
return true;
} catch (Exception e) {
mErrorReporter.reportError(e);
return false;
}
}
Custom adapter (TabPageAdapter.java):
public class TabPagerAdapter extends FragmentStatePagerAdapter {
int mTabCount;
int mLayoutType;
public TabPagerAdapter(FragmentManager fm, int numberOfTabs, int layoutType) {
super(fm);
this.mTabCount = numberOfTabs;
this.mLayoutType = layoutType;
}
#Override
public Fragment getItem(int position) {
switch (mLayoutType) {
case 0:
switch (position) {
case 0:
return new Fragment15();
case 1:
return new Fragment1();
default:
return null;
}
case 1:
switch (position) {
case 0:
return new Fragment3();
case 1:
return new Fragment2();
default:
return null;
}
default:
return null;
}
}
#Override
public int getCount() {
return mTabCount;
}
#Override
public int getItemPosition(Object object) {
return POSITION_NONE;
}
}
When the app starts the function setPagerViewContents (with the right mode and titles) is called, after that mViewPager.setCurrentItem(number) is called to display the right tab. But now the fragment displayed in mViewPager is correct but when clicking on a tab title the fragment is the same displayed at start (the one with the index number, not the one that should have been displayed), when tapping the first (number) tab again and then again to some other tab (not number) the shown tab is correct.
The most annoying thing here is that it's NOT consistent, it sometimes happens, sometimes doesn't and it doesn't happen when the debugger is attached so I can't debug it properly. If some other code is needed please do tell, I'll update this post as quick as possible because I'd really love to see this solved, for my own sanity and for the happiness of my few users.
Change your following method:
public boolean setPagerViewContents(int mode, ArrayList<String> titles) {
try {
mAdapter = new TabPagerAdapter(getSupportFragmentManager(), titles.size(), mode);
mViewPager.setAdapter(mAdapter);
mTabLayout.setupWithViewPager(mViewPager);
return true;
} catch (Exception e) {
mErrorReporter.reportError(e);
return false;
}
}
and set text on tabs in TabPagerAdapter by overriding following method:
#Override
public CharSequence getPageTitle(int position) {
return context.getString(tabTitles[position]);
}
In settngs existed checkbox, if it is checked, then a specific fragment must not be loaded. I have just 4 fragment and I use FragmentStatePagerAdapter to show them.
public class TabPagerAdapter extends FragmentStatePagerAdapter {
public TabPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int i) {
switch (i) {
case 0:
return new Fragment_One();
case 1:
return new Fragment_Two();
case 2:
return new Fragment_Three();
case 3:
return new Fragment_Four();
}
return null;
}
#Override
public int getCount() {
return 4;
}
}
As show fragments, which are not only check in the settings? I get value (true of false (Check or Uncheck) fragment, but how do not show this fragment, i dont know.
You have to adapt your getItem() method as well as the getCount() method.
Let's assume you have a method shouldShowFragment(int fragmentNumber) that tells me for a given fragment number from 0 to 3, whether it should be shown or not (depending on the settings).
Now, implement getCount() like so to return the number of fragments that should be shown:
public int getCount() {
int cnt = 0;
for (int i = 0; i < 4; i++) {
if (shouldShowFragment(i)) cnt++;
}
return cnt;
}
And implement getItem() like so to take the not showing fragments into account:
public Fragment getItem(int position) {
int cnt = -1;
for (int i = 0; i < 4; i++) {
if (shouldShowFragment(i)) cnt++;
if (cnt == position) {
switch(i) {
case 0 : return new Fragment_One();
case 1 : return new Fragment_Two();
case 2 : return new Fragment_Three();
case 3 : return new Fragment_Four();
}
}
}
return null;
}
First of all Save the all check buttons state globally(i.e. in shared preferences like btn1.setChecked == true/false whatever) and in above code do like below:-
#Override
public Fragment getItem(int i) {
switch (i) {
case 0:
if(btn0.isChecked == true)
return new Fragment_One();
case 1:
if(btn1.isChecked == true)
return new Fragment_Two();
case 2:
if(btn2.isChecked == true)
return new Fragment_Three();
case 3:
if(btn3.isChecked == true)
return new Fragment_Four();
}
return null;
}
I am working on my Java MySql project. I am showing workers in Kitchen Department meals that they need to prepare for guests. When my app starts it fetches unprepared meals from database and displays them in JTable. After they have done it, they check field "done" in table and they press confirm button. Now I want that my table is refreshed when they click order button and that table shows only meals that they need to prepare. I don't have problems with that, I just execute query and I can get unprepared meals from database. My problem is that I don't know how to refresh table. In code I have wrote comment where I think that JTable needs to be refreshed. I am using AbstractTableModel.
Picture of my JTable: http://i.imgur.com/mfO2ts9.jpg
Here is my TableModel class:
public class KitchenTableModel extends AbstractTableModel {
private ArrayList<WrapperKitchen> hrana;
public KitchenTableModel(ArrayList<WrapperKitchen> hrana2) {
this.hrana = hrana2;
}
#Override
public int getColumnCount() {
// TODO Auto-generated method stub
return 8;
}
#Override
public int getRowCount() {
// TODO Auto-generated method stub
return hrana.size();
}
public String getColumnName(int columnIndex) {
switch (columnIndex) {
case 0:return "Order number";
case 1:return "Room";
case 2:return "Category";
case 3:return "Meal";
case 4:return "Quantity";
case 5:return "Note";
case 6:return "Order time";
case 7:return "Done";
}
return null;
}
#Override
public Object getValueAt(int rowIndex, int columnIndex) {
// TODO Auto-generated method stub
WrapperKitchen jelo = hrana.get(rowIndex);
switch (columnIndex) {
case 0:return jelo.getIdUslugaHrana();
case 1:return jelo.getBrojSobe();
case 2:return jelo.getNazivKategorija();
case 3:return jelo.getNazivHrane();
case 4:return jelo.getKolicina();
case 5:return jelo.getNapomena();
case 6:return jelo.getDatumVrijeme();
case 7:return jelo.getIzvrseno();
}
return null;
}
#Override
public Class<?> getColumnClass(int columnIndex) {
if (columnIndex == 7)
return Boolean.class;
return super.getColumnClass(columnIndex);
}
#Override
public boolean isCellEditable(int rowIndex, int colIndex) {
return (colIndex == 7);
}
#Override
public void setValueAt(Object inValue, int inRow, int inCol) {
if(inRow < 0 || inCol < 0 || inRow >= getRowCount() )
return;
WrapperKitchen jelo= hrana.get(inRow);
switch (inCol) {
case 0:jelo.setIdUslugaHrana((int)inValue);break;
case 1:jelo.setBrojSobe((int)inValue);break;
case 2:jelo.setNazivKategorija((String)inValue);break;
case 3:jelo.setNazivHrane((String)inValue);break;
case 4:jelo.setKolicina((int)inValue);break;
case 5:jelo.setNapomena((String)inValue);break;
case 6:jelo.setDatumVrijeme((Date)inValue);break;
case 7:jelo.setIzvrseno((boolean)inValue);break;
default: throw new RuntimeException("something bad happen incorrect column " + inCol);
}
fireTableCellUpdated(inRow, inCol);
}
}
Here is code of my JButton ActionListener with commented line:
ActionListener a1 = new ActionListener() {
public void actionPerformed(ActionEvent a) {
IzvrseneNarudzbe.clear();
boolean izvrseno;
int id;
for(int red=0;red<KuhinjaListaJela.size();red++){
Object obj = Tablica.getModel().getValueAt(red, 7);
izvrseno=(boolean)obj;
if(izvrseno==true)
{
Object obj2 = Tablica.getModel().getValueAt(red, 0);
id=(int)obj2;
IzvrseneNarudzbe.add(id);
}
}
izvrsiQuery();
//IN THIS LINE I NEED TO REFRESH MY JTABLE
}
void izvrsiQuery(){
for(int i=0;i<IzvrseneNarudzbe.size();i++){
String SqlQuery="UPDATE `room_service`.`usluga_hrana` SET `izvrseno` = '" + 1 +"' WHERE `usluga_hrana`.`id_usluga_hrana` ="+IzvrseneNarudzbe.get(i);
try {
Class.forName("com.mysql.jdbc.Driver");
Connection con = (Connection) DriverManager
.getConnection("jdbc:mysql://"
+ "localhost:3306/room_service",
"root", "");
Statement Stat = (Statement) con.createStatement();
int Rez = Stat.executeUpdate(SqlQuery);
Stat.close();
} catch (Exception e2) {
System.out.println(e2);
}
}
}
};
In your button's ActionListener, you should invoke setValueAt() to update your TableModel with the results of your query. The model will fireTableCellUpdated() to notify the table to update itself.
I've created a menu with a intents to access different activities, but I have a strange behavior, it always goes through all the cases of the switch statement after the statement selected , I've reviewed the value of the variable item and is correct, any ideas what could be wrong?
the snippet of code that represents the menu is:
public static final int wiifidi = 0;
public static final int cuentaint = 1;
public static final int cajerosint = 2;
public static final int indicadoresint = 3;
public static final int promoint = 5;
public static final int contactoint = 4;
....
....
....
#Override
//add the items to the menu on the class
public boolean onCreateOptionsMenu(Menu menu) {
boolean result = super.onCreateOptionsMenu(menu);
menu.add(0,wiifidi, 0, R.string.menu_wifi);
menu.add(0,cuentaint, 0, R.string.menu_cuenta);
menu.add(0,cajerosint,0,R.string.menu_cajeros);
menu.add(0,indicadoresint,0,R.string.menu_indicadores);
menu.add(0,contactoint,0,R.string.menu_contacto);
menu.add(0,promoint,0,R.string.menu_promo);
return result;
}
#Override
//handle everything that happens when an item of menu is selected
public boolean onOptionsItemSelected(MenuItem item) {
Toast.makeText(getApplicationContext(), "el item es " +item.getItemId(), Toast.LENGTH_LONG).show();
switch (item.getItemId()) {
case wiifidi:
wifistatus();
case cuentaint:{
consulta();
}
case cajerosint:{
cajero();
}
case indicadoresint:{
indicador();
}
case contactoint:{
contacto();
}
case promoint:{
promocion();
}
}
return super.onOptionsItemSelected(item);
}
Remember to break out of your switches.
switch (item.getItemId())
{
case wiifidi:
wifistatus();
break;
case cuentaint:
consulta();
break;
case cajerosint:
cajero();
break;
case indicadoresint:
indicador();
break;
case contactoint:
contacto();
break;
case promoint:
promocion();
break;
}
Specify break
case wiifidi:
wifistatus();
break;