import javax.swing.;
import javax.swing.table.AbstractTableModel;
import java.awt.;
import java.awt.event.*;
import java.util.List;
public class RoutingInfoTableSelector {
// Your complex record class
public static class RoutingInfoPresentation {
public String url;
public String[] receiveDestinationNames;
public String sendDestinationName;
public String solaceJmsVpn;
public String login;
public String password;
public RoutingInfoPresentation(String url, String sendName, String vpn, String login, String password, String[] receiveNames) {
this.url = url;
this.sendDestinationName = sendName;
this.solaceJmsVpn = vpn;
this.login = login;
this.password = password;
this.receiveDestinationNames = receiveNames;
}
}
// Custom TableModel for displaying records in columns
static class RoutingInfoTableModel extends AbstractTableModel {
private final List<RoutingInfoPresentation> data;
private final String[] columnNames = {
"URL", "Send Dest", "Receive Dest(s)", "VPN", "Login", "Password"
};
public RoutingInfoTableModel(List<RoutingInfoPresentation> data) {
this.data = data;
}
@Override
public int getRowCount() {
return data.size();
}
@Override
public int getColumnCount() {
return columnNames.length;
}
@Override
public Object getValueAt(int row, int col) {
RoutingInfoPresentation record = data.get(row);
switch (col) {
case 0: return record.url;
case 1: return record.sendDestinationName;
case 2: return record.receiveDestinationNames != null
? String.join(", ", record.receiveDestinationNames)
: "";
case 3: return record.solaceJmsVpn;
case 4: return record.login;
case 5: return record.password;
default: return "";
}
}
@Override
public String getColumnName(int column) {
return columnNames[column];
}
public RoutingInfoPresentation getRowObject(int rowIndex) {
return data.get(rowIndex);
}
}
// The popup selector dialog
public static RoutingInfoPresentation showSelector(Component parent, List<RoutingInfoPresentation> data) {
JDialog dialog = new JDialog((Frame) null, "Select Routing Info", true);
dialog.setLayout(new BorderLayout());
RoutingInfoTableModel tableModel = new RoutingInfoTableModel(data);
JTable table = new JTable(tableModel);
table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
table.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
table.setFillsViewportHeight(true);
JScrollPane scrollPane = new JScrollPane(table);
dialog.add(scrollPane, BorderLayout.CENTER);
JPanel buttonPanel = new JPanel();
JButton selectBtn = new JButton("Select");
JButton cancelBtn = new JButton("Cancel");
buttonPanel.add(selectBtn);
buttonPanel.add(cancelBtn);
dialog.add(buttonPanel, BorderLayout.SOUTH);
final RoutingInfoPresentation[] result = new RoutingInfoPresentation[1];
selectBtn.addActionListener(e -> {
int selectedRow = table.getSelectedRow();
if (selectedRow >= 0) {
result[0] = tableModel.getRowObject(selectedRow);
dialog.dispose();
} else {
JOptionPane.showMessageDialog(dialog, "Please select a row.", "No Selection", JOptionPane.WARNING_MESSAGE);
}
});
cancelBtn.addActionListener(e -> {
result[0] = null;
dialog.dispose();
});
dialog.setSize(800, 300);
dialog.setLocationRelativeTo(parent);
dialog.setVisible(true);
return result[0]; // selected or null
}
// Example launcher
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
JFrame frame = new JFrame("RoutingInfo Selector (Table View)");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 150);
frame.setLayout(new FlowLayout());
JButton openSelector = new JButton("Select Routing Info");
JLabel resultLabel = new JLabel("None selected");
openSelector.addActionListener(e -> {
List<RoutingInfoPresentation> dataList = List.of(
new RoutingInfoPresentation("http://server1", "send1", "vpn1", "alice", "secret1", new String[]{"r1", "r2"}),
new RoutingInfoPresentation("http://server2", "send2", "vpn2", "bob", "secret2", new String[]{"r3"})
);
RoutingInfoPresentation selected = showSelector(frame, dataList);
if (selected != null) {
resultLabel.setText("Selected: " + selected.url + " / " + selected.login);
} else {
resultLabel.setText("None selected");
}
});
frame.add(openSelector);
frame.add(resultLabel);
frame.setVisible(true);
});
}
}
Top comments (0)