Trouble with scroll pane with nested layouts - java

I am having trouble adding a scroll pane to a nested panel. Here is what I have:
public class board {
public static void addComponentsToPane(Container pane) {
pane.setLayout(new GridLayout(1, 0));
JPanel left = new JPanel();
pane.add(left);
left.setLayout(new BoxLayout(left, BoxLayout.Y_AXIS));
JPanel leftTop = new JPanel();
leftTop.setPreferredSize(new Dimension(266, 300));
leftTop.setBackground(Color.black);
left.add(leftTop);
JScrollPane scrollPane = new JScrollPane(leftTop); //problem is here
left.add(scrollPane);
JButton jb = new JButton();
jb.setPreferredSize(new Dimension(250,50));
leftTop.add(jb);
JButton jb1 = new JButton();
jb1.setPreferredSize(new Dimension(250,50));
leftTop.add(jb1);
JButton jb2 = new JButton();
jb2.setPreferredSize(new Dimension(250,50));
leftTop.add(jb2);
JButton jb3 = new JButton();
jb3.setPreferredSize(new Dimension(250,50));
leftTop.add(jb3);
JButton jb4 = new JButton();
jb4.setPreferredSize(new Dimension(250,50));
leftTop.add(jb4);
JButton jb5 = new JButton();
jb5.setPreferredSize(new Dimension(250,50));
leftTop.add(jb5);
JButton jb6 = new JButton();
jb6.setPreferredSize(new Dimension(250,50));
leftTop.add(jb6);
JPanel leftBottom = new JPanel();
leftBottom.setPreferredSize(new Dimension(266, 300));
leftBottom.setBackground(Color.red);
left.add(leftBottom);
JPanel middle = new JPanel();
pane.add(middle);
middle.setLayout(new BoxLayout(middle, BoxLayout.Y_AXIS));
JPanel middleTop = new JPanel();
middleTop.setPreferredSize(new Dimension(266, 200));
middleTop.setBackground(Color.green);
middle.add(middleTop);
JPanel middleBottom = new JPanel();
middleBottom.setPreferredSize(new Dimension(266, 400));
middleBottom.setBackground(Color.yellow);
middle.add(middleBottom);
JPanel right = new JPanel();
right.setPreferredSize(new Dimension(266, 600));
right.setBackground(Color.blue);
pane.add(right);
}
private static void createAndShowGUI() {
JFrame frame = new JFrame("GridBagLayoutDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
addComponentsToPane(frame.getContentPane());
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
createAndShowGUI();
}
});
}
}
I am just messing around with JPanels and I cannot seem to add scrolling to the left top pane. I think i initialized scrollPane right, but am I adding it to the wrong pane?

Your initial problem is here
leftTop.setPreferredSize(new Dimension(266, 300));
This is overriding what the layout manager (FlowLayout in this case) would otherwise provide to the JScrollPane in order for it to know how to manage the view (when to show the scrollbars for instance)
The next problem you will have is, FlowLayout won't do what you want it to. Instead you might want to use GridLayout or maybe GridBagLayout instead
JPanel leftTop = new JPanel(new GridBagLayout());
//leftTop.setPreferredSize(new Dimension(266, 300));
leftTop.setBackground(Color.black);
JScrollPane scrollPane = new JScrollPane(leftTop); //problem is here
left.add(scrollPane);
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.weightx = 1;
gbc.insets = new Insets(5, 10, 5, 10);
JButton jb = new JButton();
jb.setPreferredSize(new Dimension(250, 50));
leftTop.add(jb, gbc);
JButton jb1 = new JButton();
jb1.setPreferredSize(new Dimension(250, 50));
leftTop.add(jb1, gbc);
JButton jb2 = new JButton();
jb2.setPreferredSize(new Dimension(250, 50));
leftTop.add(jb2, gbc);
JButton jb3 = new JButton();
jb3.setPreferredSize(new Dimension(250, 50));
leftTop.add(jb3, gbc);
JButton jb4 = new JButton();
jb4.setPreferredSize(new Dimension(250, 50));
leftTop.add(jb4, gbc);
JButton jb5 = new JButton();
jb5.setPreferredSize(new Dimension(250, 50));
leftTop.add(jb5, gbc);
JButton jb6 = new JButton();
jb6.setPreferredSize(new Dimension(250, 50));
leftTop.add(jb6, gbc);
Know, if that's not meeting your needs, you will need to create a custom component which implements Scrollable, which will allow you to specify PreferredScrollableViewportSize which will tell the JScrollPane what the preferred size of the viewable area should be, rather then using the preferredSize of the view

