Trouble getting JOptionPane to open up an image from a website - java

I want to get JOptionPane to open up images from a link, because I have an assignment due next week where I have to include images in JOptionPane, and while it works with local files I'm not sure if that'll show when I turn it in. Anyways, what I have is:
ImageIcon thing = new ImageIcon("http://i.imgur.com/OGxr68g.jpg");
String[] finalOutputChoices = {"Help", "Please"};
int finalOutput = JOptionPane.showOptionDialog(
null //
, "Message" //
, "Final Output" //
, JOptionPane.YES_NO_OPTION //
, JOptionPane.PLAIN_MESSAGE //
, thing //
, finalOutputChoices //
, "Awesome" //
);
If I replace the link with the path to an image on my desktop it works, but it doesn't with the link.
Any help would be appreciated :)

Works with a URL object.
ImageIcon thing = new ImageIcon(new URL("http://i.imgur.com/OGxr68g.jpg"));
PS: weird image ;-)

Related

Change image object on template using Brother print Android SDK

I have set up label printing from our app using the b-PAC Android SDK (Java). Using the code below I can replace the text from my template with what I want.
// Start creating P-touch Template command print data
Boolean val= myPrinter.startPTTPrint(6, null);
Log.i("print", "startPTTPrint "+val);
// Replace text
myPrinter.replaceText("abcde");
// Trasmit P-touch Template command print data
PrinterStatus status=myPrinter.flushPTTPrint();
I am now trying to replace an image object within the template. I know it can be done in VBScript using:
bpac.Object ob = doc.GetObject("Photo");
ob.SetData(0, #"C:\Photo\635466380534236711.png", 4);
I can't find any Java examples of this within the b-PAC 3.1 SDK help guide and I have only just began coding in Java so I am very much a novice.
Does anyone have experience with the Brother SDK/Java who can point me in the right direction?
Thanks!
I got this working
Basically I have a template that I copy to an new file on which I make the changes
The image object in the template is called imgPart, the other objects are textblocks
Dim m_partNum As String = "12345"
Dim m_PartName As String = "Plate Special 1 x 2" & vbCrLf & "4 Studs"
Dim m_PartImage As String = "c:\Danny\myLego\Labels\PartImages\3033.png"
Dim m_template As String = "c:\Danny\myLego\Templates\PRINTME.lbx"
Dim m_target As String = "c:\Danny\myLego\Templates\" & m_partNum & ".lbx"
Dim doc As bpac.DocumentClass = New bpac.DocumentClass
Try
File.Copy(m_template, m_target, vbTrue)
If doc.Open(m_target) <> False Then
doc.GetObject("imgPart").SetData(0, m_PartImage, 4)
doc.GetObject("txtPartName").Text = m_PartName
doc.GetObject("txtPartNum").Text = m_partNum
doc.Save()
doc.Close()
Else
MsgBox("Open Error on Receipt with error")
End If
Catch ex As Exception
MsgBox("Error occurred : " & ex.ToString)
End Try

How to add PPT notes using docx4j

I am creating PPT files using the library docx4j. I have been able to create slides with text and images, but I have not been able to add notes to them.
I am creating the slide like this:
MainPresentationPart pp = (MainPresentationPart)presentationParts.get(new PartName("/ppt/presentation.xml"));
SlideLayoutPart layoutPart = (SlideLayoutPart)presentationParts.get(new PartName("/ppt/slideLayouts/slideLayout1.xml"));
SlidePart slidePart = PresentationMLPackage.createSlidePart(pp, layoutPart, new PartName("/ppt/slides/slide" + ++slideNumber + ".xml"));
so I can add text or images to the body, but when I try to access the field slidePart.notes it is null. I have tried to initialize it
slidePart.setPartShortcut(new NotesSlidePart());
but then everything inside notes is null and I have not achieved anything.
So, does anyone have a working example of how to add notes to a PPT file?
Many thanks
Its not enough to do:
slidePart.setPartShortcut(new NotesSlidePart());
You need to explicitly add the notes slide part to your slide part (so that the relationships get set up correctly), by invoking addTargetPart.
But there's more you have to do given the way the pptx format works. To see what parts are required, upload a pptx to the docx4j webapp. Here's the code I wrote just now based on doing that:
// Now add notes slide.
// 1. Notes master
NotesMasterPart nmp = new NotesMasterPart();
NotesMaster notesmaster = (NotesMaster)XmlUtils.unmarshalString(notesMasterXml, Context.jcPML);
nmp.setJaxbElement(notesmaster);
// .. connect it to /ppt/presentation.xml
Relationship ppRelNmp = pp.addTargetPart(nmp);
/*
* <p:notesMasterIdLst>
<p:notesMasterId r:id="rId3"/>
</p:notesMasterIdLst>
*/
pp.getJaxbElement().setNotesMasterIdLst(createNotesMasterIdListPlusEntry(ppRelNmp.getId()));
// .. NotesMasterPart typically has a rel to a theme
// .. can we get away without it?
// Nope .. read this in from a file
ThemePart themePart = new ThemePart(new PartName("/ppt/theme/theme2.xml"));
// TODO: read it from a string instead
themePart.unmarshal(
FileUtils.openInputStream(new File(System.getProperty("user.dir") + "/theme2.xml"))
);
nmp.addTargetPart(themePart);
// 2. Notes slide
NotesSlidePart nsp = new NotesSlidePart();
Notes notes = (Notes)XmlUtils.unmarshalString(notesXML, Context.jcPML);
nsp.setJaxbElement(notes);
// .. connect it to the slide
slidePart.addTargetPart(nsp);
// .. it also has a rel to the slide
nsp.addTargetPart(slidePart);
// .. and the slide master
nsp.addTargetPart(nmp);
You can find the complete example at https://github.com/plutext/docx4j/blob/master/src/samples/pptx4j/org/pptx4j/samples/SlideNotes.java

WebDriver is unable to perform click on an input element having onclick=function1() as an attribute

I am using Web driver 2.31 with Java. It seems the web driver is unable to perform click action on an input element having onclick() attribute.
The input element I need to perform click action on is having the following attributes - id (which is a random generated number), class, type=button, onclick, onmouseout, onmouseover, title and value.
I'm able to fetch the values of title and value attributes which means that the web driver is able to recognize the input element but is not able to perform click action on it.
I have tried with the following:
webdriver.findElement(By.xpath("xpath for the input")).click()
webdriver.findElement(By.xpath("xpath for the input")).sendKeys(Keys.ENTER);
new Actions(webdriver).moveToElement(webdriver.findElement(By.xpath("xpath for the input"))).click().perform();
None of the above options are working.
Do you get any exceptions from element.click()? It it enabled and visible? One of the problems we had was that WebDriver didn't handle position:static elements correctly, so during playback it would cover the button (and you won't see it on screenshot) and it would throw exception "Element is not clickable at point".
We had similar problem with element and had following code that did work sometimes (but also not 100% of times):
element.click();
if("button".equals(tagName)) {
if(element.isEnabled() && element.isDisplayed())
element.sendKeys(Keys.ENTER);
}
But the problem disappeared itself after upgrading WebDriver and we removed sendKeys(ENTER), also it was working fine in 2.29.0.
I faced exactly same problem in my project. The issue was not to locate the element but the onClick() event was not firing.
Then i found out that something else was there which stopped from the event to fire. I had used java script to enable the date picker box & did this,
((JavascriptExecutor)driver).executeScript ("document.getElementById('txtOriginDate').removeAttribute('readonly',0);");
WebElement originDateBox= driver.findElement(By.xpath(prop.getProperty("originDateBox")));
originDateBox.clear();
originDateBox.sendKeys("9-Dec-2014"); //Enter date
Developer designed this in such a way that if you don't use date picker to select date, a specific variable was not set. Which eventually made the **onclick event not to fire.
The date picker code was something like this,
var jsoncustdate = "";
var jsonorigindate = "";
function onSelectCalender( StrDt, obj )
{
if ( !varReadonly ) {
if ( $( "#txtCustDescisionDate" ).attr( "IsDisable" ) == "FALSE" )
{
if ( obj.id == "txtCustDescisionDate" )
{
custobjDt = new Date( obj.selectedYear, obj.selectedMonth,obj.selectedDay, 0, 0, 0, 0 );
jsoncustdate = custobjDt.getTime();
jsoncustdate = "\/Date(" + jsoncustdate + ")\/";
DisabledBtnStage();
// $("#txtFromDate").datepicker("option", "maxDate", objDt);
}
if ( obj.id == "txtOriginDate" )
{
var objDt = new Date( obj.selectedYear, obj.selectedMonth,obj.selectedDay,0, 0,0,0 );
jsonorigindate = objDt.getTime();
jsonorigindate = "\/Date(" + jsonorigindate + ")\/";
DisabledBtnStage();
// $("#txtToDate").datepicker("option", "minDate", objDt);
}
}
elogCommon.CheckMandatory();
}
}
I finally used date picker in normal way & the event fired smoothly.
I hope this answer will help . .cheers !!!

