how to display a video from a JList? - swing

I am currently doing an application using Swing and i am stuck at a certain point. In my function, i have to link videos from a JList. The problem is i am not sure how to link the videos from the JList. I am using an OpenBrowser class to link the video to the internet. I did consider using JButton but i would have to hardcode it and that would be ugly. Is there any other alternatives to do this? I am in desperate need and would be eternally grateful to whoever that can help me.
Safa :)

If you don't want to open a browser with the video using a selection listener, you can consider the idea of launching it with a double click on a JList entry.
sample code
String[] items = {"i1", "i2", "i3", "i4"};
JList list = new JList(items);
list.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent evt) {
JList list = (JList)evt.getSource();
if (evt.getClickCount() == 2) { //check if it is a Double-click
int index = list.locationToIndex(evt.getPoint());
// do whatever you want with the entry at that index
}
}
});
Desktp classes to browse some site (sample code):
if (desktop.isSupported(Desktop.Action.BROWSE)) {
URI uri = new URI("http://www.google.com");
desktop.browse(uri);
}
The desktop.browse() call will open your favourite browser with the given URL.

Related

Sound button to make sound ON or OFF does not changing button image

I have a sound button in my menu screen.It has two images.one is normal and other is checked(indicating sound is OFF).
I am using a boolean variable to get the state of this button.If it is true,sound will play else sound will be OFF.I am using it to control in-game sounds.
public boolean soundBool=true;
created soundButton like this:Here soundTexture1 is normal image(ON) and soundTexture2 has a cross mark(OFF).
private void drawSoundButton() {
soundButton = new ImageButton(new TextureRegionDrawable(soundTexture1),
new TextureRegionDrawable(soundTexture2), new TextureRegionDrawable(soundTexture2));
stage.addActor(soundButton);
soundButton.setPosition(UiConstants.SOUND_X, UiConstants.SOUND_Y, Align.bottom);
soundButton.addListener(new ChangeListener() {
#Override
public void changed(ChangeEvent event, Actor actor) {
if (!game.buttonClickSoundBool)
buttonClickSound.play();
soundButton.scaleBy(0.025f);
if(game.soundBool)
game.soundBool=false;
else
game.soundBool=true;
}
});
}
I am calling all sounds with play() only when this soundBool is true.
Now when I click this button,crossmark will come,sound will be off and works fine in terms of sound.
Whenever I come back to menu,the image with cross mark should be displayed because the sound is OFF.But it does not shows the cross mark image.It shows the first image.But functionality is fine.If I click,sound will go ON.
It would be helpful if someone explains how to solve this.
Use setChecked(boolean status); on soundButton according to the status of soundBool.
private void drawSoundButton() {
soundButton = new ImageButton(new TextureRegionDrawable(soundTexture1),new TextureRegionDrawable(soundTexture2), new TextureRegionDrawable(soundTexture2));
soundButton.setChecked(!soundBool); // setChecked according to boolean status
stage.addActor(soundButton);
soundButton.setPosition( UiConstants.SOUND_X,UiConstants.SOUND_Y,Align.bottom);
...
}
Also don't forget to save status of soundBool in preference so that you can restore that value, when you come back in your game.

Vaadin Spring Navbar