Related

Borderlayout Gridlayout

public class Editor{
public static void main(String[] args) {
JFrame f = new JFrame("Editpr");
f.setLayout(new GridLayout(5, 2, 25, 54));
JButton button1 = new JButton("1");
JButton button1 = new JButton("10");
JTextArea ausgabe = new JTextArea();
ausgabe.setText("Text");
ausgabe.setEditable(false);
f.add(ausgabe);
f.add(button1);
f.add(Button2)
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(550, 550);
f.setVisible(true);
}
I need help with this Code.
I want this is the grid layout in a Border Layout . The buttons to the center. Textarea should be in BorderLayout below. Who can help a newbie.
Here is an example of a panel with GridLayout, at the center of a panel with BorderLayout , and a text area to the south.
Note that I added random buttons to fill the grid, since your GridLayout has 5 rows and 2 columns (You probably planned to add some more components).
public static void main(final String[] args) {
JFrame f = new JFrame("Editpr");
JPanel content = new JPanel();
content.setLayout(new BorderLayout());
JPanel buttonsPanel = new JPanel();
buttonsPanel.setLayout(new GridLayout(5, 2, 25, 54));
JButton button1 = new JButton("1");
JButton button2 = new JButton("2");
buttonsPanel.add(button1);
buttonsPanel.add(button2);
// random filling to demonstrate the result of the filled grid
buttonsPanel.add(new JButton("3"));
buttonsPanel.add(new JButton("4"));
buttonsPanel.add(new JButton("5"));
buttonsPanel.add(new JButton("6"));
buttonsPanel.add(new JButton("7"));
buttonsPanel.add(new JButton("8"));
buttonsPanel.add(new JButton("9"));
buttonsPanel.add(new JButton("10"));
JTextArea ausgabe = new JTextArea();
ausgabe.setText("Text");
ausgabe.setEditable(false);
content.add(buttonsPanel, BorderLayout.CENTER);
content.add(ausgabe, BorderLayout.SOUTH);
f.setContentPane(content);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(550, 550);
f.setVisible(true);
}

How is it possible to add a container for border layout?

I want to add labels and buttons above and below the border layout. How can I do that? Here is what I did:
import java.awt.*;
import javax.swing.*;
class homework{
public static void main(String[] args) {
JFrame frame= new JFrame("border layout");
frame.setVisible(true);
JLabel label=new JLabel("Container of BorderLayout");
JButton button1 = new JButton("NORTH");
JButton button2 = new JButton("SOUTH");
JButton button3 = new JButton("EAST");
JButton button5 = new JButton("CENTER");
JButton button4 = new JButton("WEST");
JPanel panel1=new JPanel();
JPanel panel2=new JPanel();
frame.add(panel2);
label.setLayout(new FlowLayout(0));
panel2.add(label);
panel1.setLayout(new BorderLayout());
panel1.add(button1,BorderLayout.NORTH);
panel1.add(button2,BorderLayout.SOUTH);
panel1.add(button3,BorderLayout.EAST);
panel1.add(button4,BorderLayout.WEST);
panel1.add(button5,BorderLayout.CENTER);
frame.add(panel1);
frame.pack();
}
}
Above and below of border layout, set new 2 containers (for example JPanel) and make them flow layout. enter image description here
JPanel panel = new JPanel();
frame.getContentPane().add(panel, BorderLayout.NORTH);
JRadioButton rdbtnNewRadioButton = new JRadioButton("New radio button");
panel.add(rdbtnNewRadioButton);
JRadioButton rdbtnNewRadioButton_1 = new JRadioButton("New radio button");
panel.add(rdbtnNewRadioButton_1);
JPanel panel_1 = new JPanel();
frame.getContentPane().add(panel_1, BorderLayout.SOUTH);
JLabel lblNewLabel = new JLabel("New label");
panel_1.add(lblNewLabel);
JLabel lblNewLabel_1 = new JLabel("New label");
panel_1.add(lblNewLabel_1);
add something like that before frame.pack(); code.
Actually what do you mean by above and below? Do you mean north and south? If It is you should have something like this enter image description here
and you should write code this way
import java.awt.*;
class homework{
public static void main(String[] args) {
JFrame frame= new JFrame("border layout");
frame.setVisible(true);
JLabel label=new JLabel("Container of BorderLayout");
JButton button3 = new JButton("EAST");
JButton button5 = new JButton("CENTER");
JButton button4 = new JButton("WEST");
JPanel panel1=new JPanel();
JPanel panel2=new JPanel();
frame.getContentPane().add(panel2);
label.setLayout(new FlowLayout(0));
panel2.add(label);
panel1.setLayout(new BorderLayout());
panel1.add(button3,BorderLayout.EAST);
panel1.add(button4,BorderLayout.WEST);
panel1.add(button5,BorderLayout.CENTER);
frame.getContentPane().add(panel1);
JPanel panel = new JPanel();
panel1.add(panel, BorderLayout.NORTH);
JLabel lblNewLabel = new JLabel("New label");
panel.add(lblNewLabel);
JRadioButton rdbtnNewRadioButton = new JRadioButton("New radio button");
panel.add(rdbtnNewRadioButton);
JPanel panel_1 = new JPanel();
panel1.add(panel_1, BorderLayout.SOUTH);
JLabel lblNewLabel_1 = new JLabel("New label");
panel_1.add(lblNewLabel_1);
JRadioButton rdbtnNewRadioButton_1 = new JRadioButton("New radio button");
panel_1.add(rdbtnNewRadioButton_1);
frame.pack();
}
}
In two conditions, you should add two containers into your code and make them flow layout.

JScrollPane scroll visible but don't work

Can you please tell me why the scroll doesn't work. It is visible, but it doesn't work,
here you can view that piece of code. What could be the missing part?
// GUI elements
private JTextField textSend = new JTextField(20);
private JTextArea textArea = new JTextArea(5, 20);
private JScrollPane scroll = new JScrollPane(textArea);
private JButton buttonConnect = new JButton("Connect");
private JButton buttonSend = new JButton("Send");
private JButton buttonDisconnect = new JButton("Disconnect");
private JButton buttonQuit = new JButton("Quit");
private JPanel leftPanel = new JPanel();
private JPanel rightPanel = new JPanel();
private JLabel empty = new JLabel("");
ChessHeroChatClient() {
setTitle("ChessHero Chat Client");
setLocationRelativeTo(null);
setSize(500, 500);
setResizable(false);
leftPanel.setLayout(new BorderLayout());
rightPanel.setLayout(new GridLayout(6, 1));
scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
leftPanel.add(textSend, BorderLayout.NORTH);
leftPanel.add(textArea, BorderLayout.WEST);
leftPanel.add(scroll, BorderLayout.EAST);
add(leftPanel, BorderLayout.CENTER);
add(rightPanel, BorderLayout.EAST);
textArea.setEditable(false);
private JTextArea textArea = new JTextArea(5, 20);
private JScrollPane scroll = new JScrollPane(textArea);
You create the scrollPane with the textArea which is good.
//leftPanel.add(textArea, BorderLayout.WEST); // this is wrong
leftPanel.add(scroll, BorderLayout.EAST);
But then you add the textArea to the WEST and the scroll to the EAST, which is wrong. A Swing component can only have a single parent, so just leave the textArea alone and add the scrollPane to the EAST or WEST.

How to layout the button and combobox middle?

I am new to java swing, I wrote a startup program to formart text, but i am confused with the layout,
the result is below:
I want the combobox and the button are placed middle of the ctrlPanel, and the combobox should not be stretched
public class MainFrame extends JFrame {
private static final long serialVersionUID = 7553142908344084288L;
private static String[] formats = new String[] {
"JSON",
"XML",
"YAML"
};
public MainFrame() {
super("jValidator");
Panel mainPanel = new Panel();
mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.X_AXIS));
setContentPane(mainPanel);
JTextArea fromTextArea = new JTextArea(20, 40);
JScrollPane fromTextAreaScrollPanel = new JScrollPane(fromTextArea);
fromTextAreaScrollPanel.setPreferredSize(new Dimension(300, 300));
fromTextAreaScrollPanel.setBorder(BorderFactory.createEmptyBorder(15, 5, 15, 5));
mainPanel.add(fromTextAreaScrollPanel);
JButton fmtButton = new JButton("Format >>");
JComboBox jComboBox = new JComboBox(formats);
jComboBox.setBorder(BorderFactory.createTitledBorder("Text Format"));
JPanel ctrPanel = new JPanel();
ctrPanel.setLayout(new BoxLayout(ctrPanel, BoxLayout.Y_AXIS));
ctrPanel.setAlignmentY(Component.CENTER_ALIGNMENT);
ctrPanel.setBorder(BorderFactory.createEmptyBorder(0, 5, 0, 5));
ctrPanel.add(jComboBox);
ctrPanel.add(Box.createRigidArea(new Dimension(50, 15)));
ctrPanel.add(fmtButton);
mainPanel.add(ctrPanel);
JTextArea toTextArea = new JTextArea(20, 40);
JScrollPane toTextAreaScrollPanel = new JScrollPane(toTextArea);
toTextAreaScrollPanel.setPreferredSize(new Dimension(300, 300));
toTextAreaScrollPanel.setBorder(BorderFactory.createEmptyBorder(15, 5, 15, 5));
mainPanel.add(toTextAreaScrollPanel);
setDefaultCloseOperation(EXIT_ON_CLOSE);
pack();
setVisible(true);
}
public static void main(String[] args) {
new MainFrame();
}
}
You could use a GridBagLayout instead of a BoxLayout...
JPanel ctrPanel = new JPanel(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridwidth = GridBagConstraints.REMAINDER;
ctrPanel.setBorder(BorderFactory.createEmptyBorder(0, 5, 0, 5));
gbc.fill = GridBagConstraints.HORIZONTAL;
ctrPanel.add(jComboBox, gbc);
ctrPanel.add(Box.createRigidArea(new Dimension(50, 15)), gbc);
gbc.fill = GridBagConstraints.NONE;
ctrPanel.add(fmtButton, gbc);
Take a look at Laying Out Components Within a Container for more details
For that purposes I recommend you to use another LayoutManager, for example GridBagLayout change creation of ctrPanel like next :
JButton fmtButton = new JButton("Format >>");
JComboBox jComboBox = new JComboBox(formats);
jComboBox.setBorder(BorderFactory.createTitledBorder("Text Format"));
JPanel ctrPanel = new JPanel(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.gridx=0;
c.gridy=1;
ctrPanel.setBorder(BorderFactory.createEmptyBorder(0, 5, 0, 5));
ctrPanel.add(fmtButton,c);
c.gridy=0;
c.fill = GridBagConstraints.HORIZONTAL;
ctrPanel.add(jComboBox,c);
mainPanel.add(ctrPanel);
And it looks like:

GUI Layout for User Interface

I am trying to get a userinterface page to display correctly on my page. It has to display a certain way on the screen but I cant get the correct Lay out to show up. Any suggestions or Help?! I've tried many things but Java can get a bit confusing. ATTACHED IS A LINK FOR IMAGE OF HOW ITS SUPPOSED TO LOOK
https://courses.eas.asu.edu/cse205/current/assignments/assignment6/assignment6.html
public CreatePanel(Vector accountList, TransferPanel tPanel)
{
this.accountList = accountList;
this.transferPanel = tPanel;
JLabel label1 =new JLabel("Account ID: ");
JLabel label2 = new JLabel("Amount: ");
JTextField field1 = new JTextField();
field1.setPreferredSize(new Dimension(250,70));
JTextField field2 = new JTextField();
field2.setPreferredSize(new Dimension(250,70));
button1 = new JButton("Create an Account");
JTextArea textArea = new JTextArea();
textArea.setPreferredSize(new Dimension(500, 600));
textArea.append("No account");
textArea.setEditable(true);
JPanel panel1 = new JPanel();
panel1.setLayout(new GridLayout(3,3));
panel1.add(label1);
panel1.add(field1);
panel1.add(label2);
panel1.add(field2);
JPanel panel2 = new JPanel();
panel2.setLayout(new BorderLayout());
panel2.add(button1, BorderLayout.SOUTH);
JPanel panel3 = new JPanel();
panel3.setLayout(new BorderLayout());
panel3.add(textArea, BorderLayout.WEST);
add(panel1);
add(panel3);
add(panel2);
//ActionListener listener = new ButtonListener();
//button1.addActionListener(listener);
}

Categories