JButton scale all the screen - java

I have the following code
JFrame frame = new JFrame("Organizer");
frame.setBounds(100, 100, 700, 700);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
JButton testbutton = new JButton("testbutton");
testbutton.setBounds(0, 0, 55, 55);
JButton testbutton2 = new JButton("tdestbutton2");
testbutton2.setBounds(55, 0, 44, 44);
frame.add(testbutton2);
frame.add(testbutton);
and the result sometimes is correct and sometimes is this
what im doing wrong?

What am I doing wrong?
Don't use setBounds(); do use a layout manager.
Invoke setVisible() after adding components to the enclosing container.
Construct and manipulate Swing GUI objects only on the event dispatch thread.
The example below adds a panel having an empty border and a GridLayout that is padded to match. For such an application, also consider JToolBar for the buttons and CardLayout for the working screens.
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.GridLayout;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
/**
* #see http://stackoverflow.com/a/37366846/230513
*/
public class Test {
private static final int PAD = 50;
private void display() {
JFrame f = new JFrame("Test");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel p = new JPanel(new GridLayout(0, 1, PAD, PAD));
p.setBorder(BorderFactory.createEmptyBorder(PAD, PAD, PAD, PAD));
p.add(new JButton("Test Button 1"));
p.add(new JButton("Test Button 2"));
f.add(p, BorderLayout.CENTER);
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}
public static void main(String[] args) {
EventQueue.invokeLater(new Test()::display);
}
}

testbutton.setBounds(0, 0, 55, 55);
testbutton.setLocation(50, 50);
testbutton2.setBounds(55, 0, 44, 44);
testbutton2.setLocation(50, 100);
Get rid of those statement because then do nothing.
They do nothing because the default layout manager for the content pane of a JFrame is a BorderLayout. The BorderLayout will reset the size/location of each component.
The BorderLayout expects you to provide a "constraint", when you add the component to the frame. If you don't provide the constraint, then the "CENTER" is used. Only a single component can be added to the "CENTER" so only the last button added is displayed.
A simple solution to understand the basic concept of using a BorderLayout is to use:
//frame.add(testbutton2);
//frame.add(testbutton);
frame.add(testbutton2, BorderLayout.PAGE_START);
frame.add(testbutton, BorderLayout.PAGE_END);
This will display the button on two rows.
Read the Swing tutorial on Layout Managers for more information and examples. Download the demo code and modify that code will follow Swing conventions.
For example, you need to add components to the frame BEFORE you make the frame visible. So the order of the code should be:
frame.add(...);
frame.add(...);
frame.pack();
frame.setVisible(true);

Related

Jscrollpane center aligns when label is too small

