From e96d3c681bc38d5e371d0702aeedc29d63a4915d Mon Sep 17 00:00:00 2001 From: mykola2312 <49044616+mykola2312@users.noreply.github.com> Date: Fri, 12 Apr 2024 05:17:00 +0300 Subject: [PATCH] implemented EDT for MainFrame, making access from non-UI thread possible --- .../com/mykola2312/mptv/ui/MainFrame.java | 46 +++++++++++++++++-- 1 file changed, 41 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/mykola2312/mptv/ui/MainFrame.java b/src/main/java/com/mykola2312/mptv/ui/MainFrame.java index afe15cb..2961b09 100644 --- a/src/main/java/com/mykola2312/mptv/ui/MainFrame.java +++ b/src/main/java/com/mykola2312/mptv/ui/MainFrame.java @@ -11,7 +11,25 @@ public class MainFrame { private JList categoryList; private JList channelList; - public void create(short width, short height, boolean fullscreen) { + public void setCategories(String[] categories) { + if (categoryList == null) { + categoryList = new JList<>(categories); + categoryList.setFont(font); + } else { + categoryList.setListData(categories); + } + } + + public void setChannels(String[] channels) { + if (channelList == null) { + channelList = new JList<>(channels); + channelList.setFont(font); + } else { + channelList.setListData(channels); + } + } + + private void spawn(short width, short height, boolean fullscreen) { font = new Font("Arial", Font.PLAIN, 48); frame = new JFrame(I18n.get("MainFrame_Title")); @@ -33,8 +51,7 @@ public class MainFrame { items[i] = "item" + i; } - categoryList = new JList(items); - categoryList.setFont(font); + setCategories(items); final JScrollPane categoryListScroll = new JScrollPane(categoryList); categoryListScroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_NEVER); @@ -43,8 +60,7 @@ public class MainFrame { statusPanel.add(new JButton("status")); - channelList = new JList(items); - channelList.setFont(font); + setChannels(items); final JScrollPane channelListScroll = new JScrollPane(channelList); channelListScroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_NEVER); @@ -63,4 +79,24 @@ public class MainFrame { frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } + + public void create(short width, short height, boolean fullscreen) { + SwingUtilities.invokeLater(() -> { + spawn(width, height, fullscreen); + }); + + // TEST: multi thread access + new Thread(() -> { + try { + Thread.sleep(5000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + + String[] newChannels = new String[] {"first", "second"}; + SwingUtilities.invokeLater(() -> { + setChannels(newChannels); + }); + }).start(); + } }