Java中的JTable在单击JButton时更改

huangapple go评论69阅读模式
英文:

Java JTable changes when click on JButton

问题

我有一个带有表的数据库我将表的名称传递给按钮当您点击时需要出现一个带有字段和填充的表问题是每次单击按钮时我都会添加一个新表

有人可以告诉我如何做吗提前谢谢

public class App extends JPanel {

    static List<FieldElement> fields = new ArrayList<>();
    static List<Map<String, Object>> data = new ArrayList<>();
    static JTable jTable = new JTable();

    public static void createGUI() throws SQLException {

        TableContent tableContent = new TableContent();
        JFrame frame = new JFrame();
        MetadataHelper databaseMetadata = new MetadataHelper();
        List<ButtonElement> elements = databaseMetadata.showTables();
        JPanel panel = new JPanel();

        JPanel buttons = new JPanel(new GridLayout(0, 1));
        for (ButtonElement buttonElements : elements) {
            JButton jButton = new JButton(buttonElements.getTablesInMigrateSchema());
            buttons.add(jButton);
            jButton.addActionListener(new ActionListener() {
                @SneakyThrows
                @Override
                public void actionPerformed(ActionEvent e) {
                    fields = tableContent.getDatabaseMetadata().showFields(buttonElements.getTablesInMigrateSchema());
                    data = tableContent.getDatabaseMetadata().selectAll(buttonElements.getTablesInMigrateSchema());
                    Object[][] objectRows = data.stream().map(m -> m.values().toArray()).toArray(Object[][]::new);
                    jTable = new JTable(objectRows, fields.toArray());
                    panel.add(new JScrollPane(jTable));
                    frame.revalidate();
                    frame.repaint();
                }
            });
        }

        panel.add(buttons, BorderLayout.EAST);
        frame.add(panel);
        frame.setTitle("SwingSandbox");
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
    }

    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            @SneakyThrows
            public void run() {
                JFrame.setDefaultLookAndFeelDecorated(true);
                createGUI();
            }
        });
    }
}

图片部分未能在文本中显示,请提供图片的文字描述或链接以获得更多帮助。

英文:

I have a database with tables. I passed the name of the tables into the buttons. When you click, you need a table with fields and filling to appear. The problem is that I have a new table added every time I click on the button.

Can someone please say me how to do it? Thanks in advance!

public class App extends JPanel {
static List&lt;FieldElement&gt; fields = new ArrayList&lt;&gt;();
static List&lt;Map&lt;String, Object&gt;&gt; data = new ArrayList&lt;&gt;();
static JTable jTable = new JTable();
public static void createGUI() throws SQLException {
TableContent tableContent = new TableContent();
JFrame frame = new JFrame();
MetadataHelper databaseMetadata = new MetadataHelper();
List&lt;ButtonElement&gt; elements = databaseMetadata.showTables();
JPanel panel = new JPanel();
JPanel buttons = new JPanel(new GridLayout(0, 1));
for (ButtonElement buttonElements : elements) {
JButton jButton = new JButton(buttonElements.getTablesInMigrateSchema());
buttons.add(jButton);
jButton.addActionListener(new ActionListener() {
@SneakyThrows
@Override
public void actionPerformed(ActionEvent e) {
fields = tableContent.getDatabaseMetadata().showFields(buttonElements.getTablesInMigrateSchema());
data = tableContent.getDatabaseMetadata().selectAll(buttonElements.getTablesInMigrateSchema());
Object[][] objectRows = data.stream().map(m -&gt; m.values().toArray()).toArray(Object[][]::new);
jTable = new JTable(objectRows, fields.toArray());
panel.add(new JScrollPane(jTable));
frame.revalidate();
frame.repaint();
}
});
}
panel.add(buttons, BorderLayout.EAST);
frame.add(panel);
frame.setTitle(&quot;SwingSandbox&quot;);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
@SneakyThrows
public void run() {
JFrame.setDefaultLookAndFeelDecorated(true);
createGUI();
}
});
}

}Java中的JTable在单击JButton时更改

答案1

得分: 1

一种简单的方法是将JScrollPane存储在一个变量中(该变量保存您想要移除的表格):

static JTable jTable = new JTable();
static JScrollPane jScrollPane;

然后在您的actionPerformed方法中:

public void actionPerformed(ActionEvent e) {
    ...
    if (jScrollPane != null) {
        panel.remove(jScrollPane);
    }
    jTable = new JTable(objectRows, fields.toArray());
    jScrollPane = new JScrollPane(jTable);
    panel.add(jScrollPane);
    frame.revalidate();
    frame.repaint();
}
英文:

One easy possibility is to store the JScrollPane in a variable (which holds your table that you want to remove):

static JTable jTable = new JTable();
static JScrollPane jScrollPane;

And then in your actionPerformed method:

public void actionPerformed(ActionEvent e) {
...
if (jScrollPane != null) {
panel.remove(jScrollPane);
}
jTable = new JTable(objectRows, fields.toArray());
jScrollPane = new JScrollPane(jTable);
panel.add(jScrollPane);
frame.revalidate();
frame.repaint();
}

huangapple
  • 本文由 发表于 2020年9月16日 18:31:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/63918128.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定