Having trouble printing a JTable

For my french verb conjugation program I trying to include an option to print a conjugation (in table form)
However the problem is that it prints a blank box, I have read some places that this is because the table is not been visible in the GUI or something like that. The problem is I want the table to be printed without adding it to the GUI at all...
the code for the table:
JTable getPrint(String Infinitive)
{
String [] aPresent = fetchTense(Tense.PRESENT, getID(Infinitive)).split("\n");
String [] aPerfect = fetchTense(Tense.PERFECT, getID(Infinitive)).split("\n");
String [] aImperfect = fetchTense(Tense.IMPERFECT, getID(Infinitive)).split("\n");
String [] aSimpleFuture = fetchTense(Tense.SIMPLE, getID(Infinitive)).split("\n");
String [] aConditional = fetchTense(Tense.CONDITIONAL, getID(Infinitive)).split("\n");
String [] aColumnNames = {"Pronoun", "Present" , "Perfect" , "Imperfect" , "Simple Future" , "Conditional" };
String [][] aValues = {
{"Je" , aPresent[0], aPerfect[0], aImperfect[0], aSimpleFuture[0], aConditional[0]},
{"Tu" , aPresent[1], aPerfect[1], aImperfect[1], aSimpleFuture[1], aConditional[1]},
{"Il" , aPresent[2], aPerfect[2], aImperfect[2], aSimpleFuture[2], aConditional[2]},
{"Elle" , aPresent[3], aPerfect[3], aImperfect[3], aSimpleFuture[3], aConditional[3]},
{"On" , aPresent[4], aPerfect[4], aImperfect[4], aSimpleFuture[4], aConditional[4]},
{"Nous" , aPresent[5], aPerfect[5], aImperfect[5], aSimpleFuture[5], aConditional[5]},
{"Vous" , aPresent[6], aPerfect[6], aImperfect[6], aSimpleFuture[6], aConditional[6]},
{"Ils" , aPresent[7], aPerfect[7], aImperfect[7], aSimpleFuture[7], aConditional[7]},
{"Elles" , aPresent[8], aPerfect[8], aImperfect[8], aSimpleFuture[8], aConditional[8]}
};
JTable pTable = new JTable(aValues, aColumnNames);
return pTable;
}
and I want to print it with the following code:
try
{
JTable pTable = pGUI.getParser().getPrint("Aller");
JFrame fix = new JFrame();
fix.add(pTable);
fix.setVisible(true);
fix.setVisible(false);
boolean bComplete = pTable.print(JTable.PrintMode.FIT_WIDTH, new MessageFormat(String.format("Conjugation of %s", "Aller")), new MessageFormat("Page {0}"));
if (bComplete)
{
JOptionPane.showMessageDialog(pGUI, "Finished printing", "Printed", JOptionPane.INFORMATION_MESSAGE);
}
else
{
JOptionPane.showMessageDialog(pGUI, "Printing Cancelled", "Printing Cancelled", JOptionPane.WARNING_MESSAGE);
}
}
catch (PrinterException e)
{
JOptionPane.showMessageDialog(pGUI, "An error has occured", "Printing Error", JOptionPane.ERROR_MESSAGE);
}
finally
{
pGUI.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
}
Does anybody have any idea what is going wrong here and how can I fix it?
Also as a side note, as this is a blank box I can't be sure but when it prints if a word is too long to fit in the cell will it get shorted to word... for example. And how can this be fixed?
For better help sooner, post an SSCCE, because very similair code your posted prints me correct output (to the File or paper), nobody know whats happened in important rest of your code
possible solution___________________________________________________________
without merging the Arrays together, create 2D array and put that as JTable(Object[][] rowData, Object[] columnNames) or JTable(String[][] rowData, String[] columnNames), doesn't matter for testing, sure Object[][] is prepared for various data types (Double, Date, e.i. not only the String) in JTable
all updates must be done on EventDispatchThread
building for a new TopLevelContainer must be done on InitialThread
for both above mentioned points there is about wrapping in invokeLater
see JTables tutorial Printing, try working code example ( TablePrintDemo.java )
JTables tutorial ended with link to the lesson Printing

