Im trying to add a Scroll bar to my JList. I want only 4 headings to be available on my JList at a time. When I add a JList and run my program, the JList disappears from the screen. Can someone help me fix this problem? I am using Java Eclipse.
This is my code:
songList1 = new JList (ListData);
songList1.setVisibleRowCount(4);
songList1.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
songList1.setBounds(300,100,100,200);
panel.add(new JScrollPane(songList1);
You can try one of the following:
set panel's layout or
set scroll panes bounding area by scrollPane.setBounds()
Just an oversight:
songList1 = new JList(ListData);
songList1.setVisibleRowCount(4);
songList1.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
JScrollPane songList1ScrollPane = new JScrollPane(songList1)
//songList1ScrollPane.setBounds(300, 100, 100, 200);
songList1ScrollPane.setBounds(10, 10, 100, 200);
panel.setLayout(null); // Absolute positioning
panel.add(songList1ScrollPane);
...
add(panel); // Or so
Of course the panel should have an appropiate layout.
Related
I want to add the possibility for my users to add a comment on a form. To display them, I created JPanel inside a simple JScrollPane. I set the layout of this JPanel to BoxLayout because I wish to add them all in only one column and it seemed to be the easiest way by calling BoxLayout.Y_AXIS in the constructor. I also tried GridLayout and GridBagLayout but it was not what I was looking for.
My problem is that when a JPanel has the BoxLayout layout, it's width automatically is the same as it's container, but my container is a JScrollPane and the caret hides the right side of my comment!
You can see the JTextField and a JButton on the bottom left, here's the code on the click event :
private void btnAjoutCommentaireActionPerformed(java.awt.event.ActionEvent evt) {
//I take the text from the JTextField and format it to html
String formattedComment = "<html><br><div style='width:280px;'>" +
txtArCommentaire.getText().replaceAll("\n", "<br>") +
"</div><br></html>";
JLabel label = new JLabel(formattedComment);
//I add a blue border
label.setBorder(new TitledBorder(new EtchedBorder(Color.lightGray, Color.blue), ConfigUser.getCu().toString()));
//this below doesn't work
label.setSize(280, 200);
//I tried adding a JPanel in between but it didn't really worked out
//JPanel panel = new JPanel();
//panel.setLayout(new GridLayout(1, 1));
//panel.setSize(297, 200);
//panel.add(label);
///pnlCommentaire is the JPanel inside the JScrollPane
pnlCommentaire.setLayout(new BoxLayout(pnlCommentaire, BoxLayout.Y_AXIS));
pnlCommentaire.add(label);
pnlCommentaire.revalidate();
pnlCommentaire.repaint();
}
As you can see I tried to adust the size in html using style='width:280px'and on the JLabel using label.setSize(280, 200); but none of them worked.
Do you have any idea on how I could resize this Jlabel?
EDIT :
I added a margin-right property to the div so that I can at least fully see the text in the JLabel but the right border is still hidden.
String formattedComment = "<html><br><div style='width:280px;margin-right:50px;'>" +
txtArCommentaire.getText().replaceAll("\n", "<br>") +
"</div><br></html>";
Everytime I click a button in my program, I add 5 new rows to the JPanel. Eventually, the rows overflow and I would like to add a JScrollPane so I can scroll down and see the new rows.
I know how to get it working for a TextArea but I can't seem to figure out how to make it work when I have a GridBagLayout. Below, I will attach the code for setting up my panels.
JPanel panelMain = new JPanel();
getContentPane().add(panelMain);
JPanel panelForm = new JPanel(new GridBagLayout());
panelMain.add(panelForm);
JScrollPane scrollpane = new JScrollPane(panelForm);
panelMain.add(scrollpane);
When I run, my code I get a box enclosing the GridBagLayout, but the scrollpane is nowhere to be seen.
JPanel panelMain = new JPanel();
getContentPane().add(panelMain);
You would typically add the scroll pane directly to the frame. There is no need for the "panelMain".
panelMain.add(panelForm);
That line is not needed. A component can only belong to a single parent. You later add "panelForm" to the scrollpane.
So the basic code would be:
JPanel panelForm = new JPanel(new GridBagLayout());
JScrollPane scrollpane = new JScrollPane(panelForm);
frame.add(scrollpane);
but the scrollpane is nowhere to be seen.
The scrollbars only appear when needed by default. Adding empty panels will not cause the scrollbar to be displayed. Click on your button a few times and add your child components to the "panelForm" using the appropriate GridBagConstraints. You will then need to use:
panelForm.revalidate();
panelForm.repaint();
The revalidate() causes the layout manager to be invoked so the scrollpane can determine is scrollbars are required or not.
I would like to achieve the below layout.
There are 6 panels. The 4 buttons at the top are one panel, and the 3 buttons at the right side of the image are also in one panel. Apart from those two there are 4 other panels as indicated by the borders. I tried the below code but displays everything in a scattered way.
mainPanel.add(topToolBarPanel,BorderLayout.PAGE_START);
mainPanel.add(lefsideToolBarPanel,BorderLayout.LINE_START);
mainPanel.add(descriptionPanel,BorderLayout.LEFT);
mainPanel.add(mapPanel,BorderLayout.CENTER);
mainPanel.add(propertiesPanel,BorderLayout.EAST);
mainPanel.add(tablePanel,BorderLayout.PAGE_END);
How can I achieve the design as shown in the image? I need all the panels to be arranged inside that mainPanel. I cannot use null layout though. Please advice.
After trashgod's answer :
JPanel gridPanel = new JPanel(new GridLayout(1, 0));
gridPanel.add(jInternalFrame1);
gridPanel.add(descriptionPanel);
mainPanel.add(gridPanel, BorderLayout.LINE_START);
mainPanel.add(topToolBarPanel,BorderLayout.PAGE_START);
mainPanel.add(tablePanel,BorderLayout.PAGE_END);
mainPanel.add(mapPanel,BorderLayout.CENTER);
mainPanel.add(PropertiesPanel,BorderLayout.LINE_END);
What I get :
Add lefsideToolBarPanel and descriptionPanel to a panel having GridLayout; add the new panel to the BorderLayout.
Panel p new Panel(new GridLayout(1, 0));
p.add(lefsideToolBarPanel);
p.add(descriptionPanel);
//mainPanel.add(lefsideToolBarPanel, BorderLayout.LINE_START);
//mainPanel.add(descriptionPanel, BorderLayout.LEFT);
mainPanel.add(p, BorderLayout.LINE_START);
There is no BorderLayout.LEFT. See also A Visual Guide to Layout Managers.
Addendum: Your updated question shows elements of topToolBarPanel, which should be added to PAGE_START, rather than LINE_START.
//mainPanel.add(topToolBarPanel,BorderLayout.LINE_START);
mainPanel.add(topToolBarPanel,BorderLayout. PAGE_START);
The width of the propertiesPanel and height of the tablePanel need to be increased. I used setSize()…
For the propertiesPanel, you can override getPreferredSize(), as discussed here. For the tablePanel, override getPreferredScrollableViewportSize() to customize the size of the table's enclosing JScrollPane, for example.
I suggest using a JLabel as your "layout" to use exact positioning of yout objects with setBounds(x, y, width, height). It would look similar to this :
JButton button = new JButton("Text or Image");
JLabel backgr = new JLabel();
JFrame frame = new JFrame("JLabel as Layout");
button.setBounds(100, 200, 340, 40);
backgr.add(button);
frame.add(backgr);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(600, 600);
frame.setLocation(40, 40);
frame.validate();
frame.setVisible(true);
I know that this is just a quick example for you, but I think it should do for explanation... so just add everything on the backgr JLabeland your good to go. Quick and dirty example but the a way to go.
Currently I have a nifty JViewport with a Jlabel set up and used as a view. I'm wondering if it's possible to use layered Jlabels as the Viewport's view. IE: I want to add new JLabels into a pre-existing Viewport.
Thanks!
EDIT: On StanislavL's advice, I'm now using a JLayeredPane within an JScrollPane. Currently there are two JLabels in the JLayeredPane, when I scroll the JScrollPane, the larger background image scrolls properly, by the smaller shipSprite remains in the same position. Any ideas how I can get them to scroll together?
public void initViewport() {
explorePort = new JScrollPane();
explorePort.setBounds(0, 0, retailWidth, retailHeight);
explorePort.setBackground(new Color(0, 100, 0));
explorePort.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
explorePort.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
ImageIcon background = Main.global.imgScaler.scaleImage(new ImageIcon("images/blankgrid.jpg"), retailWidth*2, retailHeight*2);
JLabel backSplash = new JLabel(background);
backSplash.setBounds(0, 0, retailWidth*2, retailHeight*2);
ImageIcon shipIcon = Main.global.imgScaler.scaleImage(new ImageIcon("images/ship.png"), Main.global.nodeWidth, Main.global.nodeHeight);
JLabel shipSprite = new JLabel(shipIcon);
shipSprite.setBounds(100, 100, Main.global.nodeWidth, Main.global.nodeHeight);
Main.global.gamePanel.add(backSplash, 0);
explorePort.setViewportView(backSplash);
Main.global.gamePanel.add(shipSprite, 1);
Main.global.gamePanel.add(explorePort, 2);
//explorePort.addMouseListener(this);
Main.global.gameFrame.addKeyListener(new ListenKey());
}
Use Layered pane to add multiple lables to container and place the container in JScrollPane
http://docs.oracle.com/javase/tutorial/uiswing/components/layeredpane.html
I have a really weird problem with a JScrollPane and a BorderLayout. For short explaination: i have a JTable which is inside the JScrollPane and this is with a JPanel and the JTableHeader on a JTabbedPane. Very Simple Layout. If i add just the JTable to my JPanel, the buttons are working. If i add the JScrollPane, the Buttons are not working anymore, so i cant click them! The ActionLister is never reached and i cant see the click-animation.
Some Sample code to explain:
d_pane = new JPanel();
d_button = new JPanel();
d_pane.add(table.getTableHeader(), BorderLayout.PAGE_START);
dl_scroll = new JScrollPane(table);
d_pane.add(dl_scroll, BorderLayout.CENTER);
// d_button is ridLayouted with 3 Buttons in there
d_pane.add(d_button, BorderLayout.PAGE_END);
1) The JScrollPane takes care of the table header itself. Don't add it to the pane.
2) the button does not seem to get the mouse events, probably because another component is above it - do you have other components/code in the setup?