问题:我有一个方法可以从解析的ArrayList创建一个列表.我设法在GUI中显示列表,没有滚动条.但是,我有问题设置它只显示ArrayList的大小.意思是,如果大小为6,则显示的列表中应该只有6行.下面是我正在使用的代码.我尝试设置visibleRowCount如下,但它不起作用.我尝试打印出结果,它显示了更改.
private void createSuggestionList(ArrayList<String> str) {
int visibleRowCount = str.size();
System.out.println("visibleRowCount " + visibleRowCount);
listForSuggestion = new JList(str.toArray());
listForSuggestion.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
listForSuggestion.setSelectedIndex(0);
listForSuggestion.setVisibleRowCount(visibleRowCount);
System.out.println(listForSuggestion.getVisibleRowCount());
listScrollPane = new JScrollPane(listForSuggestion);
MouseListener mouseListener = new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent mouseEvent) {
JList theList = (JList) mouseEvent.getSource();
if (mouseEvent.getClickCount() == 2) {
int index = theList.locationToIndex(mouseEvent.getPoint());
if (index >= 0) {
Object o = theList.getModel().getElementAt(index);
System.out.println("Double-clicked on: " + o.toString());
}
}
}
};
listForSuggestion.addMouseListener(mouseListener);
textPane.add(listScrollPane);
repaint();
}
Run Code Online (Sandbox Code Playgroud)
总结一下:我希望JList显示与解析的ArrayList的大小一样多的行,而不需要滚动条.
这是问题的图片:
这是与其他2的链接,因为图片分辨率非常大我害怕它会扭曲视图:
JList 1和2图片清楚地显示了它.JList显示空行,我不希望它发生.
有任何想法吗?请帮忙.谢谢.如果我没有正确地说出我的问题,请告诉我是否需要问题的图片.
-
编辑:
JScrollPane scrollPane = new JScrollPane(textPane);
scrollPane.setPreferredSize(new Dimension(200, 200));
//Create the text area for the status log and configure it.
changeLog = new JTextArea(5, 30);
changeLog.setEditable(false);
JScrollPane scrollPaneForLog = new JScrollPane(changeLog);
//Create a split pane for the change log and the text area.
JSplitPane splitPane = new JSplitPane(
JSplitPane.VERTICAL_SPLIT,
scrollPane, scrollPaneForLog);
splitPane.setOneTouchExpandable(true);
//Create the status area.
JPanel statusPane = new JPanel(new GridLayout(1, 1));
CaretListenerLabel caretListenerLabel =
new CaretListenerLabel("Caret Status");
statusPane.add(caretListenerLabel);
//Add the components.
getContentPane().add(splitPane, BorderLayout.CENTER);
getContentPane().add(statusPane, BorderLayout.PAGE_END);
Run Code Online (Sandbox Code Playgroud)
如果有帮助,textPane如何包含在容器中
另一个编辑:
public void showSuggestionList(JScrollPane pane, Rectangle caretCoords) {
pane.setVisible(false);
pane.setBounds(caretCoords.x - 5, caretCoords.y + 25, 400, 250);
pane.setVisible(true);
repaint();
}
Run Code Online (Sandbox Code Playgroud)
showSuggestionList()被称为我的CaretListener,用于在插入符号移动时显示JScrollPane.
我怀疑这是布局管理的textPane问题。据我所知,listForSuggestions如果尊重首选尺寸,则不应占用比显示这些项目所需的更多空间。
所以它JTextPane是 a Container,也就是说,你可以向它添加子组件。但这些子组件是如何布局的呢?这取决于当前使用的布局管理器。listForSuggestios如果布局管理器尊重我认为你应该没问题的首选尺寸。但不确定。
据我所知,您只需实例化 a 即可获得“空布局” JTextPane,这意味着除非您显式设置另一个布局管理器,否则您需要自己处理子组件的放置/调整大小。
你可以尝试做类似的事情
Dimension dim = listForSuggestions.getPreferredSize();
listForSuggestions.setBounds(xPos, yPos, dim.getWidth(), dim.getHeight());
Run Code Online (Sandbox Code Playgroud)
这是一个完整的例子
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import javax.swing.*;
public class FrameTest {
public static void main(String[] args) {
JFrame f = new JFrame("Frame Test");
ArrayList<String> str = new ArrayList<String>();
for (int i = 0; i < 20; i++)
str.add("number " + i);
JTextPane tp = new JTextPane();
int visibleRowCount = str.size();
System.out.println("visibleRowCount " + visibleRowCount);
JList listForSuggestion = new JList(str.toArray());
listForSuggestion.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
listForSuggestion.setSelectedIndex(0);
listForSuggestion.setVisibleRowCount(5);
System.out.println(listForSuggestion.getVisibleRowCount());
JScrollPane listScrollPane = new JScrollPane(listForSuggestion);
MouseListener mouseListener = new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent mouseEvent) {
JList theList = (JList) mouseEvent.getSource();
if (mouseEvent.getClickCount() == 2) {
int index = theList.locationToIndex(mouseEvent.getPoint());
if (index >= 0) {
Object o = theList.getModel().getElementAt(index);
System.out.println("Double-clicked on: " + o.toString());
}
}
}
};
listForSuggestion.setBorder(BorderFactory.createMatteBorder(1, 1, 1, 1, Color.BLACK));
listForSuggestion.addMouseListener(mouseListener);
Dimension dim = listForSuggestion.getPreferredSize();
listForSuggestion.setBounds(20, 20, (int) dim.getWidth(), (int) dim.getHeight());
tp.add(listForSuggestion);
f.add(tp);
f.setSize(400, 400);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
}
Run Code Online (Sandbox Code Playgroud)
我认为最优雅的方法是推出自己的布局管理器。(实际上非常简单。)然后,textPane.add(list)您不做 ,而是做textPane.add(list, YourLayoutManager.POPUP_LIST)。然后,布局管理器会记住list应该根据其首选大小进行布局的事实,并在其layoutContainer方法中相应地进行布局。(如果您给出YourLayoutManager对其所附加的 的引用JTextPane,您甚至可以将其布局list在当前插入符号位置的右侧。)
| 归档时间: |
|
| 查看次数: |
7032 次 |
| 最近记录: |