Created
August 3, 2020 13:04
-
-
Save art-solopov/93ec594991d610bcfbac924f4b1611fd to your computer and use it in GitHub Desktop.
Tinkering with Java forms & JRuby logic
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# frozen_string_literal: true | |
require 'java' | |
java_import javax.swing.table.AbstractTableModel | |
class LProc | |
java_import Java::MeArtsolopovJrp::LogicProcessor | |
include LogicProcessor | |
class TableModel < AbstractTableModel | |
COLUMN_NAMES = { | |
q: 'Q', | |
w: 'Win', | |
x: 'Cross' | |
}.freeze | |
def initialize(data) | |
super() | |
@data = data | |
end | |
def getColumnName(col) | |
COLUMN_NAMES.values[col] | |
end | |
def getColumnCount | |
COLUMN_NAMES.count | |
end | |
def getRowCount | |
@data.count | |
end | |
def getValueAt(row, col) | |
col_key = COLUMN_NAMES.keys[col] | |
@data[row][col_key] || 0 | |
end | |
def isCellEditable(_r, _c) | |
true | |
end | |
def setValueAt(value, row, col) | |
col_key = COLUMN_NAMES.keys[col] | |
@data[row][col_key] = Integer(value) | |
end | |
end | |
def initialize(frame) | |
@frame = frame | |
@table = [{ q: 1, w: 2, x: 3 }, { q: 2, w: 4, x: 3 }] | |
@table_model = TableModel.new(@table) | |
end | |
attr_reader :table_model | |
def action_trig(inst, anno) | |
anno.text = <<~DOC | |
Inputted text: #{inst} | |
data: #{@table} | |
DOC | |
end | |
def action_close | |
@frame.dispose | |
end | |
def action_add_row | |
@table << {} | |
@table_model.fire_table_rows_inserted(@table.length - 1, @table.length - 1) | |
end | |
end | |
module Main | |
java_import javax.swing.JFrame | |
java_import javax.swing.UIManager | |
java_import java.awt.Font | |
include_package 'me.artsolopov.jrp' | |
def self.main | |
UIManager.look_and_feel = 'javax.swing.plaf.nimbus.NimbusLookAndFeel' | |
UIManager.look_and_feel_defaults.put("defaultFont", Font.new('Ubuntu', Font::PLAIN, 16)) | |
form = PlayForm.new | |
frame = JFrame.new('PlayForm') | |
frame.content_pane = form.container | |
frame.default_close_operation = JFrame::EXIT_ON_CLOSE | |
frame.pack | |
frame.set_location(300, 300) | |
lp = LProc.new(frame) | |
form.bind_to_processor(lp) | |
frame.visible = true | |
end | |
end | |
Main.main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package me.artsolopov.jrp; | |
import javax.swing.*; | |
import javax.swing.table.TableModel; | |
import javax.swing.text.JTextComponent; | |
public interface LogicProcessor { | |
void actionTrig(String inst, JTextComponent anno); | |
void actionClose(); | |
void actionAddRow(); | |
TableModel getTableModel(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package me.artsolopov.jrp; | |
import javax.swing.*; | |
/* Not pictured: the UI built with IntelliJ */ | |
public class PlayForm { | |
public JPanel getContainer() { | |
return container; | |
} | |
public JTextField getInst() { | |
return inst; | |
} | |
private JPanel container; | |
private JTextField inst; | |
private JTextArea anno; | |
private JButton trig; | |
private JButton quit; | |
private JTable tabul; | |
private JButton addRow; | |
public void bindToProcessor(LogicProcessor lp) { | |
trig.addActionListener(_ev -> lp.actionTrig(inst.getText(), anno)); | |
quit.addActionListener(_ev -> lp.actionClose()); | |
addRow.addActionListener(_ev -> lp.actionAddRow()); | |
tabul.setModel(lp.getTableModel()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment