I currently have a JFrame to start fullscreen, inside this jframe i have a jpanel, this jpanel includes a vertical scrollpane. Now if i resize my jframe vertically it just kinda removes the bottom part of the jpanel. Is there any way to just shrink the jscrollpane.
im currently using flowlayout for the jframe,
Scrollbar appear automatically when the preferred size of the components added to the scroll pane area greater than the size of the scroll pane.
The FlowLayout will wrap components to a new row, but it always gives the preferred size as the size required to fit the components on a single row, so the preferred height will never change.
To solve this problem you can use the Wrap Layout which simple extend FlowLayout to recalculate the preferred size when wrapping occurs.
The JPanel consists of 3 other panels, a top panel, a scrollpane in the middle and a botpanel. The top and bot panel are just button and checkboxes and stuff
private void initPane() {
createFolderCompPanel();
createBotPanel();
createTopPanel();
createScrollPane();
createTotalPanel();
add(totalPanel);
}
private void createFolderCompPanel() {
//Create folderCompPanel
folderCompPanel = new JPanel();
folderCompPanel.setLayout(new BoxLayout(folderCompPanel, BoxLayout.Y_AXIS));
folderCompPanel.add(Box.createVerticalGlue());
}
private void createTotalPanel() {
//Create TotalPanel
totalPanel = new JPanel();
totalPanel.setLayout(new BoxLayout(totalPanel, BoxLayout.Y_AXIS));
totalPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
totalPanel.add(topPanel);
totalPanel.add(scrollPane);
totalPanel.add(botPanel);
}
private void createScrollPane() {
//Create ScrollPane
scrollPane = new JScrollPane();
scrollPane.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
scrollPane.setViewportBorder(BorderFactory.createEtchedBorder());
scrollPane.getVerticalScrollBar().setUnitIncrement(6);
}
private void createBotPanel() {
//Create BotPanel
botPanel = new JPanel();
botPanel.setLayout(new BoxLayout(botPanel, BoxLayout.X_AXIS));
//AddButton
addButton = new JButton("Add");
addButton.setEnabled(false);
addButton.addActionListener(this);
//SaveButton
saveButton = new JButton("Save");
saveButton.setEnabled(false);
saveButton.addActionListener(this);
//CancelButton
cancelButton = new JButton("Cancel");
cancelButton.setEnabled(false);
cancelButton.addActionListener(this);
lblTotalLength = new JLabel("Total Length: " + totalLength);
botPanel.add(Box.createRigidArea(new Dimension(10, 0)));
botPanel.add(addButton);
botPanel.add(Box.createRigidArea(new Dimension(10, 0)));
botPanel.add(lblTotalLength);
botPanel.add(Box.createHorizontalGlue());
botPanel.add(saveButton);
botPanel.add(Box.createRigidArea(new Dimension(10, 0)));
botPanel.add(cancelButton);
botPanel.add(Box.createRigidArea(new Dimension(10, 0)));
}
private void createTopPanel() {
//Create TopPanel
topPanel = new JPanel();
topPanel.setLayout(new BoxLayout(topPanel, BoxLayout.X_AXIS));
//create deletedisplay button
deleteDisplayButton = new JButton("Delete Display");
deleteDisplayButton.addActionListener(this);
deleteDisplayButton.setEnabled(false);
//create displaybox
displayBox = new JComboBox();
displayBox.addActionListener(this);
displayBox.addItem("<None>");
for (String s : connect.getAllDisplays()) {
displayBox.addItem(s);
}
displayBox.setMaximumSize(displayBox.getPreferredSize());
//create newdisplay button
newDisplayButton = new JButton("New Display");
newDisplayButton.addActionListener(this);
topPanel.add(Box.createRigidArea(new Dimension(10, 0)));
topPanel.add(displayBox);
topPanel.add(Box.createHorizontalGlue());
topPanel.add(newDisplayButton);
topPanel.add(Box.createRigidArea(new Dimension(5, 0)));
topPanel.add(deleteDisplayButton);
topPanel.add(Box.createRigidArea(new Dimension(10, 0)));
}
this is the panel i add to the jframe
public GuiConstructor(){
super(APPLICATION_NAME);
this.setExtendedState(JFrame.MAXIMIZED_BOTH);
this.setMinimumSize(new Dimension(630, 600));
setLayout(new FlowLayout(FlowLayout.LEFT));
LoopControlWindow folderSearch = new LoopControlWindow(connect, this);
add(folderSearch);
pack();
setLocationRelativeTo(null);
setResizable(true);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
Related
I want to be able to scroll down a dynamically generated list of movies. I tried adding a Scrollpane.
I have a navigation bar at the page start and in the center a jpanel with all the movies.
You can recreate this example by using this code:
private static JFrame frame;
public static void main(String[] args) throws HeadlessException {
frame = new JFrame();
frame.setLayout(new BorderLayout());
frame.setBackground(new Color(32, 32, 32));
JPanel navigationPanel = createNavigationBar();
frame.add(navigationPanel, BorderLayout.PAGE_START);
JPanel moviePanel = createMoviePanel();
frame.add(moviePanel, BorderLayout.CENTER);
frame.setPreferredSize(new Dimension(1920, 1080));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("Example App");
frame.pack();
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
frame.setVisible(true);
}
// MoviePanel Class
public static JPanel createMoviePanel() {
JPanel parentMoviePanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 0, 25));
JPanel contentPanel = new JPanel(new GridLayout(0, 1));
JPanel moviePanel = new JPanel(new GridLayout(0, 9, 8, 5));
JScrollPane scrollPane = new JScrollPane(moviePanel);
scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
scrollPane.getVerticalScrollBar().setPreferredSize(new Dimension(0, 0));
scrollPane.setBorder(new EmptyBorder(0, 0, 0, 0));
parentMoviePanel.setBackground(new Color(32, 32, 32));
contentPanel.setBackground(new Color(32, 32, 32));
moviePanel.setBackground(new Color(32, 32, 32));
final File root = new File("");
for (int i = 0; i < 70; i++) {
// Get the image and scale it down
BufferedImage movieCover=new BufferedImage(150,200,BufferedImage.TYPE_INT_RGB);
Graphics2D g2d=(Graphics2D)movieCover.getGraphics();
g2d.setColor(Color.GRAY);
g2d.fillRect(0,0,movieCover.getWidth(),movieCover.getHeight());
// Create button and change settings
JButton movieButton = new JButton("Movie " + i, new ImageIcon(movieCover));
movieButton.setMargin(new Insets(0, 0, 0, 0));
movieButton.setPreferredSize(new Dimension(180, 250));
movieButton.setForeground(Color.WHITE);
movieButton.setContentAreaFilled(false);
movieButton.setBorderPainted(false);
movieButton.setFocusPainted(false);
movieButton.setHorizontalTextPosition(JButton.CENTER);
movieButton.setVerticalTextPosition(JButton.BOTTOM);
moviePanel.add(movieButton);
scrollPane.revalidate();
}
contentPanel.add(moviePanel);
contentPanel.add(scrollPane);
parentMoviePanel.add(contentPanel);
return parentMoviePanel;
}
// Navbar Class
public static JPanel createNavigationBar() {
JPanel navBar = new JPanel();
navBar.setLayout(new FlowLayout(FlowLayout.LEFT, 30, 20));
navBar.setBackground(new Color(25, 25, 25));
JButton homeButton = new JButton("Home");
homeButton.setContentAreaFilled(false);
homeButton.setBorderPainted(false);
homeButton.setFocusPainted(false);
JButton movieButton = new JButton("Movies");
movieButton.setContentAreaFilled(false);
movieButton.setBorderPainted(false);
movieButton.setFocusPainted(false);
// Add all the buttons to the navbar
navBar.add(homeButton);
navBar.add(movieButton);
return navBar;
}
What I'm trying to do is to scroll down this list of movies, using my mouse wheel without seeing any kind of scrollbar. It should look exactly how it looks now, but I want to be able to scroll down and see all the movies.
I don't know why it isn't working that's why I'm asking here in hope someone can explain to me what I'm doing wrong.
Your usage of a scroll pane is incorrect.
A Swing component can only have a single parent. The following code is creating the scroll pane with a child component. However you then remove the moviePanel from the scroll pane when you add it to the content pane.
So the scroll pane has no child component and will never work.
JScrollPane scrollPane = new JScrollPane(moviePanel);
...
contentPanel.add(moviePanel);
contentPanel.add(scrollPane);
However, even that won't solve your problem because you are now using a FlowLayout on your top level panel so all the child components will always be displayed at their preferred size so there is no need for scroll bars.
Get rid of all the scroll pane logic in your createMoviePanel() method.
Instead you probably want to add the scroll pane to the content pane:
//frame.add(moviePanel, BorderLayout.CENTER);
frame.add(new JScrollPane(moviePanel), BorderLayout.CENTER);
Now the scroll pane will dynamically resize as the frame size changes. Scrollbars will then appear as required.
I want to align Labels and a Panel (containing Buttons) to the left inside a vertical BoxLayout.
As long as I don't add the panel to the BoxLayout everything is aligned to the left perfectly, but adding it screws everything up.
import javax.swing.*;
import java.awt.*;
public class BoxLayoutDemo{
public static void main(String[] args) {
JFrame frame = new JFrame();
JPanel panel = new JPanel();
JPanel five = new JPanel();
JButton plus = new JButton("+");
JButton minus = new JButton("-");
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
Font testFont = new Font("Arial", Font.BOLD, 20);
JLabel label1 = new JLabel("Label1");
label1.setFont(testFont);
JLabel label2 = new JLabel("Label2");
label2.setFont(testFont);
five.setLayout(new BoxLayout(five, BoxLayout.X_AXIS));
plus.setMinimumSize(new Dimension(30, 30));
plus.setMaximumSize(new Dimension(30, 30));
minus.setMinimumSize(new Dimension(30, 30));
minus.setMaximumSize(new Dimension(30, 30));
five.add(plus);
five.add(minus);
panel.add(label1);
panel.add(five);
panel.add(label2);
panel.setOpaque(true);
panel.setBackground(Color.red);
frame.setMinimumSize(new Dimension(200, 200));
frame.getContentPane().add(panel, BorderLayout.EAST);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
}
Your components need to have the same "x alignment":
label1.setAlignmentX(Component.LEFT_ALIGNMENT);
label2.setAlignmentX(Component.LEFT_ALIGNMENT);
five.setAlignmentX(Component.LEFT_ALIGNMENT);
Read the section from the Swing tutorial on Fixing Alignment Problems for more information.
You can use invisible components as fillers.
private static Box leftAlignedInHorizontalBox(Component component) {
Box box = Box.createHorizontalBox();
box.add(component);
box.add(Box.createHorizontalGlue());
return box;
}
and then:
panel.add(leftAlignedInHorizontalBox(label1));
I have following source code...Can someone please give me an advice how to add jscrollpane onto jframe? I tried several time to add it to jframe but without any progress. It is not even showing.
public class Form3 {
JFrame jframe = new JFrame("Etiket print.");
JPanel panel1 = new JPanel();
JPanel panel2 = new JPanel();
JPanel panel3 = new JPanel();
JPanel panel4 = new JPanel();
JScrollPane scrollFrame = new JScrollPane(panel2);
Color myBlue1Color = new Color(168, 175, 247);
Color myBlue2Color = new Color(139, 146, 255);
public Form3(){
jframe.setMinimumSize(new Dimension(1280, 1000));
panel2.setLayout(new BoxLayout(panel2, BoxLayout.Y_AXIS));
panel2.setAutoscrolls(true);
jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//---------------------------------Header
panel1 = createSquareJPanel(Color.YELLOW, 600,200);
panel3 = createSquareJPanel(Color.GREEN, 400,200);
panel4 = createSquareJPanel(Color.white, 280,200);
JPanel container = new JPanel();
JPanel container1 = new JPanel();
JPanel container2 = new JPanel();
container.setLayout(new BoxLayout(container, BoxLayout.Y_AXIS));
container1.setLayout(new BoxLayout(container1, BoxLayout.Y_AXIS));
container2.setLayout(new BoxLayout(container2, BoxLayout.X_AXIS));
container1.add(panel1);
container2.add(container1);
container2.add(panel3);
container2.add(panel4);
container.add(container2);
container.add(panel2);
{
for (int i=0; i<25; i++){
JPanel harnessPanel= new JPanel();
harnessPanel.setMinimumSize(new Dimension(1280, 70));
harnessPanel.setMaximumSize(new Dimension(1280, 70));
harnessPanel.setPreferredSize(new Dimension(1280, 70));
if(i%2==0) {
harnessPanel.setBackground(myBlue1Color);
}
else {
harnessPanel.setBackground(myBlue2Color);
}
panel2.add(harnessPanel);
harnessPanel.repaint();
harnessPanel.validate();
}
panel2.repaint();
panel2.validate();
}
jframe.add(scrollFrame);
jframe.add(container);
jframe.pack();
jframe.setLocationRelativeTo(null);
jframe.setVisible(true);
}
private JPanel createSquareJPanel(Color color, int size1, int size2)
{
JPanel tempPanel = new JPanel();
tempPanel.setBackground(color);
tempPanel.setMinimumSize(new Dimension(size1, size2));
tempPanel.setMaximumSize(new Dimension(size1, size2));
tempPanel.setPreferredSize(new Dimension(size1, size2));
return tempPanel;
}
public static void main (String args[]){
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
Form3 myF=new Form3();
}
});
};
}
picture of my app:
actual state:
JFrame uses a BorderLayout by default. It's default position (if you don't specify one) is CENTER.
BorderLayout will only allow one component to occupy any of it's 5 available positions.
So when you do...
jframe.add(scrollFrame);
jframe.add(container);
It adds the scrollFrame to the center position and effectively removes it when you add container (it doesn't actually remove it, but the result is just about the same).
Try supplying a position constraint. For example...
jframe.add(scrollFrame);
jframe.add(container, BorderLayout.SOUTH);
See How to use BorderLayout for more details
I have following source code...Can someone please give me an advice how to add jscrollpane onto jframe? I tried several time to add it to jframe but without any progress. It is not even showing.
public class Form3 {
JFrame jframe = new JFrame("Etiket print.");
JPanel panel1 = new JPanel();
JPanel panel2 = new JPanel();
JPanel panel3 = new JPanel();
JPanel panel4 = new JPanel();
JScrollPane scrollFrame = new JScrollPane(panel2);
Color myBlue1Color = new Color(168, 175, 247);
Color myBlue2Color = new Color(139, 146, 255);
public Form3(){
jframe.setMinimumSize(new Dimension(1280, 1000));
panel2.setLayout(new BoxLayout(panel2, BoxLayout.Y_AXIS));
panel2.setAutoscrolls(true);
jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//---------------------------------Header
panel1 = createSquareJPanel(Color.YELLOW, 600,200);
panel3 = createSquareJPanel(Color.GREEN, 400,200);
panel4 = createSquareJPanel(Color.white, 280,200);
JPanel container = new JPanel();
JPanel container1 = new JPanel();
JPanel container2 = new JPanel();
container.setLayout(new BoxLayout(container, BoxLayout.Y_AXIS));
container1.setLayout(new BoxLayout(container1, BoxLayout.Y_AXIS));
container2.setLayout(new BoxLayout(container2, BoxLayout.X_AXIS));
container1.add(panel1);
container2.add(container1);
container2.add(panel3);
container2.add(panel4);
container.add(container2);
container.add(panel2);
{
for (int i=0; i<25; i++){
JPanel harnessPanel= new JPanel();
harnessPanel.setMinimumSize(new Dimension(1280, 70));
harnessPanel.setMaximumSize(new Dimension(1280, 70));
harnessPanel.setPreferredSize(new Dimension(1280, 70));
if(i%2==0) {
harnessPanel.setBackground(myBlue1Color);
}
else {
harnessPanel.setBackground(myBlue2Color);
}
panel2.add(harnessPanel);
harnessPanel.repaint();
harnessPanel.validate();
}
panel2.repaint();
panel2.validate();
}
jframe.add(scrollFrame);
jframe.add(container);
jframe.pack();
jframe.setLocationRelativeTo(null);
jframe.setVisible(true);
}
private JPanel createSquareJPanel(Color color, int size1, int size2)
{
JPanel tempPanel = new JPanel();
tempPanel.setBackground(color);
tempPanel.setMinimumSize(new Dimension(size1, size2));
tempPanel.setMaximumSize(new Dimension(size1, size2));
tempPanel.setPreferredSize(new Dimension(size1, size2));
return tempPanel;
}
public static void main (String args[]){
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
Form3 myF=new Form3();
}
});
};
}
picture of my app:
actual state:
JFrame uses a BorderLayout by default. It's default position (if you don't specify one) is CENTER.
BorderLayout will only allow one component to occupy any of it's 5 available positions.
So when you do...
jframe.add(scrollFrame);
jframe.add(container);
It adds the scrollFrame to the center position and effectively removes it when you add container (it doesn't actually remove it, but the result is just about the same).
Try supplying a position constraint. For example...
jframe.add(scrollFrame);
jframe.add(container, BorderLayout.SOUTH);
See How to use BorderLayout for more details
public class MainMenu extends JFrame {
private JPanel panel,file_list_panel;
private JPanel contentPane;
private JMenuBar menuBar;
private JMenu mnNewMenu1,mnNewMenu2,mnNewMenu3;
private JMenuItem mt1,mt2,mt3;
private JPanel right,left ,bottom;
private JSplitPane spver ,sphor;
private JTabbedPane tabbedPane;
private JLabel label;
private JList<String> list_1;
private JScrollPane jscroll_list;
private DefaultListModel listmodel_1 = new DefaultListModel();
public MainMenu() {
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
setBounds(0,0,screenSize.width, screenSize.height);
setExtendedState(getExtendedState() | JFrame.MAXIMIZED_BOTH);
int intValue = Integer.parseInt( "EEEEEE",16);
Color aColor = new Color( intValue );
UIManager.put("TabbedPane.background", new Color(230, 216, 174));
UIManager.put("TabbedPane.selected", Color.WHITE);
UIManager.put("TabbedPane.contentAreaColor",aColor );
UIManager.put("TabbedPane.focus", Color.WHITE);
setTitle("Main Menu");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//setBounds(100, 100, 848, 680);
//setLocation(10, 10);
setLocationRelativeTo(null); // set jFrame alignment to center
//parent(first) panel
contentPane = new JPanel();
contentPane.setBackground(Color.BLACK);
contentPane.setBorder(new EmptyBorder(2, 2, 2, 2));
setContentPane(contentPane);
contentPane.setLayout(new BorderLayout(0, 0));
//Main Menu BAR
menuBar = new JMenuBar();
menuBar.setFont(new Font("Tekton Pro Ext", Font.PLAIN, 11));
setJMenuBar(menuBar);
//Menu 1
mnNewMenu1 = new JMenu("New menu");
menuBar.add(mnNewMenu1);
//Menu 1 MenuItem 1
mt1 = new JMenuItem("Browse New Project");
mt1.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent arg0) {
BrowseScreen frame = new BrowseScreen();
frame.setVisible(true);
}
});
mnNewMenu1.add(mt1);
//second panel
panel = new JPanel();
contentPane.add(panel, BorderLayout.CENTER);
panel.setLayout(new BorderLayout(0, 0));
spver =new JSplitPane(JSplitPane.VERTICAL_SPLIT);
spver.setDividerLocation(500);
spver.setEnabled(false);
bottom=new JPanel();
sphor =new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
sphor.setEnabled(false);
spver.setTopComponent(sphor);
spver.setBottomComponent(bottom);
bottom.setLayout(null);
panel.add(spver);
sphor.setDividerLocation(180);
left =new JPanel();
right =new JPanel();
sphor.setRightComponent(right);
right.setLayout(new GridLayout(0, 1, 0, 0));
tabbedPane = new JTabbedPane(JTabbedPane.TOP);
tabbedPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
JPanel tab1 = new JPanel();
tabbedPane.addTab("fffa", tab1);
tabbedPane.setMnemonicAt(0, KeyEvent.VK_1);
JPanel tab2 = new JPanel();
tabbedPane.addTab("TAB1", tab2);
tabbedPane.setMnemonicAt(0, KeyEvent.VK_1);
right.add(tabbedPane);
sphor.setLeftComponent(left);
left.setLayout(new BorderLayout(0, 0));
file_list_panel= new JPanel();
file_list_panel.setBackground(Color.BLACK);
file_list_panel.setForeground(Color.WHITE);
label = new JLabel("Java Files of Project");
label.setBackground(Color.BLACK);
label.setForeground(Color.WHITE);
label.setFont(new Font("Garamond", Font.BOLD, 14));
file_list_panel.add(label);
left.add(file_list_panel, BorderLayout.NORTH);
list_1 = new JList<String>(listmodel_1);
jscroll_list = new JScrollPane(list_1);
left.add(jscroll_list, BorderLayout.CENTER);
}
public void setList(Vector<String> files){
listmodel_1.removeAllElements();
list_1.removeAll();
for(int i=0;i<files.size();i++)
listmodel_1. addElement(files.elementAt(i));
list_1.setModel(listmodel_1);
this.invalidate();
this.validate();
this.repaint();
}
}
setList is called from the browse window before it calls setVisible(false);
ie . this methods gets called when the browse window disappears .. it does all the things in method but does not update it in the MainMenu
public void setFileList(){
MainMenu mm= new MainMenu();
mm.setList(java_files);
}
list_1 -JList listmodel_1=DefaultListModel
i've tried to refreshing the frame but does not refresh after adding the new list to the Main Window ...
before JList is updated im browsing the the files in another window then it is set to setVisible(false) then MainMenu gets focused and calls the setList method but its not changing
You're creating a new JList<String> and copying the reference to your instance variable - but you're neither removing the old JList from the frame, nor adding the new one. Just changing the variable won't do that.
Rather than creating a new JList<String>, why don't you just replace the model of the existing one? Remove this line:
list_1=new JList<String>(listmodel_1);
and you may find it just works. (You're already setting the model in the subsequent line...)
Basically, you are creating a new JList and associating your model with it, but this has no effect on the JList that is on the screen
public void setList(Vector<String> files){
// Good...
listmodel_1.removeAllElements();
// Not required, has nothing to do with the items in the list
//list_1.removeAll();
// Good
for(int i=0;i<files.size();i++)
listmodel_1. addElement(files.elementAt(i));
// This here is your problem
//list_1=new JList<String>(listmodel_1);
// Because I have no idea what model list_1 is actually using...
list_1.setModel(listmodel_1);
//list_1.setSelectedIndex(0);
//this.invalidate();
//this.validate();
//this.repaint();
}