我正在使用一个GUI,其中需要将传感器添加到两个不同类的两个JScrollPanes
中。有一个UIManager类,用于发送当前连接到UI类的传感器的ArrayList
,然后将这些传感器设置在JPanel
内,然后将其设置为两个JScrollPanes的视口:
来自UIManager类
private static void updateSensorsConnectedSmallEntries() {
UserInterface.setSensorEntriesSmall(sensorEntriesSmall);
}
在UserInterface类内部
public static void setSensorEntriesSmall(ArrayList<SensorEntrySmall> sesList) {
setSensorEntriesSmall(tabDataLogging.getConnectedSensorsScrollList(), sesList);
setSensorEntriesSmall(tabLiveChart.getConnectedSensorsScrollList(), sesList);
}
private static void setSensorEntriesSmall(JScrollPane pane, ArrayList<SensorEntrySmall> sesList) {
javax.swing.JPanel scrollList = new javax.swing.JPanel();
scrollList.setBackground(Color.WHITE);
pane.setBackground(Color.WHITE);
javax.swing.GroupLayout paneLayout = new javax.swing.GroupLayout(scrollList);
scrollList.setLayout(paneLayout);
GroupLayout.ParallelGroup hor = paneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING);
GroupLayout.ParallelGroup ver = paneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING);
GroupLayout.SequentialGroup horSeq = paneLayout.createSequentialGroup();
GroupLayout.SequentialGroup verSeq = paneLayout.createSequentialGroup();
GroupLayout.ParallelGroup horSeqPlaceHolder = paneLayout.createParallelGroup();
horSeq.addContainerGap();
verSeq.addContainerGap();
for (SensorEntrySmall ses : sesList) {
horSeqPlaceHolder.addComponent(ses.getSensorEntrySmall());
verSeq.addComponent(ses.getSensorEntrySmall(), javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE);
verSeq.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED);
}
horSeq.addGroup(horSeqPlaceHolder);
horSeq.addContainerGap();
verSeq.addContainerGap();
hor.addGroup(horSeq);
paneLayout.setHorizontalGroup(hor);
ver.addGroup(verSeq);
paneLayout.setVerticalGroup(ver);
pane.setViewportView(scrollList);
}
问题似乎出在将列表分配给不同选项卡的顺序上。如果我先将它们分配给tabDataLogging
,然后再分配给tabLiveChart
,则传感器仅显示在tabLiveChart
中,反之亦然。
我希望传感器显示在两个选项卡中。有人对这里出什么问题有任何建议吗?
答案 0 :(得分:0)
需要将传感器添加到两个不同类的两个JScrollPanes中。
Swing组件只能有一个父组件,因此您不能共享面板。
但是,您可以共享数据。大多数Swing组件都有一个<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.0/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/js/bootstrap.min.js"></script>
</head>
<body>
<nav class="navbar navbar-inverse navbar-fixed-top">
<div class="container-fluid">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#mynavbar">
</button>
</div>
<div class="collapse navbar-collapse" id="mynavbar">
<ul class="nav navbar-nav">
<li class="active"><a href="index.html">Home</a></li>
<li><a href="login.html">Account</a></li>
<li><a href="about.html">About</a></li>
</ul>
</div>
</div>
</nav>
</body>
</html>
。该模型可以共享。
例如,如果您的传感器显示在JTable中,则可以共享TableModel。您将需要创建两个JTable,但是可以将相同的模型添加到每个表中。