split method for text value in Appcelerator

I have a window that displays my tweets in a label.
My tweets come from my FB page statuses and if i have put a pic or write more than 140 characters then i get a link in tweet to the actuall post.
I wonder if there is any way to get the label text to split so i can point the link into an url to open in webview
This is how far i have got:
var win = Ti.UI.currentWindow;
win.showNavBar();
var desc = Ti.UI.createLabel({
text: win.data,
font:{
fontSize:'20dp',
fontWeight:'bold'
},
height:'300dp',
left:'5dp',
top:'10dp',
color:'#111',
touchEnabled:true
});
win.add(desc);
desc.addEventListener('click',function(e){
var v = desc.text;
if(v.indexOf('http') != -1){
// open new window with webview
var tubeWindow = Ti.UI.createWindow({
modal: true,
barColor: '#050505',
backgroundColor: '#050505'
});
var linkview = Ti.UI.createWebView({
url: e.v,
barColor: '#050505',
backgroundColor: '#050505'
});
// Create a button to close the modal window
var close_modal = Titanium.UI.createButton({title:'Stäng'});
tubeWindow.rightNavButton = close_modal;
// Handle close_modal event
close_modal.addEventListener('click', function() {
tubeWindow.close();
});
tubeWindow.add(linkview);
tubeWindow.open({
modalTransitionStyle: Ti.UI.iPhone.MODAL_TRANSITION_STYLE_FLIP_HORIZONTAL,
});
}
});
win.open();
What i´ve been told i need to split the win.data to get the link. (win.data is the tweet)
now i just have: url: e.v, i need to get the link out
Any ideas on how this can work?
Thanx
//R
I did a similar thing a while ago. pull down the tweet(s) run the text through a regular expression to pull out a URL.
What I did was put each tweet in a tableview row, and set the tableview row to hasChild=true if the regular expression returned anything, then onClick of a tableView row, if hasChild == true open a webview with the given URL (stored in the row).
A regualr expression like the one here should work:
http://www.geekzilla.co.uk/view2D3B0109-C1B2-4B4E-BFFD-E8088CBC85FD.htm
So something like:
str= <<tweet text>>;
re= <<URL expression>>;
check=str.match(re);
now check contains either null or a url.

Categories

Resources