I am trying to add a navbar (just like bootstrap's) for a Spring application but am not getting the nav menu to appear on the web page!
Can anyone please tell me what's wrong here?
Below is my code:
private Panel viewContainer;
private HorizontalLayout navbar;
private Button btnHome;
private Button btnNested;
private Button createNavigationButton(String caption, final String viewName) {
Button button = new Button(caption);
button.addStyleName(ValoTheme.BUTTON_SMALL);
// If you didn't choose Java 8 when creating the project, convert this
// to an anonymous listener class
button.addClickListener(event -> getUI().getNavigator().navigateTo(
viewName));
return button;
}
#Override
protected void init(VaadinRequest request) {
final VerticalLayout root = new VerticalLayout();
root.setSizeFull();
navbar = new HorizontalLayout();
navbar.setWidth("100%");
navbar.setDefaultComponentAlignment(Alignment.MIDDLE_RIGHT);
root.addComponent(navbar);
final Label brand = new Label("Nested demo");
brand.addStyleName(ValoTheme.LABEL_H1);
brand.addStyleName(ValoTheme.LABEL_NO_MARGIN);
navbar.addComponent(brand);
navbar.setComponentAlignment(brand, Alignment.MIDDLE_LEFT);
navbar.setExpandRatio(brand, 1);
btnHome = new Button("Home", FontAwesome.HOME);
btnHome.addStyleName(ValoTheme.BUTTON_BORDERLESS);
navbar.addComponent(btnHome);
btnNested = new Button("nested", FontAwesome.COFFEE);
btnNested.addStyleName(ValoTheme.BUTTON_BORDERLESS);
navbar.addComponent(btnNested);
viewContainer = new Panel();
viewContainer.setSizeFull();
root.addComponent(viewContainer);
root.setExpandRatio(viewContainer, 1);
}
Any hint is much appreciated.
Thanks
Henri's comment is almost certainly the correct answer.
Judging by the init(VaadinRequest) code you're using a UI class.
Without setContent(some components with visible stuff in them);
you won't see anything.
'you won't see anything' happens a lot in Vaadin when you're attempting new things, or doing proof of concept stuff. IMHO it's always a good practice to start with really dumb UI stuff e.g. setContent(new Label("TODO - implement this content-xxx"));
Using browser dev-tools is also a great idea. A quick select-element should show you that the UI div is empty, and allow you to start diagnosing.
TL;DR :-
UI is a ComponentContainer, so you need to - either
setContent(myLayoutWithStuff);
or
getContent().addComponent(myStuff);
to show someStuff.

Want to create an automation that switches between two tabs

I am new to automation and want to create an automation test which can do following:
Open one tab --- click and get some info from that tab
Switch to another tab --- click and get some info from this tab now.
Compare the infos.
We use Page Object Model to get info from one page. However the moment, I switch to another tab -- it switches the tab successfully but does not locate any element on it.
Any idea ?
Questions I would ask is,
Is the element locator correct?
Is this a unique element locator?
Is this a synchronization issue? Are you waiting enough for the page to load before finding the element?
Is this problem particular to a browser? Is it consistent across?
Also make sure you pass on the driver object from one page object to the other. Like,
public class PageOne {
public PageOne(WebDriver driver) {
//do something in constructor
}
public void someMethodInPage1() {
driver.findElement(By.id("button1")).click();
PageTwo pageTwo = new PageTwo(driver);
pageTwo.someMethodInPage2();
}
}
public class PageTwo {
private WebDriver driver;
public PageTwo(WebDriver driver) {
//do something in constructor
this.driver = driver;
}
public void someMethodInPage2() {
driver.findElement(By.id("button2")).click();
}
}

How to set multiple links in RichEditBox without AccessViolationException

Essentially my problem is that I receive AccessViolationException when I try to programmatically apply more than 2 links to an ITextDocument, if a user has edited the content. I've put together a simple demo app, based on the windows phone (8.1) Blank App template.
I add to the main page:
<StackPanel Margin="19,0,0,0">
<Button
Content="Apply Links"
Click="Button_Click"
/>
<RichEditBox
x:Name="RtfBox"
Height="300"
Loaded="RtfBox_Loaded"
Margin="0,0,19,0"
TextWrapping="Wrap"
/>
</StackPanel>
And to the code behind for the same page I add (using statements not included):
private void RtfBox_Loaded(object sender, RoutedEventArgs e)
{
//RtfBox.Document.SetText(TextSetOptions.None, "Links to demo, example, test. More links to demo, demo, example, test and test.");
}
private void Button_Click(object sender, RoutedEventArgs e)
{
var pages = new Dictionary<Guid, string> { { Guid.NewGuid(), "demo" }, { Guid.NewGuid(), "example" }, { Guid.NewGuid(), "test" } };
// NOTE: Avoid performance implications of many small updates
RtfBox.Document.BatchDisplayUpdates();
ITextRange range;
foreach (var page in pages)
{
var link = string.Format("\"richtea.demo://pages/{0}\"", page.Key);
var skip = 0;
while ((range = RtfBox.Document.GetRange(skip, TextConstants.MaxUnitCount)).FindText(page.Value, TextConstants.MaxUnitCount, FindOptions.None) != 0)
{
if (range.Link == "")
{
// TODO: Stop this throw exceptions
System.Diagnostics.Debug.WriteLine("Setting text at position {0} to link: '{1}'.", range.StartPosition, link);
range.Link = link;
}
skip = range.EndPosition;
}
}
RtfBox.Document.ApplyDisplayUpdates();
}
If you start this up and type something like "A link to the demo page" and click the button, it becomes a link correctly. You can keep putting the same text and clicking the button and it continues to work.
However if you put in three or more (for some reason for me it's always 3 or more) of the words demo, example or test (my keywords) and hit the button, it errors on an AccessViolationException on setting range.Link = link. It's worth noting if you check while debugging, the range.Link property has actually been set.
More interestingly, if you uncomment RtfBox_Loaded's contents, and run the app and click the button right away, it handles it fine. So it seems to relate to the selection having been set on the RichEditBox? I've tried disabling the control before applying the links, but that's not helped me.
Some other things which have made it harder for me to diagnose the issue here include:
It seems to work more often if I'm debugging line by line, so might be timing related too
I can't use the ITextDocument not on the UI thread it seems (the COM object fails to cast) so while it seems async might be a better approach, I haven't succeeded at it here.
Also for the record, the reason I'm attempting to do all the updates on mass, rather than as the user types them is that I don't want to deal with the cleanup when notes are renamed or deleted, and I don't really want those links in at edit time or saved, but I could live with the later.
This solution was posted on the MSDN forums by Eric Fleck and worked for me:
RtfBox.Document.Selection.StartPosition = RtfBox.Document.Selection.EndPosition = range.StartPosition;
range.Link = link;
RtfBox.Document.Selection.StartPosition = RtfBox.Document.Selection.EndPosition = range.EndPosition;
It seemed that it's important to do it around each link being set, because unless I'm very much mistaken I tried this before updating all my links and it didn't help.
I'm not yet using the ability to restore the selection to it's original location, but I might want to in the future, so I made this little utility class. Also so that I can wrap places like these in a using() block for a bit of syntactic sugar.
public static class ITextDocumentExtensions
{
public static IDisposable SuppressSelection(this ITextDocument document)
{
var start = document.Selection.StartPosition;
var end = document.Selection.EndPosition;
var disposable = new ActionDisposable(() => document.Selection.SetRange(start, end));
document.Selection.SetRange(0, 0);
return disposable;
}
private sealed class ActionDisposable : IDisposable
{
private readonly Action dispose;
public ActionDisposable(Action dispose)
{
this.dispose = dispose;
}
public void Dispose()
{
dispose();
}
}
}
Which allows me to write
using (RtfBox.Document.SuppressSelection())
{
range.Link = link;
}

Windows Phone: How to Navigate another page which is using DispatcherTimer?

I have two pages Main.xaml which includes only a button and another page timer.xaml which includes a timer. After pressing the button in the main page I want to go to the another page and start timer. I am using following code:
enter code here
**Main Page:**
private void Start_Click(object sender, RoutedEventArgs e)
{
NavigationService.Navigate(new Uri("/timer.xaml", UriKind.Relative));
}
**timer Page:**
public Page1()
{
InitializeComponent();
dispatcherTimer.Interval = new TimeSpan(0, 0, 1, 0, 0);
dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);
dispatcherTimer.Start();
counter=0;
count.Text = counter.ToString();
}
private void dispatcherTimer_Tick(object sender, EventArgs e)
{
counter++;
count.Text = counter.ToString();
}
I can not see time in the timer page however, after pressing the button it will navigate to the timer page correctly but I can not see updates in my timer page. I am debugging my code and it seems that DistpacherTimer() works correctly but my timer page does not update. Do you know how can I fix this problem?
Finally, I could find the problem. the code that you can see above was not whole code that I have written. Actually, in my real code I was sending a list <> from main page to timer page. Passing data from one page to another page is a little tricky. My main problem was that I did not send the data from main page to timer page in correct way, so that caused problem. Thus, there was nothing wrong with my DispatcherTimer() class or Event handler.
In other words, the code above works well if you do not pass data (e.g. list, array and so on) from a page to another page in a wrong way.