This question is very similiar to this: JScrollPane doesn't top align when there is more than enough space to show the content I tried this solution, but it does not work.
When I add a jlabel to jscrollpane, when the jlabel is small, the label becomes centered. It works normally when the scrollbar shows. Setting boxlayout does not change anything. I feel like this isn't working properly because I'm setting a perferred size to the panel? But if I remove the line panel.setPreferredSize(new Dimension((int)(screenSize.width*0.7 - 50), screenSize.height-150)); The label becomes small when there is no text, and grows to accomdate text, which I don't want. If I add the panel instead of the label, it makes the screen scrollable even though there isn't text?
This is my code:
public class Test {
// JFrame
static JFrame frame = new JFrame("Test");
//panel 1
static JPanel panel = new JPanel(new BorderLayout());
// label to display text
static JLabel label = new JLabel();
//scroll panel in main method
public static void main(String[] args) throws Exception {
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
label.setBorder(new EmptyBorder(5, 5, 5, 20));
label.setText("any text makes it centered beyond 40 lines");
//create panel
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
panel.add(label, BorderLayout.NORTH);
panel.add(Box.createVerticalGlue());
panel.setPreferredSize(new Dimension((int)(screenSize.width*0.7 - 50), screenSize.height-150));
panel.setBorder(new EmptyBorder(5, 5, 5, 10));
JScrollPane jspanel = new JScrollPane(label, ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
//jspanel.add(label, BorderLayout.CENTER);
jspanel.setPreferredSize(new Dimension((int)(screenSize.width*0.7 - 70), screenSize.height-180));
jspanel.getVerticalScrollBar().setUnitIncrement(20);
jspanel.setBorder(BorderFactory.createEmptyBorder());
jspanel.setAlignmentX(JScrollPane.LEFT_ALIGNMENT);
jspanel.setAlignmentY(JScrollPane.TOP_ALIGNMENT);
//panel.setPreferredSize(new Dimension(640, 480));
//frame.add(panel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(jspanel);
frame.setSize((int)(screenSize.width*0.7), screenSize.height - 50);
frame.revalidate();
frame.pack();
frame.setVisible(true);
}
}
jspanel.setAlignmentX(JScrollPane.LEFT_ALIGNMENT);
jspanel.setAlignmentY(JScrollPane.TOP_ALIGNMENT);
That will align the scrollpane in its parent container, depending on the layout manager being used. It does not affect the alignment of any component added to the scrollpane. It is not needed.
the label becomes centered
The label is sized to fill the entire space available, so you need to customize how the text of the label is painted.
If you don't want it centered then you can place it at the top using:
label.setVerticalAlignment( SwingConstants.TOP );
After reworking your code, I came up with the following GUI.
I added a call to the SwingUtilities invokeLater method. This method ensures that all Swing components are created and executed on the Event Dispatch Thread.
I eliminated all static references, except for the main method.
I reworked your code into methods so I could focus on one part of the GUI at a time.
Here's the complete runnable example. This is a minimal reproducible example.
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;
import javax.swing.border.EmptyBorder;
public class JScrollPaneTestGUI implements Runnable {
public static void main(String[] args) {
SwingUtilities.invokeLater(new JScrollPaneTestGUI());
}
#Override
public void run() {
JFrame frame = new JFrame("JScrollPane Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JScrollPane jspanel = createJScrollPane();
frame.add(jspanel, BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
}
private JScrollPane createJScrollPane() {
JPanel panel = new JPanel(new BorderLayout());
panel.setBackground(Color.YELLOW);
panel.setPreferredSize(new Dimension(400, 300));
JLabel label = new JLabel();
label.setBorder(new EmptyBorder(5, 5, 5, 20));
label.setText("any text makes it centered beyond 40 lines");
//create panel
panel.add(label, BorderLayout.NORTH);
JScrollPane jspanel = new JScrollPane(panel);
return jspanel;
}
}

Java Swing Pixels Being Inaccurate

I am designing a Java app with Swing, and I have trouble designing the GUI without a layout.
My purpose is to design a GUI with one JPanel and four JButtons. I've done the math to set buttons and panel on the right place and coded like the following:
import java.awt.*;
import javax.swing.*;
public class MainFrame extends JFrame {
public MainFrame() {
this.setTitle("Example Frame");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLayout(null);
JPanel randomPanel = new JPanel();
randomPanel.setOpaque(true);
randomPanel.setBackground(Color.RED);
randomPanel.setBounds(10, 10, 430, 530);
JButton addButton = new JButton("Add");
addButton.setBounds(10, 550, 100, 40);
addButton.setBackground(Color.GRAY);
JButton deleteButton = new JButton("Delete");
deleteButton.setBounds(120, 550, 100, 40);
deleteButton.setBackground(Color.GRAY);
JButton refreshButton = new JButton("Refresh");
refreshButton.setBounds(230, 550, 100, 40);
refreshButton.setBackground(Color.GRAY);
JButton devButton = new JButton("Developer");
devButton.setBounds(340, 550, 100, 40);
devButton.setBackground(Color.GRAY);
this.add(randomPanel);
this.add(addButton);
this.add(deleteButton);
this.add(refreshButton);
this.add(devButton);
this.setSize(900, 600);
this.setResizable(false);
this.setVisible(true);
}
public static void main(String[] args) {
new MainFrame();
}
}
Following to the code, the components are expected to be placed as following:
However, the actual form was displayed as following:
The components exceed the form, which does not match with the expected look.
What is the problem of this and what should be done for an accurate placement of components?
There are two main problems...
setLayout(null)
setSize
What you've not taken into account is the fact that the amount of space available to the content of the window, is the size of the window MINUS the frame decorations.
Pixel perfect layouts are an illusion in modern UI development and are best avoided.
You could have a look at:
What's wrong with the Null Layout in Java?
Why is it frowned upon to use a null layout in Swing?
Why null layout and absolute positions are bad practice in Java Swing?
for more details.
A better solution is to make use one or more available layout managers. The example below simply makes use of BorderLayout and GridLayout with the help of EmptyBorder to provide some padding
See Laying Out Components Within a Container for more details
Benefits
Adaptable layout:
The example uses pack to "pack" the window around the content, automatically, without you having to adapt your code to the currently running OS (or frame decorations provided by different look and feels)
The user can change the size of the window and the content will resize automatically - bonus to the user.
The layout will adapt to the user's system settings, so if they are using a font larger then you've designed for, it won't completely blow up in your face
Want to add more buttons? No worries, knock yourself out, just add more buttons, the layout will adapt automatically, no need to "pixel push" ever component on the screen
Runnable example...
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.EmptyBorder;
public class Test {
public static void main(String[] args) {
new Test();
}
public Test() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
public TestPane() {
setLayout(new BorderLayout());
setBorder(new EmptyBorder(10, 10, 10, 10));
add(new SizablePane(430, 530));
JPanel buttonPane = new JPanel(new GridLayout(1, 3, 20, 0));
buttonPane.setBorder(new EmptyBorder(10, 0, 0, 0));
buttonPane.add(new JButton("Add"));
buttonPane.add(new JButton("Delete"));
buttonPane.add(new JButton("Refresh"));
buttonPane.add(new JButton("Developer"));
add(buttonPane, BorderLayout.SOUTH);
}
}
public class SizablePane extends JPanel {
private Dimension size;
public SizablePane(int width, int height) {
size = new Dimension(width, height);
setBackground(Color.RED);
}
#Override
public Dimension getPreferredSize() {
return size;
}
}
}
Need to add more buttons? Easy...
JPanel buttonPane = new JPanel(new GridLayout(1, 0, 20, 0));
buttonPane.setBorder(new EmptyBorder(10, 0, 0, 0));
buttonPane.add(new JButton("Add"));
buttonPane.add(new JButton("Delete"));
buttonPane.add(new JButton("Refresh"));
buttonPane.add(new JButton("Developer"));
buttonPane.add(new JButton("Some"));
buttonPane.add(new JButton("More"));
buttonPane.add(new JButton("Buttons"));
I'm quite late, I don't think this will be helpful to OP anymore... But to anyone else in the same situation.
As others mentioned, when you setSize on a JFrame, that includes the title bar and borders. There's a way to get the size values for those, but... If you want to lay things out manually in your content pane, why not prepare a content pane first, then add it to the JFrame?
class MainPanel extends JPanel {
public MainPanel() {
setLayout(null);
setPreferredSize(new Dimension(900, 600));
// JFrame will have some layouting going on,
// it won't listen to setSize
JPanel randomPanel = new JPanel();
randomPanel.setOpaque(true);
randomPanel.setBackground(Color.RED);
randomPanel.setBounds(10, 10, 430, 530);
JButton addButton = new JButton("Add");
addButton.setBounds(10, 550, 100, 40);
addButton.setBackground(Color.GRAY);
JButton deleteButton = new JButton("Delete");
deleteButton.setBounds(120, 550, 100, 40);
deleteButton.setBackground(Color.GRAY);
JButton refreshButton = new JButton("Refresh");
refreshButton.setBounds(230, 550, 100, 40);
refreshButton.setBackground(Color.GRAY);
JButton devButton = new JButton("Developer");
devButton.setBounds(340, 550, 100, 40);
devButton.setBackground(Color.GRAY);
this.add(randomPanel);
this.add(addButton);
this.add(deleteButton);
this.add(refreshButton);
this.add(devButton);
}
public static void main(String[] args) {
JFrame mainFrame = new JFrame();
mainFrame.setTitle("Example Frame");
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainFrame.setContentPane(new MainPanel());
mainFrame.pack();
mainFrame.setResizable(false);
mainFrame.setVisible(true);
}
}
If you mess with JFrame directly you're sort of bypassing the component system. Whereas this way, you're doing components just fine! Now, you have a JFrame fit to a single child panel, which has some things laid out manually.
This is how I normally do things, in such a situation.
P.S. "Don't lay things out manually, just use layout managers" is not something you can apply everywhere. You may need custom components sometimes, especially for something like a video game, where you have a game screen that you're custom rendering. Inside the game screen, you would be doing manual layout. They can coexist just fine, as long as you know which is which.
You need to override the getInsets() method of the underlying JFrame.
#Override
public Insets getInsets() {
return new Insets(0, 0, 0, 0);
}
Take a look at this question for more information.

Unstable Miglayout -- make two grow columns of the same size

I have this simple MigLayout:
The two last components (JScrollPanes) should have the same width. But in fact, if I resize the window, they are randomly jumping. Is it possible to make their width equal? How else can I arrange components to make this look symmetrical?
You need your columns to use the same sizegroup/sg in your column constraints. This way both columns will always have the same width.
Like this:
setLayout(new MigLayout("", "[sizegroup main, grow][sizegroup main,grow]"[][][][grow]"));
See also the MigLayout Cheatsheet about sizegroup.
Most of the time I prefer to use the built-in facilities of java rather than mixing much further complex libraries and dependencies for such simple cases. I think when you can achieve the solution with a trivial effort like this it's not needed to use third party libraries such as MIG. This preference comes from the situation you are in: not so many people work with a purchased tools, so you can not get so many help from the community.
I know this question asks about MigLayout but I preferred to show that there is no need to use that for this simple situation. MIG library is rich and have some useful components which can make your life easier but when it comes to layouts I prefer pure java.
Sampling your layout using pure BorderLayout and GridLayout:
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.UIManager;
import javax.swing.WindowConstants;
public class TestMain {
public static void main(String[] args) {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) {
e.printStackTrace();
}
JFrame f = new JFrame();
f.setBounds(50, 50, 500, 400);
f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
f.add(createSpacerPanel(10, 10), BorderLayout.NORTH);
f.add(createSpacerPanel(10, 10), BorderLayout.SOUTH);
f.add(createSpacerPanel(10, 10), BorderLayout.EAST);
f.add(createSpacerPanel(10, 10), BorderLayout.WEST);
f.add(new MainPanel());
f.setVisible(true);
}
private static JPanel createSpacerPanel(int width, int height){
JPanel spacer = new JPanel();
spacer.setPreferredSize(new Dimension(width, height));
return spacer;
}
}
class MainPanel extends JPanel{
public MainPanel() {
init();
}
private void init() {
JPanel northPanel = new JPanel(new BorderLayout(10, 10));
northPanel.setPreferredSize(new Dimension(100, 60));
northPanel.add(new JLabel("Class Expression: "), BorderLayout.NORTH);
JTextArea classExpressionTextArea = new JTextArea();
classExpressionTextArea.setSize(10, 40);
northPanel.add(new JScrollPane(classExpressionTextArea), BorderLayout.CENTER);
JButton calculateButton = new JButton("Calculate");
northPanel.add(calculateButton, BorderLayout.EAST);
JPanel definitionPanel = new JPanel(new BorderLayout(10,10));
definitionPanel.add(new JLabel("Definitions Found: "), BorderLayout.NORTH);
JTextArea definitionsTextArea = new JTextArea();
definitionPanel.add(new JScrollPane(definitionsTextArea), BorderLayout.CENTER);
JPanel signaturePanel = new JPanel(new BorderLayout(10,10));
signaturePanel.add(new JLabel("Target Signature: "), BorderLayout.NORTH);
JTextArea targetTextArea = new JTextArea();
signaturePanel.add(new JScrollPane(targetTextArea), BorderLayout.CENTER);
GridLayout gridLayout = new GridLayout(1,1,10,10);
JPanel centerPanel = new JPanel(gridLayout);
centerPanel.add(definitionPanel);
centerPanel.add(signaturePanel);
setLayout(new BorderLayout(10,10));
add(northPanel, BorderLayout.NORTH);
add(centerPanel, BorderLayout.CENTER);
}
}
And the fully resizable output:
The same can be achieved using GridBagLayout which is similar to MigLayout in the way of thinking about the layout and positioning and spanning components over grid cells.
Hope this would be helpful.

Java JTextArea & JScrollPane not working

i am writing simple code with GUI that should have one text area which should be scrollable. So far so good.
I created my frame and the text area and i can write in it ok. Next I created my ScrollPane and added the TextArea in it, then added the ScrollPane to the frame but nothing shows.
Here is the code i have at this point:
JFrame frame = new JFrame();
frame.setBounds(100, 100, 325, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
//textArea
JTextArea textArea = new JTextArea();
textArea.setEnabled(true);
textArea.setEditable(true);
JScrollPane scroll = new JScrollPane(textArea,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
textArea.setBounds(10, 101, 272, 149);
textArea.setWrapStyleWord(true);
frame.getContentPane().add(scroll);
change
frame.getContentPane().setLayout(null);
to
frame.getContentPane().setLayout(new BorderLayout());
and you are done
You have to set the bounds to the component that is being added to the content pane of the frame. In this case, it should be: scroll.setBounds(10,101,271,149).
However, I strongly recommend to not use null layout. Use a layout manager of your choice, BorderLayout for instance. In this case you don't have to worry about the bounds, it will fit the frame size (it will resize when you change the size of the frame). Here's your example, tweaked a little bit:
import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
public class Test {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setBounds(100, 100, 325, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(new BorderLayout());
// textArea
JTextArea textArea = new JTextArea();
textArea.setEnabled(true);
textArea.setEditable(true);
JScrollPane scroll = new JScrollPane(textArea, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
textArea.setWrapStyleWord(true);
frame.getContentPane().add(scroll, BorderLayout.CENTER);
frame.setVisible(true);
}
}
I'm still learning however by looking at this case.
I have couple of issues:
There is issue with setting bounds of textArea
Layout of frame/container should not be set to null.
I have removed this sentence, and I tried this code, it displays desired textArea.
import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
/*
* Tester class
*/
public class GuiTester extends JFrame{
public static void main(String[] args) {
// create new instance of JFrame
GuiTester s = new GuiTester();
// set the frame to be visible
s.setVisible(true);
}
/**
* Tester constructor calling method which initialise all widgets.
*/
GuiTester() {
//
invokeWidget();
}
/*
* This code is yours, just removed setting up the values of container and did that straight on the frame.
*/
void invokeWidget() {
setBounds(100, 100, 325, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
JTextArea textArea = new JTextArea();
textArea.setEnabled(true);
textArea.setEditable(true);
JScrollPane scroll = new JScrollPane(textArea, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
textArea.setWrapStyleWord(true);
add(scroll);
}
}
This is the line that was causing issues, as well as setting layout Manager to null.
// textArea.setBounds(10, 101, 272, 149);
I hope I helped, and if I'm wrong please correct me as well.

Trying to add a scrollbar to a JPanel with GridLayout, but the JCheckBoxes just get made smaller instead

So I'm trying to set up a Gui in Java which holds a list of checkboxes. What determines the length of the list is the highlighted checkboxes. However, when I add more things to the list the checkboxes just get smaller to fit the panel. I've added a vertical scrollbar, but this just doesn't do anything. Is there something I have to do to stop the GridLayout from resizing what it holds or is it the wrong layout?
package darrt;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
public class TestForScrollBat {
public static void main(String[] args){
new TestForScrollBat();
}
public TestForScrollBat(){
JFrame frame = new JFrame();
JPanel panel = new JPanel(new GridLayout(0, 1));
JScrollPane scrollPane = new JScrollPane(panel, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
scrollPane.setBounds(50, 30, 300, 50);
frame.getContentPane().add(scrollPane, BorderLayout.CENTER);
JLabel label = new JLabel(" Soc Categories");
JCheckBox soc1 = new JCheckBox("Blood and Lymphatic System Disorder");
JCheckBox soc2 = new JCheckBox("Cardiac Disorders");
JCheckBox soc3 = new JCheckBox("Congenital, familial and Genetic Disorders");
JButton jbtn = new JButton("Go!");
panel.add(label);
panel.add(soc1);
panel.add(soc2);
panel.add(soc3);
panel.add(jbtn);
frame.add(panel);
frame.pack();
frame.setVisible(true);
}
}
I had it before so that it would add a scroll to this panel, but now it doesn't even do that.. It just creates a new JPanel on the JFrame
Your problem is about the following lines in your code:
scrollPane.setBounds(50, 30, 300, 50);
You should not set static sizes and locations when using layouts. You are telling a specific size and location to the scrollPane while you had add it to the center of the contentPane before. These two are in conflict.
And next problem is about this line:
frame.add(panel);
This line will detach the panel from you JScrollPane and add it directly to the contentPane of the JFrame.
By deleting/commenting these lines, your problem will be solved.

Categories

Resources