Aligning buttons in a Scala MainFrame using setAlignmentX - scala-swing

I'm having trouble getting the buttons on a simple Scala MainFrame GUI to align at center.
Have tried many combos of buttonname.peer.setAlignmentX and Y with no effect.
The code is listed below:
object CGui extends SimpleSwingApplication
{
def top = new MainFrame
{
title = "Client Dashboard"
var tbox = new TextArea(4,20) { text = "Welcome to the dashboard"}
val stats_button = new Button { text = " View Statistics " }
val sp_button = new Button { text = " Add Server Process " }
val cp_button = new Button { text = " Add Local Process " }
val conn_button = new Button { text = " Connect" }
val quit_button = new Button { text = " Quit" }
// GUI Components *************************************************End *****
// GUI SetUp ******************************************************Start *****
contents = new BoxPanel(Orientation.Vertical)
{
contents += new Label(" ")
contents += tbox
contents += new Label(" ")
contents += stats_button
contents += new Label(" ")
contents += sp_button
contents += new Label(" ")
contents += cp_button
contents += new Label(" ")
contents += new FlowPanel {
contents += new Label(" ")
contents += conn_button
contents += quit_button }
border = Swing.EmptyBorder(20, 20, 20, 20)
}
listenTo(sp_button, cp_button, stats_button, conn_button, quit_button)
// GUI SetUp ******************************************************End *****
Can anyone point me to any descriptions of setAlignmentX,Y usage.
Would ScalaDoc have examples of usage ?

After investigating the post Scala Swing Component alignment, I was able to use BorderPanel { }
to position the buttons as in the code below:
// GUI SetUp ******************************************************Start *****
contents = new BoxPanel(Orientation.Vertical)
{
contents += new Label(" ")
contents += tbox
contents += new Label(" ")
contents += new BorderPanel {
add(stats_button, BorderPanel.Position.Center)}
contents += new Label(" ")
contents += new BorderPanel {
add(sp_button, BorderPanel.Position.Center)}
contents += new Label(" ")
contents += new BorderPanel {
add(cp_button, BorderPanel.Position.Center)}
contents += new Label(" ")
contents += new FlowPanel
{
contents += new BorderPanel {
add(conn_button, BorderPanel.Position.West)
add(quit_button, BorderPanel.Position.Center) }
}
border = Swing.EmptyBorder(20, 20, 20, 20)
}
listenTo(sp_button, cp_button, stats_button, conn_button, quit_button)
// GUI SetUp ******************************************************End *****
Should have looked at it before posting mine.

Related

how to get value after the record has been save?

I have an issue after saving the record in Stock Items screen, it doesn't work the way I want it to work.
I want to get the concatenated values after saving the record.
I customized the description field to concatenate the values ​​of some attributes.
I have all the following code inside RowPersisted event, the way I get that data is by using BQL query:
protected void InventoryItem_RowPersisted(PXCache cache, PXRowPersistedEventArgs e, PXRowPersisted InvokeBaseHandler)
{
string attr = "";
string itemClassDesc = "";
string itemClassCD = "";
if (InvokeBaseHandler != null)
InvokeBaseHandler(cache, e);
var row = (InventoryItem)e.Row;
PXResultset<InventoryItem> result =
PXSelectJoin<InventoryItem,
InnerJoin<CSAnswers,
On<CSAnswers.refNoteID, Equal<InventoryItem.noteID>>,
LeftJoin<CSAttributeDetail,
On<CSAttributeDetail.attributeID, Equal<CSAnswers.attributeID>,
And<CSAttributeDetail.valueID, Equal<CSAnswers.value>>>,
InnerJoin<CSAttribute,
On<CSAttribute.attributeID, Equal<CSAnswers.attributeID>>>>>,
Where<InventoryItem.inventoryID, Equal<Current<InventoryItem.inventoryID>>>,
OrderBy<Asc<CSAnswers.attributeID>>>.Select(this.Base);
foreach (PXResult<InventoryItem, CSAnswers, CSAttributeDetail, CSAttribute> record in result)
{
InventoryItem inventory = (InventoryItem)record;
CSAnswers answers = (CSAnswers)record;
CSAttributeDetail detail = (CSAttributeDetail)record;
CSAttribute attribute = (CSAttribute)record;
switch (itemClassCD)
{
case "0531":
if (attribute.AttributeID == "A1" || attribute.AttributeID == "A2" || attribute.AttributeID == "A3")
{
if (attribute.AttributeID == "A2")
attr += answers.Value + " ";
else
if (attribute.ControlType == 1)
attr += attribute.Description + " " + answers.Value + " ";
else
attr += attribute.Description + " " + detail.Description + " ";
}
break;
default:
break;
}
}
cache.SetValue<InventoryItem.descr>(row, itemClassDesc + " " + attr);
}
It works well because the description field concatenates the values when I press the save button but it always get a previous record.
Can you help me with this?
Thanks!
The RowPersisted event is fired after the item has been saved. You need to use the RowPersisting event or override the Persist function of the graph and run your code before invoking the base function.

I have this array class that is not removing items after re-running my recyclerview

The below class is used in an array.
package com.example.ShivitoMGO;
public class RoomTable {
public String RoomName,UpDown,minmaxint;
}
Main Activity
static ArrayList <RoomTable> CountCheck = new ArrayList<>();
public void playerup(View vvv){
st_spinner = v1.findViewById(R.id.spinner);
st_reportLayout = v1.findViewById(R.id.reportlayout);
st_Leanervidimg = v1.findViewById(R.id.Linearvidcopy);
TextView roomname = v1.findViewById(R.id.action_Players1);
RoomTable roomtb = new RoomTable();
if (CountCheck.size() == 0){
//playerupdown = "up";
Toast.makeText(this, "Will notify when " + mRooms.get(position1) + " players increase #" + mRoomSize.get(position1), Toast.LENGTH_LONG).show();
String[] minmaxval = mRoomSize.get(position1).split("/");
CountCheckint = Integer.parseInt(minmaxval[0].trim());
//CountCheck = (String) mRooms.get(position1);
roomtb.RoomName = (String) mRooms.get(position1);
roomtb.minmaxint = minmaxval[0].trim();
roomtb.UpDown = "up";
Log.d("added: ", "it was added");
CountCheck.add(roomtb);
Log.d("RoomTableadd: ",roomtb.RoomName+ " " + roomtb.minmaxint +" " +roomtb.UpDown);
st_reportLayout.setVisibility(View.GONE);
Log.d("Longclickhere: ", mRoomSize.get(position1));
Log.d("RoomNameCount ", String.valueOf(CountCheck.get(0).RoomName));
}else {
int exist1 = 0;
int poss;
for (int i = 0; i < CountCheck.size(); i++) {
if (roomname.getText() == CountCheck.get(i).RoomName) {
Log.d("RoomNametxt: " , CountCheck.get(i).RoomName);
Log.d("RoomNametxt: ", (String) roomname.getText());
Toast.makeText(this, "Notification " + CountCheck.get(i).RoomName + " OFF!", Toast.LENGTH_LONG).show();
Log.d("Removed item: ", String.valueOf(CountCheck.size()));
CountCheck.remove(i);
Log.d("Removed item: ", String.valueOf(CountCheck.size()));
st_reportLayout.setVisibility(View.GONE);
exist1 = 1;
poss = i;
} else {
exist1 = 0;
}
}
if (exist1 == 0) {
Toast.makeText(this, "Will notify when " + mRooms.get(position1) + " players increase #" + mRoomSize.get(position1), Toast.LENGTH_LONG).show();
String[] minmaxval = mRoomSize.get(position1).split("/");
CountCheckint = Integer.parseInt(minmaxval[0].trim());
//CountCheck = (String) mRooms.get(position1);
roomtb.RoomName = (String) mRooms.get(position1);
roomtb.minmaxint = minmaxval[0].trim();
roomtb.UpDown = "up";
Log.d("added: ", "it was added");
CountCheck.add(roomtb);
Log.d("RoomTableadd: ", roomtb.RoomName + " " + roomtb.minmaxint + " " + roomtb.UpDown);
st_reportLayout.setVisibility(View.GONE);
}
Log.d("CountSize: ", String.valueOf(CountCheck.size()));
for (int xb = 0;xb<CountCheck.size();xb++) {
try {
Log.d("RoomNameCount ", String.valueOf(CountCheck.get(xb).RoomName));
} catch (Exception e) {
Log.d("Out of range ", String.valueOf(e));
}
}
}
}
Recycler-View
#Override
public boolean onLongClick(View v) {
// Launch your web intent
if (CountCheck.size() != 0){
Log.d("Longclickhere: ",mRoomSize.get(position));
Toast.makeText(mContext, mRoomSize.get(position), Toast.LENGTH_SHORT).show();
Toast.makeText(mContext, "Will notify when "+mRooms.get(position)+" players decrease", Toast.LENGTH_SHORT).show();
String[] minmaxval = mRoomSize.get(position).split("/");
MainActivity.CountCheckint = Integer.parseInt(minmaxval[0].trim());
} else{
//MainActivity.CountCheck = "";
Toast.makeText(mContext, "Player Decrease notification OFF", Toast.LENGTH_SHORT).show();
}
return true;
In this app the recycler view creates the "rooms" and if the room is selected the textview and 2 other values are put in to the RoomTable. these are stored and used in a service to check if ether of the other to values change. Everything works as intended unless i use the swip-to-refresh witch runs the recycler-view again. If i do not refresh and i select the same item in the recycler-view it will remove it from CountCheck . However if i run the refresh and select the same recycler-view item that i selected previously it will add it instead of removing it. This Makes no since to me because i use a for loop to Check the CountCheck.get(i).RoomName aka the textview and if the names are the same then my if statement will remove instead of add. is it somehow possible i'm ending up with 2 CountCheck Objects????? with the same name???? Please I'm out of ideas on this one. Thanks.
I dont remember why. Maybe someone can explain but i changed this line
if (roomname.getText() == CountCheck.get(i).RoomName)
To this
if (roomname.getText().equals(CountCheck.get(i).RoomName));
and that fixed the issue. please let me know the difference if you are reading this.

Error While Export To Excel in sharepoint 2010 Programmatically

The file you are trying is in a different format than specified by the file extension.Verify that the file is not corrupted and is from a trusted source before opening the file.Do you want to open the file now?
and this 'export to excel' dead the events on the page.
Here is my code
public void ExportToExcelitems(DataTable dt, string fileNameWithoutExt)
{
if (dt.Rows.Count > 0)
{
string filename = fileNameWithoutExt + ".xls";
System.IO.StringWriter tw = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);
var dgGrid = new GridView();
dgGrid.DataSource = dt;
dgGrid.DataBind();
dgGrid.HeaderRow.BackColor = System.Drawing.Color.White;
dgGrid.HeaderRow.BackColor = System.Drawing.Color.White;
foreach (GridViewRow row in dgGrid.Rows)
{
row.BackColor = System.Drawing.Color.White;
foreach (TableCell cell in row.Cells)
{
if (row.RowIndex % 2 == 0)
{
cell.BackColor = dgGrid.RowStyle.BackColor;
}
else
{
cell.BackColor = System.Drawing.Color.LightGray;
}
}
}
dgGrid.AutoGenerateColumns = false;
dgGrid.RenderControl(hw);
HttpContext.Current.Response.ContentType = "application/vnd.ms-excel";
HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment; filename=" + filename + "");
this.EnableViewState = false;
HttpContext.Current.Response.Write(tw.ToString());
HttpContext.Current.Response.Flush();
HttpContext.Current.Response.Close();
}
}
I think you should consider using OpenXML (or more specfically ClosedXML) to build a modern Excel file with the xlsx extension.
Open XML SDK: https://learn.microsoft.com/en-us/office/open-xml/open-xml-sdk
Closed XML (nuget): https://www.nuget.org/packages/ClosedXML/
Code Samples: https://github.com/ClosedXML/ClosedXML/wiki/Deliver-an-Excel-file-in-ASP.NET

Could not Delete image from directory using C#

I am trying to delete an image from directory using following statement
System.IO.File.Delete(Path);
i have previously accessed this image in another function
Image image=Image.FromFile(imagePath)
imageList1.Images.Add(image);
but it shows an error that file is locked by another process. I searched for this and find out that I should use using to dispose of the image object so i tried this
using(Image image=Image.FromFile(imagePath))
imageList1.Images.Add(image);
but this gives me an error in program.cs that paramter is not valid and programs doesnot run.
and sometime it gives the error
Starting a second message loop on a single thread is not a valid operation. Use Form.ShowDialog instead.
I am adding the imagelist to a list view and before deleting the file i am clearing all the items of both imagelist and listview with following commands.
imageList1.Images.RemoveByKey(imageName);
imageList1.Images.Clear();
listView1.Items.RemoveByKey(imageName);
listView1.Items.Clear();
listView1.SmallImageList = null;
this is my code where i am populating listview.
try
{
string album = albumListBox.SelectedItem.ToString();
List<string> imageName=new List<string>();
XmlDocument doc = new XmlDocument();
doc.Load(Application.StartupPath + "/image.xml");
XmlNodeList list = null;
list = doc.SelectNodes(string.Format("/images/image"));
listView1.Columns.Clear();
listView1.Columns.Add("Image List",210,HorizontalAlignment.Left);
listView1.SmallImageList = imageList1;
listView1.Items.Clear();
imageList1.Images.Clear();
listView1.MultiSelect = false;
foreach (XmlNode node in list)
{
if (node.ChildNodes[2].InnerText == album)
{
string imagePath = Application.StartupPath + "\\images\\" + album + "\\" + node.ChildNodes[0].InnerText;
Image image = Image.FromFile(imagePath);
imageList1.Images.Add(image);
imageName.Add(node.ChildNodes[0].InnerText);
}
}
for (int j = 0; j < this.imageList1.Images.Count; j++)
{
listView1.Items.Add(imageName[j],imageName[j],j);
listView1.View = View.Details;
}
}
catch (Exception e)
{
MessageBox.Show("No Images in this Album Please add the Images");
showImage.Hide();
}
and this is the code where i want to delete the file
var listItem = listView1.SelectedItems[0];
string album = albumListBox.SelectedItem.ToString();
string imageName = listItem.Text;
string imagePath = Application.StartupPath + "\\images\\" + album + "\\" + imageName;
XmlDocument doc = new XmlDocument();
doc.Load(Application.StartupPath + "/image.xml");
XmlNode node = doc.SelectSingleNode(string.Format("//image[./imageName/text()='" + imageName + "' and ./album/text()='" + album + "']"));
if (node != null)
{
//remove image from directory tooV
doc.DocumentElement.RemoveChild(node);
doc.Save(Application.StartupPath + "/image.xml");
imageList1.Images.RemoveByKey(imageName);
imageList1.Images.Clear();
listView1.Items.RemoveByKey(imageName);
listView1.Items.Clear();
listView1.Items[listView1.SelectedIndices[0]].Remove();
imageList1.Images.RemoveAt(listView1.SelectedIndices[0]);
listView1.SmallImageList = null;
fillImageList();
System.IO.File.Delete(imagePath);
MessageBox.Show("Image deleted");
fillAlbumList();
}
So far as the image is still referenced in imagelist1, it is not disposed. In order to properly dispose it, you will have to dispose the imagelist1 also. Then only all the reference to Image are closed.

When adding a single letter at a time to a text field, how do I avoid the word wrap occuring mid-word?

Given the following example SWF:
Sample
Notice how with the words "enthusiast" at the end of the first line and "write" at the end of the second line, that they start to type out on the first line but after a few letters they are bumped.
I understand this is the correct behavior, but is there a way for "enthusiast" to begin being typed on the second line, and "write" on the third line instead of being bumped during the typing?
Currently I am thinking of doing a search ahead mechanism, so it finds the next word in whole, then makes that the active word to print, temporarily print it, see if it increases the numlines, and if it does insert a line break and continue writing. But it seems fiddly.
Code below:
import flash.text.TextField;
import flash.events.Event;
var tt:TextField = new TextField();
tt.wordWrap = true;
tt.width = 200;
tt.height = 50;
tt.border = true;
var s = "Stack Overflow is for professional and enthusiast programmers, people who write code because they love it. We feel the best Stack Overflow questions have a bit of source code in them, but if your question generally covers";
addChild(tt);
var currentLetter:int = 0;
addEventListener(Event.ENTER_FRAME, onEnter, false, 0, true);
function onEnter(e:Event):void
{
if(currentLetter < s.length)
{
tt.appendText(s.charAt(currentLetter));
}
currentLetter++;
}
adjusted your code a bit and posted the result on wonderfl: http://wonderfl.net/c/rZkm
as #mouseas suggested i put the next word into an additional textfield measure the width and compare this to the remaining space in the current line ... if the next word doesn't fit I add a line-break and continue.
here's the code:
package {
import flash.geom.Rectangle;
import flash.text.TextFieldAutoSize;
import flash.display.Sprite;
import flash.text.TextField;
import flash.events.Event;
public class FlashTest extends Sprite {
private var currentLetter:int = 0;
private var tt:TextField;
private var debug:TextField;
private var pre:TextField;
private var s:String;
public function FlashTest()
{
// write as3 code here..
tt = new TextField();
tt.wordWrap = true;
tt.width = 200;
tt.height = 150;
tt.border = true;
s = "Stack Overflow is for professional and enthusiast programmers, people who write code because they love it. We feel the best Stack Overflow questions have a bit of source code in them, but if your question generally covers";
addChild(tt);
// predraw word
pre = new TextField();
pre.y = 150;
pre.width = 200;
pre.height = 50;
pre.autoSize = TextFieldAutoSize.LEFT;
pre.border = true;
addChild(pre);
// debug txt
debug = new TextField();
debug.x = 250;
debug.wordWrap = true;
debug.width = 200;
debug.height = 500;
debug.border = true;
addChild(debug);
addEventListener(Event.ENTER_FRAME, onEnter, false, 0, true);
}
private function onEnter(e:Event):void
{
//debug.appendText("char: " + rect.x + " " + rect.width + "\n");
var c:String = "";
if (currentLetter < s.length)
{
c = s.charAt(currentLetter);
tt.appendText(c);
}
else
{
debug.appendText("DONE! \n");
removeEventListener(Event.ENTER_FRAME, onEnter);
}
if (c == " ")
{
var rect:Rectangle = tt.getCharBoundaries(currentLetter-1);
if (rect != null)
{
//debug.appendText("char: " + rect + "\n");
var cPos:int = rect.x + rect.width;
var r:int = tt.width - 4 - cPos; // 4px for gutter on left+right side of textfield
var start:int = s.lastIndexOf(" ", currentLetter);
var end:int = s.indexOf(" ", currentLetter+1);
if (start < 0) start = 0;
pre.text = s.substr(start, end-start);
debug.appendText("rest: " + r + " " + pre.textWidth + " > " + pre.text + "\n");
if (r - pre.textWidth <= 0)
{
tt.appendText("\n");
debug.appendText("\n");
}
//debug.appendText("w:" + tt.textWidth + " " + start + "->"+ end + " /" + pre.text + "/ " + pre.textWidth + "\n");
}
}
++currentLetter;
}
}
}
works great - only the "a" in the 4th line makes trouble - maybe you need to finetune the calculations a bit...

Resources