Swing Examples in Processing Window #468
Replies: 2 comments 15 replies
-
So cool!!! Running fine on Manjaro Linux. I'll see if I can test this on Windows next week! PS: If you edit your post and add |
Beta Was this translation helpful? Give feedback.
-
While we're on this subject we should probably discuss using an EventDispatchThread for Swing components. Consensus seems to be that not all of these components are 'thread safe', yet in py5 we've not be using an event dispatch thread since there have not been any problems that I am aware of. However, I have been using an EDT in Processing Swing demos based on a recommendation from another forum member when I first posted source code a couple of years ago. Recently there was comment from still another list member of the forum about not being consistent when I posted a short Swing example for py5, ie using an EDT in Processing but not for py5. Mainly I didn't know the syntax for py5 so I avoided using it; as it turns out it's pretty simple. The Python syntax is: SwingUtilities.invokeLater(buildWnd) Whenever you see buildWnd() in earlier Swing demos substitute SwingUtilities.invokeLater(buildWnd) and that should add the EventDispatchThread. Below is a re-post of the inital post making this addition. # Uses Imported mode for py5
from javax.swing import *
from java.awt import *
from javax.swing.event import *
from java.io import *
_wndW = 600
_wndH = 600
# Init values
dotColor = Color.BLUE
radius = 50
def mItemOpenAction(event):
fileChooser = JFileChooser()
result = fileChooser.showOpenDialog(fileChooser)
if(result == JFileChooser.APPROVE_OPTION):
file_select = fileChooser.getSelectedFile()
file_path = file_select.getPath()
with open(str(file_path), 'r') as file:
data = file.read()
txtArea_1.setText(data)
file.close()
def sliderAction(event):
global radius
radius = slider.getValue()
def radioGrpAction(event):
if(radio1.isSelected()):
label.setText("Radio btn 'A' selected.")
if(radio2.isSelected()):
label.setText("Radio btn 'B' selected.")
if(radio3.isSelected()):
label.setText("Radio btn 'C' selected.")
def spinnerAction(event):
pbar.setValue(spinner.getValue())
def myListAction(event):
label.setText(myList.getSelectedValue() + " was selected.")
def checkBoxAction(event):
if(chkBox.isSelected()):
label.setText("checkBox checked.")
else:
label.setText("checkBox unchecked.")
def txtFldAction(event):
label.setText(txtFld.getText())
def pushBtnAction(event):
JOptionPane.showMessageDialog(frame,"You hit the 'Press me' btn.","Alert",JOptionPane.WARNING_MESSAGE)
def menuBar():
global mItemOpen
menuBar = JMenuBar()
menu = JMenu("File")
mItemOpen = JMenuItem("Open...")
mItemOpen.addActionListener(mItemOpenAction)
menu.add(mItemOpen)
menuBar.add(menu)
frame.setJMenuBar(menuBar)
menuBar.repaint()
def label():
global label
label = JLabel("Output displayed here.")
label.setHorizontalAlignment(JLabel.CENTER)
label.setBounds(30, 10, 150, 30)
frame.add(label)
label.repaint()
def colorBtnAction(event):
global dotColor
panel = JPanel()
dotColor = JColorChooser.showDialog(panel,"Choose color",Color.BLUE)
def textField():
global txtFld
txtFld = JTextField("Enter text here.")
txtFld.setBounds(30, 50, 180, 30)
txtFld.addActionListener(txtFldAction)
frame.add(txtFld)
txtFld.repaint()
def slider():
global slider
# orientation, min, max, value
slider = JSlider(JSlider.HORIZONTAL, 0, 100, 50)
slider.setBounds(185,20,150,24)
slider.addChangeListener(sliderAction)
frame.add(slider)
slider.repaint()
def colorBtn():
colorBtn = JButton("Color")
colorBtn.setBounds(350,20,80,24)
colorBtn.addActionListener(colorBtnAction)
frame.add(colorBtn)
colorBtn.repaint()
def pushBtn():
btn = JButton("Press me.")
btn.setBounds(30, 90, 100, 24)
btn.addActionListener(pushBtnAction)
frame.add(btn)
btn.repaint()
def checkBox():
global chkBox
chkBox = JCheckBox("OK",False)
chkBox.setBounds(140, 92, 60, 24)
chkBox.addItemListener(checkBoxAction)
frame.add(chkBox)
chkBox.repaint()
def spinner():
global spinner
spinner = JSpinner()
spinner.setBounds(220, 90, 50, 30)
spinner.getEditor().getTextField().setEditable(False)
# init,min,max,step
spinner.setModel(SpinnerNumberModel(5, 0, 100, 1))
spinner.addChangeListener(spinnerAction)
frame.add(spinner)
spinner.repaint()
def progressBar():
global pbar
pbar = JProgressBar()
pbar.setBounds(30, 130, 250, 24)
pbar.setValue(5)
frame.add(pbar)
pbar.repaint()
def radioGroup():
global radio1
global radio2
global radio3
radioGrp = ButtonGroup()
radio1 = JRadioButton("A")
radio1.setBounds(30,160,45,30)
radio1.setSelected(True)
radio1.addItemListener(radioGrpAction)
radio2 = JRadioButton("B")
radio2.setBounds(80,160,45,30)
radio2.addItemListener(radioGrpAction)
radio3 = JRadioButton("C")
radio3.setBounds(130,160,45,30)
radio3.addItemListener(radioGrpAction)
radioGrp.add(radio1)
radioGrp.add(radio2)
radioGrp.add(radio3)
frame.add(radio1)
frame.add(radio2)
frame.add(radio3)
radio1.repaint()
radio2.repaint()
radio3.repaint()
def comboBox():
country = ["France", "Australia", "U.S.A.", "England", "New Zealand"]
comboBox = JComboBox(country)
frame.add(comboBox)
comboBox.setBounds(440, 60, 130, 24)
comboBox.setToolTipText("comboBox")
comboBox.repaint()
def myList():
global myList
items = ["Item 1","Item 2","Item 3","Item 4"]
myList = JList(items)
myList.setBounds(450, 120,100,75)
myList.addListSelectionListener(myListAction)
frame.add(myList)
myList.repaint()
def tabbedPane():
panel1 = JPanel()
panel1.setBackground(Color.BLUE)
panel2 = JPanel()
panel2.setBackground(Color.GREEN)
panel3 = JPanel()
panel3.setBackground(Color.RED)
tabbedPane = JTabbedPane()
tabbedPane.setBounds(310, 230, 250, 150)
tabbedPane.add("Tab 1",panel1)
tabbedPane.add("Tab 2",panel2)
tabbedPane.add("Tab 3",panel3)
frame.add(tabbedPane)
tabbedPane.repaint()
def table():
data = [
["George","43","YES"],
["Mary","39","NO"],
["Bill","12","YES"],
["Vicki","10","YES"],
["Joe","6","NO"],
["Angie","1","NO"]
]
header = ["NAME","AGE","CAN SWIM"]
table = JTable(data,header)
scrlPane = JScrollPane(table)
scrlPane.setBounds(320,390,230,80)
frame.add(scrlPane)
def txtArea_1():
global txtScrlPane_1
global txtArea_1
txtArea_1 = JTextArea()
txtScrlPane_1 = JScrollPane(txtArea_1)
txtArea_1.setEditable(True)
font = Font("Menlo",0,14)
txtArea_1.setFont(font)
txtArea_1.setLineWrap(False)
txtArea_1.setWrapStyleWord(True)
txtArea_1.repaint()
def txtArea_2():
global txtScrlPane_2
global txtArea_2
txtArea_2 = JTextArea()
txtScrlPane_2 = JScrollPane(txtArea_2)
txtArea_2.setEditable(True)
txtArea_2.setLineWrap(False)
txtArea_2.setWrapStyleWord(True)
txtArea_2.repaint()
def splitPane():
splitPane = JSplitPane(JSplitPane.VERTICAL_SPLIT, txtScrlPane_1, txtScrlPane_2)
splitPane.setBounds(30,200,280,350)
splitPane.setDividerLocation(250)
frame.add(splitPane)
splitPane.repaint()
def buildWnd():
menuBar()
label()
textField()
pushBtn()
slider()
colorBtn()
checkBox()
spinner()
progressBar()
radioGroup()
comboBox()
myList()
tabbedPane()
table()
txtArea_1()
txtArea_2()
splitPane()
# This needs to be down here to make all components visible!
frame.setVisible(True)
def setup():
global frame
global canvas
size(_wndW,_wndH)
wnd = get_surface()
wnd.set_location(100,100)
wnd.set_title("Swing Components In Processing Window")
canvas = wnd.get_native()
frame = canvas.getFrame()
canvas.setBounds(300,60,120,120)
SwingUtilities.invokeLater(buildWnd)
def draw():
background(255)
fill(dotColor.getRed(), dotColor.getGreen(), dotColor.getBlue())
circle(60,60,radius) |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
The following source code demonstrates the use of several Swing components in the default Processing window. It uses 'Imported mode for py5' and was created on MacOS with M2 chip (has not been tested on other platforms).
Output(mac):
Beta Was this translation helpful? Give feedback.
All reactions