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.
Related
I'm starting using java with NetBeans IDE. I'm using drag and dop GUI, it's so easy to use, but I got a problem. I'm writing this code at the contructor:
JComboBox combobox=new JComboBox();
combobox.addItem("Apple");
combobox.addItem("Banana");
for(int i=1;i<=10;i++){
combobox.addItem(i);
}
just right above initComponents(); hoping that my new combobox will shown when I run the project, but it doesn't. Did I do something wrong? Thanks in advance
Yes, you are creating a JComboBox, and yes, you are adding items to it (numeric and int -- that's a problem, but that's a discussion for another day), but no, you're not showing any code where you add this newly created JComboBox to a component that is displayed in the GUI. To display a component in a Swing GUI, it must be created and added to a component that is ultimately displayed in a top-level window, in the "GUI".
So this begs the question, how do you add your created JComboBox to your GUI that you've created with drag and drop code? One way: you could add it to say a JPanel that's already in your GUI, but you will need to do this after initializing components, usually this means after the constructor calls initComponents(), and you'll also need to make sure that this JPanel uses a layout manager that makes it easy for it to accept new components (this means most of the layout managers except NetBean's default layout, GroupLayout).
There are other issues, such as whether or not the container holding your JComboBox is large enough to display it, but the best suggestion that I can give is for you to go through the Swing tutorials, and hit especially hard the layout manager section. You can find links to the Swing tutorials and other Swing resources here: Swing Info.
So I have a program that at the start only contains an 'add movie' button at the bottom of the frame.
Above it I inserted a scrollpane.
I also made a seperate JPanel form which contains labels and textfields where you have to input the data of the movie.
Every time I click the 'add'-button I want a form to appear inside the scrollpane (next to previously made forms).
So I figured I just needed to do this:
private void AddMovieButtonActionPerformed(java.awt.event.ActionEvent evt) {
MovieForm movie = new MovieForm();
MovieScrollPane.add(movie);
}
But nothing new appears.
I tried validate() and repaint(), but so far these don't seem to work.
I made the interface in Eclipse btw.
Anyone who can help me?
Thanks anyway!
MovieScrollPane.add(movie);
Don't add components directly to the scrollpane. Normally a JPanel is added the the viewport of the scrollpane.
Then, whenever you add a component to a visible GUI the basic code is:
panel.add(...);
panel.revalidate();
panel.repaint();
This makes sure the layout manager is invoked to the preferred size can be recalculated.
Also, follow Java naming conventions. Variable names should NOT start with an upper case characters.
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.
I am creating a JList dynamically and I want to use it on different JPanels where I can call a setQuery(query); on it. There is a Search Button + field connected to it too. Is there a way to place the JList, search button and search field on like 10 different panels without having to duplicate the code everywhere? I can't place it on a seperate JPanel as it has other buttons / gui elements that need be around it depending on what JPanel is being displayed. I've looked everywhere but there is not much mention of reusing a dynamic JList.
You can place a component in one visualized container only. Consider creating a method that creates these components for you, that returns a JPanel that holds the components, and perhaps have all created JLists share the same ListModel, and all JButtons the same AbstractAction.
Or another option, if you are using CardLayout to swap JPanels, and you want all of the panels to hold these components above, consider not doing this but moving the JList, JButton, and JTextField out of the cards and on to a less dynamic portion of your GUI.
Also, this is not clear to me:
I can't place it on a seperate JPanel as it has other buttons / gui elements that need be around it depending on what JPanel is being displayed.
Please clarify and tell us more. Images might help as well.
Also consider the decorator pattern Your list/search panel would be the main component, while the variations would result from decorating the original.
I'm writing a simple slide program. In that program, I show small thumbnails of all slides in a new jpanel. In that new jpanel, after clicking 2 thumbnails, it should swap them. However, after swaping them, jpanel is not refreshing. If I create new jpanel by calling createAndShowGUI() function, it is ok, but then I need to close the old jpanel.
Below is my simple class,
http://docs.oracle.com/javase/tutorial/uiswing/examples/layout/GridLayoutDemoProject/src/layout/GridLayoutDemo.java
And in my main program if I click "slide sorter mode" button handler compile the following code
gridLayoutDemoObject.createAndShowGUI();
What is the solution and how should I make it ?
Thanks.
You didn't provide enough information to indicate how you display the images or how you swap them so its hard to give a proper answer.
I would display the images by using a JLabel (or JButton without a Border). Then when you can just use setIcon(...) to swap icons and the label will repaint itself.
Next time post an SSCCE that demonstrates the problem so we don't have to guess what you are talking about.