When I try setting the JList visibility to false to hide the content, I discover that the list is not hidden entirly when I scroll down. I have tried validate() and repaint() of JList, JScrollPane and JPanel (MainPanel), but no changes, I have also tried this:
jScollPane.setVerticalScrollBarPolicy(
javax.swing.ScrollPaneCantants.HORIZONTAL_SCROLLBAR_NEVER);
Also, no results. Some elements of the list remain visible if I scroll down the scroll bar.
I see at least two ways to do that:
Remove the JList from the scrollpane: scrollPane.setViewPortView(null);
Set the model of the JList to an empty model: list.setModel(new DefaultListModel());
Another alternative is to change the scroll mode of the JViewPort:
scrollpane.getViewport().setScrollMode(JViewport.SIMPLE_SCROLL_MODE);
but of course this is not efficient at all. If you use that solution, when you make your component visible again, make sure to also properly reset the scroll mode to BLIT_SCROLL_MODE or BACKINGSTORE_SCROLL_MODE which are much more efficient.
Related
I faced this new thing today, and I didn't know why. When I want to show something in panel for example, I just add it to panel; but why I cannot add a table to scroll pane directly, and why I have to call the setviewportview() method? What does add() method do and what does setViewProtView() do?
Basically, you should not use JScrollPane#add.
JScrollPane has a single component already attached to it, a JViewport, this is what the JScrollPane uses to display any component added to the view port.
setViewportView is a convenience method for for JScrollPane#getViewport#setView
The basic concept comes down to the fact that from the scroll panes point of view, it will only show a single component, so add doesn't actually make any sense for it. The method is a consequence of extending from JComponent -> Container
In my Java program, which I have made by following a tutorial, there is a class in which I create a JSplitPane, and inside that I put a JList embedded in a JScrollPane and a JTextArea below that. The problem is, whenever I add enough items to the JList so that you cannot see all of them because the list is relatively small, the scroll bar does not appear on the right side of the list. This could be because I am nesting that split pane in another split pane, but it seems to be working fine in the tutorials that I am watching, so I'm wondering what's wrong. Here is the code that I use to create that particular JSplitPane:
lowerPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, new JScrollPane(messages), textPanel);
As you can see, I have carefully embedded the messages component, which is a JList, inside a JScrollPane. However, the scroll bar does not appear in my program when the JList is filled up. Here is a picture of my program:
The application may be hard to understand, even though I know it is very basic coding. What I'm really concerned with is the JScrollPane. In the window where it lists the titles of the received messages, you can see no scroll bar.
I wonder if you guys had problem such if you got some JPanel which got his own JScrollPane and in your panel you are using a lot of JTables with their own JScrollPanes, there is a problem to scroll up/down your panel?
I mean when your mouse is on viewport of some table, then JScrollPane of JTable is listening on scroll, so when I got many JTables I am able to scroll only in a few places of the panel, it's so annoying...
Are there some functions which will send my scroll event to parent JPanel JScrollPane when the JTable scroll is even not shown? I mean I want to disable JScrollPane whenever some JTable don't need to use scroll (when it is hidden, cause there are too less records).
Yes, JTable understands several named scrolling actions, listed below. They are normally used in key bindings, but you can evoke them yourself, as shown in this related example that commandeers the actions defined for a scroll pane.
Addendum: In outline, get the named action from the component's action map:
Action action = table.getActionMap().get(name);
Evoke the action by name when needed:
action.actionPerformed(new ActionEvent(table, 0, name));
Scrolling related action names for JTable:
scrollDownChangeSelection
scrollDownExtendSelection
scrollLeftChangeSelection
scrollLeftExtendSelection
scrollRightChangeSelection
scrollRightExtendSelection
scrollUpChangeSelection
scrollUpExtendSelection
I am using three JButtons in my swing application. When I click on each button, the corresponding data (formatted in JTable with JScrollPane) will display on JPanel.
Problem: when I resize the JFrame, the JPanel is replacing with default button (the button which i was clicked first) information instead of current JButton information.
My sample code:
jbutton1.addActionListener(this);
jbutton2.addActionListener(this);
public void actioPerformed(ActionEvent e){
if(e.getActionCommand.equals("button1"))
JPanel.add(table1);
}
if(e.getActionCommand.equals("button2"))
JPanel.add(table1);
}.......
Resizing the JPanel will not suddenly replace components or add other components to the panel.
My best guess (and it is a guess due to the limited information in the question) is that none of your buttons actually work and just show the wrong information.
The code you posted only contains an add without any revalidation of the layout. Consult the javadoc of the Container#add method. When you resize, the layout gets revalidated and you see what is actually contained in the JPanel.
Possible solutions:
Call invalidate and repaint on your panel as well in your ActionListener
Use a CardLayout to switch between the different components
I personally prefer the CardLayout option, but it might depend a bit on the situation.
Note that in the code you posted, you add table1 for both buttons. Might be a copy-paste problem, or a problem with your actual code.
I was unable express problem clearly.Sorry for your inconvenience.
JPanel.removeAll() method has fixed my problem.
I added this method before adding any new component to JPanel. That fixes JPanel unexpected behavior.
I would like to make JComboBoxes scrollable by dragging their contents. It makes sense for a touch-screen app. I think I could manage to do it if there was a way to programmatically scroll a JComboBox. Is there?
I'm not sure I understand the question since the popup of the combo box is scrollable by default.
However, in general, to scroll a component added to a scrollpane you would use the scrollRectToVisible(...) method on that component.
The combo box popup uses a JList to hold each item. You can access the JList using:
Object child = comboBox.getAccessibleContext().getAccessibleChild(0);
BasicComboPopup popup = (BasicComboPopup)child;
JList list = popup.getList();