Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Tiny issue] swing2.rb example at https://github.com/jruby/jruby/blob/master/samples/swing2.rb has a warning, "swing.rb:4: warning: import is deprecated; use java_import" #7380

Closed
rubyFeedback opened this issue Sep 24, 2022 · 3 comments
Milestone

Comments

@rubyFeedback
Copy link

rubyFeedback commented Sep 24, 2022

The file at:

https://github.com/jruby/jruby/blob/master/samples/swing2.rb

I believe line 4 should be changed from:

import javax.swing.JFrame

to:

java_import javax.swing.JFrame

Otherwise a (harmless) warning is shown right now.

On a side note, I am currently trying to create and use an entry for
user input via jruby + swing (the JTextField) and find out how to
respond to "if user hit the enter key in the entry, call a method".

The swing2.rb kind of has a way to show how this is done via:

button.add_action_listener do |evt|
  javax.swing.JOptionPane.showMessageDialog(nil, <<EOS)
<html>Hello from <b><u>JRuby</u></b>.<br>
Button '#{evt.getActionCommand()}' clicked.
EOS

But I don't know whether this will work for JTextField. Will try
to find out.

If someone feels like wanting to do extra work, it would be nice
if swing2.rb could be renamed to swing.rb or swing_example.rb
and extended with a few more widgets. Right now I am trying
to gather information from java, but the syntax is often not
easy to translate 1:1 for me for jruby+swing. In my own ruby-gtk3
code, for instance, I tend to simplify all of this via:

 button.on_clicked {
 }

Or:

 entry.on_enter_key_pressed {
 }

Not sure if jruby wants to add custom DSLs (since it is more work
I get it), but I find from a usability point of view this is so much easier
to work with. Via the add_action_listener I have to kind of simulate
the java swing API in ruby and this is not always trivial or convenient
IMO. What could help indirectly would be more examples in jruby
but these are quite hard to find. I find many more examples in java
(unsurprisingly so); for instance https://java-swing-tips.blogspot.com/
it is a really good website with TONS of code + screenshots, but it
is quite hard to translate this all back into ruby. Perhaps rubocop will
auto-correct other languages into java too one day ... :D Anyway, this
issue request is mostly about the typo, but if someone wants to improve
on swing2.rb and/or rename, that may also be nice - just as a demo
in one file. (I get that it was last modified some ~14 years ago or so...)

@rubyFeedback
Copy link
Author

rubyFeedback commented Sep 24, 2022

In the event someone else finds this, the swing2.rb code works fine - on enter
key pressed in the entry, the pop up described above showed up.

For anyone finding this here via google search, I will copy paste it (entry1 is
my instance of JTextField, I kind of use a DSL for GUI stuff):

entry1.add_action_listener { |evt|
    javax.swing.JOptionPane.showMessageDialog(nil, <<EOF)
  <html>Hello from <b><u>JRuby</u></b>.<br>
  Enter key was pressed.
EOF
}

@headius
Copy link
Member

headius commented Sep 27, 2022

That small fix would be a good first PR for you. Could you submit it?

As for the rest, perhaps you can move it to a Discussion? We and other users would be happy to help!

@headius headius added this to the JRuby 9.3.9.0 milestone Sep 27, 2022
@enebo
Copy link
Member

enebo commented Sep 27, 2022

@rubyFeedback Your description of making a friendlier face on top of Swing sounds like a great idea but it also sounds more like a gem than a core feature of JRuby. We did this for JavaFX (JRubyFx) and other than us adding some advanced features for interacting with Java classes it works fine as it's own gem. Actually, probably better since it can release on its own schedule.

As @headius said though....if you open this up in a discussion perhaps you will find some kindred spirits to hash out how to make this API?

headius added a commit that referenced this issue Oct 13, 2022
@headius headius closed this as completed Oct 13, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants