{"id":22248882,"url":"https://github.com/parubok/swing-stream-utils","last_synced_at":"2025-06-26T00:04:09.063Z","repository":{"id":57734451,"uuid":"271774514","full_name":"parubok/swing-stream-utils","owner":"parubok","description":"Utilities for working with Java Swing components via Java 8 streams.","archived":false,"fork":false,"pushed_at":"2023-04-29T07:48:47.000Z","size":313,"stargazers_count":13,"open_issues_count":0,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-06-26T00:03:11.206Z","etag":null,"topics":["java-8","java-swing","jcombobox","jtable","jtree","streams-api","swing"],"latest_commit_sha":null,"homepage":"","language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/parubok.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2020-06-12T10:45:40.000Z","updated_at":"2025-04-27T17:41:39.000Z","dependencies_parsed_at":"2025-06-26T00:03:16.618Z","dependency_job_id":null,"html_url":"https://github.com/parubok/swing-stream-utils","commit_stats":null,"previous_names":[],"tags_count":39,"template":false,"template_full_name":null,"purl":"pkg:github/parubok/swing-stream-utils","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/parubok%2Fswing-stream-utils","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/parubok%2Fswing-stream-utils/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/parubok%2Fswing-stream-utils/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/parubok%2Fswing-stream-utils/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/parubok","download_url":"https://codeload.github.com/parubok/swing-stream-utils/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/parubok%2Fswing-stream-utils/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":261973725,"owners_count":23238586,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["java-8","java-swing","jcombobox","jtable","jtree","streams-api","swing"],"created_at":"2024-12-03T06:19:44.037Z","updated_at":"2025-06-26T00:04:09.041Z","avatar_url":"https://github.com/parubok.png","language":"Java","funding_links":[],"categories":[],"sub_categories":[],"readme":"![Java CI with Maven](https://github.com/parubok/table-stream-utils/workflows/Java%20CI%20with%20Maven/badge.svg)\n[![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://github.com/parubok/table-stream-utils/blob/master/LICENSE)\n[![Latest Version](https://img.shields.io/maven-central/v/io.github.parubok/swing-stream-utils)](https://search.maven.org/search?q=a:swing-stream-utils)\n[![javadoc](https://javadoc.io/badge2/io.github.parubok/swing-stream-utils/javadoc.svg)](https://javadoc.io/doc/io.github.parubok/swing-stream-utils)\n\n# swing-stream-utils\nUtils for working with Java Swing components via Java 8 streams.\n\nFunctionality of the library is provided by the static methods of class [`SwingStreamUtils`](https://javadoc.io/doc/io.github.parubok/swing-stream-utils/latest/io/github/parubok/stream/SwingStreamUtils.html).\nThe methods may be divided into two categories: those which allow to iterate (or stream) over data items of Swing components \n(e.g. `SwingStreamUtils.stream`) and collectors which allow to create Swing components from a stream data (e.g. `SwingStreamUtils.toTable`).\n\nExample 1 (for `JTable`, count how many times value `\"London\"` appears in the selected cells of column `\"City\"`):\n```java\nimport javax.swing.JTable;\n\nimport io.github.parubok.stream.SwingStreamUtils;\nimport io.github.parubok.stream.TableCellData;\n\nJTable table = ...;\nlong count = SwingStreamUtils.stream(table)\n  .filter(TableCellData::isSelected)\n  .filter(cellData -\u003e cellData.getColumnName().equals(\"City\"))\n  .map(TableCellData::getValue)\n  .filter(\"London\"::equals)\n  .count();\n  \n// or with for-each loop:\ncount = 0;\nfor (TableCellData\u003cJTable\u003e cellData: SwingStreamUtils.asIterable(table)) {\n  if (cellData.isSelected() \u0026\u0026 cellData.getColumnName().equals(\"City\") \u0026\u0026 \"London\".equals(cellData.getValue())) {\n    count++;\n  }\n}\n```\n\nExample 2 (create table with 'Name' and 'Size' columns from a list of `File` objects):\n```java\nimport java.io.File;\nimport java.util.List;\n\nimport io.github.parubok.stream.ColumnDef;\n\nimport static io.github.parubok.stream.SwingStreamUtils.toTable;\n\nList\u003cFile\u003e files = ...;\n/* FileTable is a subclass of JTable */\nFileTable table = files.stream()\n             .collect(toTable(FileTable::new, new ColumnDef\u003c\u003e(\"Name\", File::getName, 100, String.class), \n                                                new ColumnDef\u003c\u003e(\"Size\", File::length, 70, Long.class)));\n```\n\nExample 3 (create a table with specific model class):\n```java\nimport java.io.File;\nimport java.util.List;\n\nimport io.github.parubok.stream.ColumnDef;\n\nimport static io.github.parubok.stream.SwingStreamUtils.toTable;\n\nList\u003cFile\u003e files = ...;\nFileTable table = files.stream()\n             .collect(toTable(() -\u003e new FileTable(), rowCount -\u003e new FileTableModel(rowCount), \n                                                new ColumnDef\u003c\u003e(\"Name\", File::getName, 100, String.class), \n                                                new ColumnDef\u003c\u003e(\"Size\", File::length, 70, Long.class)));\n```\n\n### Tip: Using `enum` to define table columns\nFor a table with fixed (i.e. predefined) columns, Java `enum` provides a convenient way to define and access the column \ndefinitions - for example, when a column index is required after the table was created.\n\nExample:\n```java\nimport java.io.File;\nimport java.util.function.Supplier;\n\nimport io.github.parubok.stream.ColumnDef;\n\nimport static io.github.parubok.stream.SwingStreamUtils.toTable;\n\nenum Column implements Supplier\u003cColumnDef\u003cFile\u003e\u003e {\n    NAME(new ColumnDef\u003c\u003e(\"Name\", File::getName, 100, String.class)),\n    SIZE(new ColumnDef\u003c\u003e(\"Size\", File::length, 70, Long.class));\n\n    final ColumnDef\u003cFile\u003e def;\n\n    Column(ColumnDef\u003cFile\u003e def) {\n        this.def = def;\n    }\n\n    @Override\n    public ColumnDef\u003cFile\u003e get() {\n        return def;\n    }\n}\n\nList\u003cFile\u003e files = ...;\n// use enum values() method to get all column definitions:\nJTable table = files.stream()\n        .collect(toTable(ColumnDef.get(Column.values())));\n\n// use enum ordinal() method to obtain column index:\nString name = (String) table.getValueAt(0, FileTableColumn.NAME.ordinal());\n\n// translate column index to ColumnDef:\nint columnIndex = ...;\nColumnDef\u003cFile\u003e def = FileTableColumn.values()[columnIndex].get();\n```\n\nExample 4 (create combo box with file names from a list of `File` objects):\n```java\nimport java.io.File;\nimport java.util.List;\nimport javax.swing.JComboBox;\n\nimport static io.github.parubok.stream.SwingStreamUtils.toComboBox;\n\nList\u003cFile\u003e files = ...;\nJComboBox\u003cString\u003e = files.stream()\n             .map(File::getName)\n             .collect(toComboBox());\n```\n\nExample 5 (find all visible descendants of a container):\n```java\nimport java.util.List;\nimport java.util.stream.Collectors;\nimport java.awt.Component;\nimport java.awt.Container;\n\nimport static io.github.parubok.stream.SwingStreamUtils.streamDescendants;\n\nContainer container = ...;\nList\u003cComponent\u003e visibleDescendants = streamDescendants(container)\n             .skip(1) // skip the container itself\n             .filter(Component::isVisible)\n             .collect(Collectors.toList());\n```\n\nExample 6 (find all unselected combo box items which start with a letter \"z\"):\n```java\n\n\nimport io.github.parubok.stream.SwingStreamUtils;\n\nJComboBox\u003cString\u003e comboBox = ...;\nList\u003cString\u003e visibleDescendants = SwingStreamUtils.stream(comboBox)\n             .filter(comboBoxItem -\u003e !comboBoxItem.isSelected())\n             .filter(comboBoxItem -\u003e comboBoxItem.getItem().startsWith(\"z\"))\n             .collect(Collectors.toList());\n```\n\nExample 7 (search for the first tree path with the specified node):\n```java\nimport java.util.Optional;\nimport javax.swing.JTree;\nimport javax.swing.tree.TreeNode;\n\nimport io.github.parubok.stream.KTreePath;\nimport io.github.parubok.stream.SwingStreamUtils;\nimport io.github.parubok.stream.TreeTraversalType;\n\nJTree tree = ...;\nTreeNode node = ...;\nOptional\u003cKTreePath\u003e nodePath = SwingStreamUtils.stream(tree, TreeTraversalType.PRE_ORDER)\n             .filter(path -\u003e path.isLastComponent(node))\n             .findFirst();\n```\n\nIt is worth mentioning that in most cases (check JavaDoc) the utility ensures that the Swing component creation and configuration are performed on EDT, even when the streaming code runs on a different thread. So the following example code is valid:\n```java\nimport java.util.List;\n...\nimport io.github.parubok.stream.ColumnDef;\n\nimport static io.github.parubok.stream.SwingStreamUtils.toTable;\n\n// may be not EDT (for example, Swing worker background thread)\nList\u003cServer\u003e servers = ...;\nJTable table = servers.parallelStream() // OK to use parallel stream!\n             .collect(toTable(new ColumnDef\u003c\u003e(\"Name\", Server::getName, 100, String.class),\n                               new ColumnDef\u003c\u003e(\"Users\", Server::getUserCount, 50, Integer.class),\n                               new ColumnDef\u003c\u003e(\"Status\", Server::getStatus, 200, String.class));\n```\n\n`SimpleTableModel` (extends `javax.swing.table.DefaultTableModel`) may be build from a stream as following:\n```java\nimport java.util.List;\n...\nimport io.github.parubok.stream.ColumnDef;\n\nimport static io.github.parubok.stream.SwingStreamUtils.toTableModel;\n\n// may be executed on any thread\nList\u003cUser\u003e users = ...;\nSimpleTableModel\u003cUser\u003e tableModel = users.parallelStream()\n                            .collect(toTableModel(new ColumnDef\u003c\u003e(\"Name\", User::getName, 100, String.class),\n                                                  new ColumnDef\u003c\u003e(\"ID\", User::getID, 50, Long.class),\n                                                  new ColumnDef\u003c\u003e(\"Role\", User::getRole, 200, String.class));\n```\n\nThis project has no external dependencies (except JUnit 5, for testing).\n\nRequires Java 8 or later.\n\n### License\n\nThis project is licensed under [Apache License, version 2.0](https://www.apache.org/licenses/LICENSE-2.0)\n\n### Installation\n\nReleases are available in [Maven Central](https://repo1.maven.org/maven2/io/github/parubok/swing-stream-utils/)\n\n#### Maven\n\nAdd this snippet to the pom.xml `dependencies` section:\n\n```xml\n\u003cdependency\u003e\n    \u003cgroupId\u003eio.github.parubok\u003c/groupId\u003e\n    \u003cartifactId\u003eswing-stream-utils\u003c/artifactId\u003e\n    \u003cversion\u003e1.37\u003c/version\u003e\n\u003c/dependency\u003e\n```\n\n### See Also\n\nhttp://www.glazedlists.com/\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fparubok%2Fswing-stream-utils","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fparubok%2Fswing-stream-utils","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fparubok%2Fswing-stream-utils/lists"}