Results 1 to 6 of 6
Thread: JList with ScrollBar
- 02-12-2010, 01:09 AM #1
Member
- Join Date
- Jan 2010
- Posts
- 81
- Rep Power
- 0
JList with ScrollBar
I have a frame with 4 panels. In one of the panels I have a JList that needs a scroll bar. I made a JScrollPane and added it to one of the panels. I then added the JList to the JScrollPane, but the scroll bar doesn't show up.
The list is inside the pane and the list shows up in the GUI, but the items in the list just go off the screen due to the lack of a scroll bar. I've tried messing around with setVisible, setOpaque, setVisibleRowCount, etc. and I still can't get it to work. The JList seems to be in the JScrollPane, but there isn't a scroll bar.
I'm not using a Layout Manager, could this be the problem? or is it something else?
Java Code:package game; import java.io.FileNotFoundException; import java.io.IOException; import java.awt.*; import javax.swing.*; import javax.swing.JFrame; import javax.swing.JList; import javax.swing.JScrollPane; public class deckEditorGUI extends javax.swing.JFrame { private static void initComponents() { String[] cardNames = {"Card 1","Card 2","Card 3","Card 4","Card 5","Card 6", "Card 7","Card 8","Card 9","Card 10","Card 11","Card 12", "Card 13","Card 14","Card 15","Card 16"}; // Frame JFrame deckEditorFrame = new JFrame("Deck Editor"); deckEditorFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); deckEditorFrame.setLayout(null); deckEditorFrame.setSize(1100, 700); deckEditorFrame.setVisible(true); // Panels final JPanel deckEditorPanelMain = new JPanel(); final JPanel deckEditorPanelCenter = new JPanel(); // Labels final JLabel deckEditorBuildLabel = new JLabel("Card in Deck", JLabel.CENTER); // Lists JList deckEditorBuildList = new JList(cardNames); // Scroll Panes JScrollPane deckEditorBuildScrollPane = new JScrollPane(deckEditorBuildList); // MAIN PANEL deckEditorFrame.add(deckEditorPanelMain); deckEditorPanelMain.setLayout(null); deckEditorPanelMain.setOpaque(true); deckEditorPanelMain.setVisible(true); deckEditorPanelMain.setBackground(new Color(0, 0, 0)); deckEditorPanelMain.setPreferredSize(new Dimension(1440, 900)); Dimension size = deckEditorPanelMain.getPreferredSize(); deckEditorPanelMain.setBounds(0, 0, size.width, size.height); // CENTER PANEL deckEditorPanelMain.add(deckEditorPanelCenter); deckEditorPanelCenter.setLayout(null); deckEditorPanelCenter.setOpaque(true); deckEditorPanelCenter.setVisible(true); deckEditorPanelCenter.setBackground(new Color(0, 0, 0)); deckEditorPanelCenter.setPreferredSize(new Dimension(752, 408)); size = deckEditorPanelCenter.getPreferredSize(); deckEditorPanelCenter.setBounds(180, 0, size.width, size.height); // CARDS IN DECK (Label) deckEditorPanelCenter.add(deckEditorBuildLabel); deckEditorBuildLabel.setLayout(null); deckEditorBuildLabel.setOpaque(true); deckEditorBuildLabel.setBackground(new Color(0, 0, 0)); deckEditorBuildLabel.setPreferredSize(new Dimension(200, 40)); size = deckEditorBuildLabel.getPreferredSize(); deckEditorBuildLabel.setBounds(10, 10, size.width, size.height); deckEditorBuildLabel.setFont(new Font("Tahoma", Font.BOLD, 20)); deckEditorBuildLabel.setForeground(new Color(255, 0, 0)); // CARDS IN DECK (Scroll Pane) deckEditorPanelCenter.add(deckEditorBuildScrollPane, BorderLayout.CENTER); deckEditorBuildScrollPane.setLayout(null); deckEditorBuildScrollPane.setOpaque(true); deckEditorBuildScrollPane.setVisible(true); deckEditorBuildScrollPane.setBackground(new Color(190, 0, 210)); deckEditorBuildScrollPane.setPreferredSize(new Dimension(180, 344)); size = deckEditorBuildScrollPane.getPreferredSize(); deckEditorBuildScrollPane.setBounds(20, 50, size.width, size.height); deckEditorBuildScrollPane.setFont(new Font("Tahoma", Font.PLAIN, 18)); deckEditorBuildScrollPane.setForeground(new Color(255, 204, 204)); // CARDS IN DECK (List) deckEditorBuildScrollPane.add(deckEditorBuildList); deckEditorBuildList.setLayout(null); deckEditorBuildList.setOpaque(true); deckEditorBuildList.setVisible(true); deckEditorBuildList.setVisibleRowCount(15); deckEditorBuildList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); deckEditorBuildList.setBackground(new Color(190, 200, 210)); deckEditorBuildList.setPreferredSize(new Dimension(180, 344)); size = deckEditorBuildList.getPreferredSize(); deckEditorBuildList.setBounds(0, 0, size.width, size.height); deckEditorBuildList.setFont(new Font("Tahoma", Font.PLAIN, 16)); deckEditorBuildList.setForeground(new Color(0, 0, 0)); } public static void main(String[] args) throws FileNotFoundException, IOException { javax.swing.SwingUtilities.invokeLater(new Runnable() { public void run() { initComponents(); } }); } }
-
1) Don't set a jscrollpane's layout to null. In fact, none of your layouts above should be null, but especially the scrollpane as it then stops working (I am curious as to why you did this).
2) Don't add a component directly to the jscrollpane but instead to its JViewport. You can do this one of two ways, either via the JScrollPane's constructor (which you do), or via the getViewport() method.
- 02-12-2010, 03:21 AM #3
Member
- Join Date
- Jan 2010
- Posts
- 81
- Rep Power
- 0
Well, I started building my GUI by using NetBeans Form Editor to build it. I was actually very annoyed by the snap-to-grid option and the automatic alignment, and after getting some advice from people on here, decided to start over from scratch.
Initially, I tried using BoxLayout, but in one of my first attempts I ended up with some things where I didn't want them. I had little patience at the time. I knew exactly what I wanted and where I wanted it (down to the pixel), so I decided to try Doing Without a Layout Manager (Absolute Positioning), even though the tutorial kind of warned against it.
Up until now, it's worked like a charm. My UI has over 100 components and they always end up in the correct position. If you only knew how many times I wanted to throw my monitor out the window because of NetBeans Form Editor, than my reasoning for trying to do it without a Layout Manager might seem less insane. I can see some of the issues I'm going to run into are not having default layouts for some components. I think I'm ready to try a layout manager again and was thinking about using GridBag.Last edited by Psyclone; 02-12-2010 at 03:26 AM.
-
If it were me, I'd not start out with GridBagLayout as it is a bit touchy and not the easiest one to use, especially starting out. You're far better off creating a GUI that nests several different JPanels each using a simpler layout manager than GridBagLayout. For instance, the main JPanel that holds everything else could use BorderaLayout, a radiobutton-holding JPanel could use GridLayout and be added to the main JPanel in the BorderLayout.EAST position, a status JPanel could us BoxLayout and be added to the main JPanel BorderLayout.SOUTH, etc...
-
Also, what is going to happen if in a week you want to add one more JRadioButton to the JPanel that happens to be near the top of your GUI? I'll tell you: you're going to have to manually reposition every component that is below and right of the new JRadioButton. Or what if you want the GUI to be displayed in a different L&F or on a different platform and then find out that labels run into other components, and some components are now not showing?
If on the other hand you used nested layouts, all you'd have to do would be to add the radiobutton to the correct panel, call pack() on the JFrame, and let your layout managers all do the heavy lifting for you. Ditto on the changing the L&F or seeing your app on a different platform. The layout managers make it all magically work, and simply too.
- 02-12-2010, 04:02 AM #6
Member
- Join Date
- Jan 2010
- Posts
- 81
- Rep Power
- 0
Similar Threads
-
scrollbar issue
By kumar_gemi in forum New To JavaReplies: 9Last Post: 05-06-2009, 06:57 AM -
scrollbar issue
By kumar_gemi in forum NetBeansReplies: 1Last Post: 05-03-2009, 10:23 PM -
Not another Jpanel Scrollbar problem!
By jiexx in forum AWT / SwingReplies: 3Last Post: 03-18-2009, 02:09 AM -
how to add scrollbar in netbeans
By kumar_gemi in forum NetBeansReplies: 1Last Post: 03-12-2009, 06:50 PM -
Using java.awt.Scrollbar
By Java Tip in forum Java TipReplies: 0Last Post: 01-03-2008, 09:19 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks