在java swing中动态创建项目或允许/禁止用户操作

发布于 2024-10-08 12:57:47 字数 432 浏览 4 评论 0原文

我有一个包含用户、角色和权限的数据库。我希望能够将其映射到前端(Java Swing),以便无法执行操作的用户看不到它。

示例:

  • 角色 AddressManager 拥有 create_address、edit_address 和 删除_地址。
  • 用户 A 具有 create_address 和 edit_address 权限。
  • 用户B具有remove_address权限。

我想要地址视图的三个按钮代表 AddressManager 中的角色,并让用户 A 和 B 启用/禁用这些按钮。

问题:是否有任何简单的方法可以将数据库表值映射到 Swing 组件(按钮)?

一种方法是手动为每个组件分配启用/禁用,但如果应用程序中有 40 个对话框,其中大约 200 个组件必须具有权限,那么这种方法是不切实际的。

I have a database which contains users, roles, and permissions. I want to be able to map this to the front end (Java Swing) so a user who can't do an action can't see it.

An example:

  • Role AddressManager has permissions create_address, edit_address and
    remove_address.
  • User A has permissions create_address and edit_address.
  • User B has permission remove_address.

I want three buttons for the address view that represent the roles from the AddressManager, and for the users A and B to enable / disable the buttons.

Question: Is there any easy way to map database table values to Swing components (Buttons)?

One way is to assign enable/disable manually to every single component, but that’s unpractical if there are 40 dialogs in the application with about 200 components that must have permission.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

月亮是我掰弯的 2024-10-15 12:57:47

你能做的就是编写一个这样的类并在任何地方使用它。
在您的示例中,您将使用 new ActionContainer("adress"); 添加它它将创建一个 create_address、edit_address、delete_address 按钮,如果用户拥有匹配权限,则启用这些按钮。

package de.steamnet.samples;

// This class is a Panel that renders buttons based on rights.

import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JPanel;

public class ActionContainer extends JPanel {
    List<JButton> buttons = new ArrayList<JButton>();

    public ActionContainer(String rightBase) {
        List<String> rights = database.getRightsStartingWith(rightBase);
        for(String nextRight : rights) {
            JButton next = new JButton(nextRight);
            buttons.add(next);
            if(user.hasRight(nextRight)) {
                next.setEnabled(true);
            } else {
                next.setEnabled(false);
            }
            add(next);
        }
    }

    public void addActionListener(ActionListener al) {
        for(JButton next: buttons) {
            next.addActionListener(al);
        }
    }
}

What you can do is to write a class like this and use it everywhere.
In your example you would add it with new ActionContainer("adress"); and it will create a create_address, edit_address, delete_address Button that are enabled if the user posses the matching right.

package de.steamnet.samples;

// This class is a Panel that renders buttons based on rights.

import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JPanel;

public class ActionContainer extends JPanel {
    List<JButton> buttons = new ArrayList<JButton>();

    public ActionContainer(String rightBase) {
        List<String> rights = database.getRightsStartingWith(rightBase);
        for(String nextRight : rights) {
            JButton next = new JButton(nextRight);
            buttons.add(next);
            if(user.hasRight(nextRight)) {
                next.setEnabled(true);
            } else {
                next.setEnabled(false);
            }
            add(next);
        }
    }

    public void addActionListener(ActionListener al) {
        for(JButton next: buttons) {
            next.addActionListener(al);
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文