import java.awt.*;
import javax.swing.*;
import javax.swing.table.*;
public class Demo extends JPanel {
private JButton addBtn, removeBtn, deleteBtn;
private DefaultTableModel model;
private JTable table;
private JTextField keyTf, valueTf;
public Demo() {
super(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
String[] columnIdentifiers = new String[] {
"Schlüssel", "Wert"
};
model = new DefaultTableModel();
model.setColumnIdentifiers(columnIdentifiers);
table = new JTable(model);
keyTf = new JTextField(10);
valueTf = new JTextField(10);
addBtn = new JButton("Hinzufügen");
removeBtn = new JButton("Entfernen");
deleteBtn = new JButton("Leeren");
gbc.gridheight = 4;
gbc.fill = GridBagConstraints.BOTH;
gbc.weightx = 1.0;
gbc.weighty = 1.0;
gbc.anchor = GridBagConstraints.NORTHWEST;
gbc.insets = new Insets(10, 10, 0, 10);
add(new JScrollPane(table), gbc);
gbc.gridx = 1;
gbc.gridheight = 1;
gbc.fill = GridBagConstraints.NONE;
gbc.weightx = 0.0;
gbc.weighty = 0.0;
gbc.insets = new Insets(35, 10, 0, 10);
add(new JLabel("Schlüssel:"), gbc);
gbc.gridx = 1;
gbc.gridy = 1;
gbc.fill = GridBagConstraints.NONE;
gbc.insets = new Insets(10, 10, 0, 10);
add(new JLabel("Wert:"), gbc);
gbc.gridx = 2;
gbc.gridy = 0;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.insets = new Insets(30, 10, 0, 10);
gbc.weightx = 0.4;
add(keyTf, gbc);
gbc.gridx = 2;
gbc.gridy = 1;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.insets = new Insets(5, 10, 0, 10);
add(valueTf, gbc);
gbc.gridx = 1;
gbc.gridy = 2;
gbc.gridwidth = 2;
gbc.fill = GridBagConstraints.NONE;
gbc.insets = new Insets(30, 10, 0, 10);
gbc.weightx = 0.0;
add(addBtn, gbc);
gbc.gridy = 3;
gbc.insets = new Insets(5, 10, 30, 10);
removeBtn.setPreferredSize(addBtn.getPreferredSize());
removeBtn.setMinimumSize(addBtn.getPreferredSize());
add(removeBtn, gbc);
gbc.gridx = 0;
gbc.gridy = 4;
gbc.gridwidth = 1;
gbc.fill = GridBagConstraints.NONE;
gbc.insets = new Insets(5, 10, 10, 10);
add(deleteBtn, gbc);
}
//zum Testen
public static void main(String[] args) {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
catch(Exception e) {
System.err.println(e.toString());
}
JFrame f = new JFrame("Demo-Test");
//f.setSize(400, 400);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(new Demo());
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}
}