I just began Swing. I'm trying to make a gallery app. I'm getting images that I manually imported and I display them. Depending on the amount of columns I put in parameters, the images' dimensions are calculated to be displayed properly. But, after a certain amount of rows, I want a scrollbar that could scroll and show the rest of images.
The images are displayed properly, like I wished but I tried to implement the scrollbar and it isn't appearing.
Could you tell me what's wrong in my code ?
GUI(String title, int width, int height, int columns) {
this.frame = new JFrame();
this.frameWidth = width;
this.columns = columns;
// set Frame's size
frame.setSize(width, height);
// set Frame's action on close button clicked
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// set Frame's title
frame.setTitle(title);
frame.setResizable(false);
// set Frame's position at screen's center
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
frame.setLocation(dim.width / 2 - frame.getSize().width / 2, (dim.height / 2 - frame.getSize().height / 2));
panel = new JPanel();
frame.setLayout(new GridLayout());
this.scrollBar = new JScrollPane();
scrollBar.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
frame.getContentPane().add(scrollBar, BorderLayout.EAST);
displayImages();
frame.setContentPane(panel);
}
displayImages() :
private void displayImages() {
for (File currentImage : getImagesList()) {
// 5 is flowlayout's default borders
// 2 because we got left and right borders
int totalBorders = (columns * 5) * 2;
int buttonWidth = (frameWidth - totalBorders) / columns;
ImageIcon image = new ImageIcon(new ImageIcon(currentImage.getAbsolutePath()).getImage().getScaledInstance(buttonWidth - 20, buttonWidth - 50, Image.SCALE_DEFAULT));
JButton button = new JButton(currentImage.getName());
button.setPreferredSize(new Dimension(buttonWidth, buttonWidth));
button.setIcon(image);
button.setHorizontalTextPosition(AbstractButton.CENTER);
button.setVerticalTextPosition(AbstractButton.BOTTOM);
button.addActionListener(e -> onImageClicked(currentImage.getName()));
panel.add(button);
}
}
Thanks, best regards
panel = new JPanel();
frame.setLayout(new GridLayout());
this.scrollBar = new JScrollPane();
scrollBar.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
frame.getContentPane().add(scrollBar, BorderLayout.EAST);
displayImages();
frame.setContentPane(panel);
The above code is mostly wrong. You need to:
set the layout of the panel and add the buttons to the panel
add the panel containing the buttons to the scroll pane
The basic code should be something like:
//panel = new JPanel( new GridLayout(0, 1) );
layout = new GridLayout(0, 1);
panel = new JPanel( layout );
displayImages();
//frame.setLayout(new GridLayout());
this.scrollBar = new JScrollPane(panel);
scrollBar.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
frame.add(scrollBar, BorderLayout.EAST);
//displayImages();
//frame.setContentPane(panel);
Also, there is no need to play with the sizes of each component on the panel. The GridLayout will make all the buttons the same size.
Edit:
When you want to increase the columns in the GridLayout you then just do:
layout.setColumns( layout.getColumns() + 1 );
panel.revalidate(); // this invokes the layout manager
//panel.repaint(); // sometimes needed to force repainting of panel.
Related
Respected Developers,
I am developing a NASA World Wind application for desktop.
I have tried to put a JSplitPane where in the lower Panel displays the result and the upper one displays the canvas is in two sections. The left component contains the layer list and the right component contains the canvas. I have been trying to insert a JTabbedPane in the left component so as to have different options like the layer list, SQL query list and many more. I am unable to add and view the frame of the JTabbedPane on the left Panel.
I am attaching my code for reference.
All help is appreciated.
CODE BLOCK
public AppFrame()
{
/*------------------------------------INITIALIZATION OF THE MAIN FRAME------------------------------------*/
this.initialize(false, true, false);
/*---------------------------------------SPLIT PANE AND PANEL CODE---------------------------------------*/
// Create a horizontal split pane containing the layer panel and the WorldWindow panel.
JSplitPane horizontalSplitPane = new JSplitPane();
horizontalSplitPane.setOrientation(JSplitPane.HORIZONTAL_SPLIT);
JTabbedPane tp=new JTabbedPane();
horizontalSplitPane.setLeftComponent(tp);
horizontalSplitPane.setRightComponent(wwjPanel);
horizontalSplitPane.setOneTouchExpandable(true);
horizontalSplitPane.setContinuousLayout(true);// prevents the pane's being obscured when expanding right
// Create a panel for the bottom component of a vertical split-pane.
JPanel bottomPanel = new JPanel(new BorderLayout());
JLabel label = new JLabel("Bottom Panel");
label.setBorder(new EmptyBorder(10, 10, 10, 10));
label.setHorizontalAlignment(SwingConstants.CENTER);
bottomPanel.add(label, BorderLayout.CENTER);
// Create a vertical split-pane containing the horizontal split plane and the button panel.
JSplitPane verticalSplitPane = new JSplitPane();
verticalSplitPane.setOrientation(JSplitPane.VERTICAL_SPLIT);
verticalSplitPane.setTopComponent(horizontalSplitPane);
verticalSplitPane.setBottomComponent(bottomPanel);
verticalSplitPane.setOneTouchExpandable(true);
verticalSplitPane.setContinuousLayout(true);
verticalSplitPane.setResizeWeight(1);
// Add the vertical split-pane to the frame.
this.getContentPane().add(verticalSplitPane, BorderLayout.CENTER);
this.pack();
// Center the application on the screen.
Dimension prefSize = this.getPreferredSize();
Dimension parentSize;
java.awt.Point parentLocation = new java.awt.Point(0, 0);
parentSize = Toolkit.getDefaultToolkit().getScreenSize();
int x = parentLocation.x + (parentSize.width - prefSize.width) / 2;
int y = parentLocation.y + (parentSize.height - prefSize.height) / 2;
this.setLocation(x, y);
this.setResizable(true);
/*-------------------------------------------PATH CREATION CODE-------------------------------------------*/
// Add a dragger to enable shape dragging
this.getWwd().addSelectListener(new BasicDragger(this.getWwd()));
// Create and set an attribute bundle.
ShapeAttributes attrs = new BasicShapeAttributes();
attrs.setOutlineMaterial(new Material(WWUtil.makeRandomColor(null)));
attrs.setOutlineWidth(2d);
ArrayList<Position> pathPositions = new ArrayList<Position>();
pathPositions.add(Position.fromDegrees(26, 75, 1e4));
pathPositions.add(Position.fromDegrees(20, 80, 1e4));
Path path = new Path(pathPositions);
path.setAttributes(attrs);
path.setVisible(true);
path.setAltitudeMode(WorldWind.RELATIVE_TO_GROUND);
path.setPathType(AVKey.GREAT_CIRCLE);
RenderableLayer propertiesLayer = new RenderableLayer();
propertiesLayer.addRenderable(path);
/*--------------------------------------------TABBED PANE CODE--------------------------------------------*/
JTextArea ta = new JTextArea();
horizontalSplitPane.add(ta);
// JPanel p2=new JPanel();
// JPanel p3=new JPanel();
tp.setBounds(50,50,200,200);
tp.add("Layers",layerPanel);
// tp.add("SQL",p2);
// tp.add("help",p3);
horizontalSplitPane.add(tp);
tp.setSize(400,400);
tp.setLayout(null);
tp.setVisible(true);
// Add the layer to the model.
insertBeforeCompass(getWwd(), propertiesLayer);
List<String> markers = new ArrayList<String>(1);
markers.add(String.valueOf(new BasicMarker(Position.fromDegrees(90, 0), new BasicMarkerAttributes())));
MarkerLayer markerLayer = new MarkerLayer();
// markerLayer.setMarkers(markers);
insertBeforeCompass(getWwd(), markerLayer);
}
ERROR BLOCK
"C:\Program Files (x86)\Java\jdk1.7.0_03\bin\java" "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2017.3.4\lib\idea_rt.jar=55088:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2017.3.4\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files (x86)\Java\jdk1.7.0_03\jre\lib\charsets.jar;C:\Program Files (x86)\Java\jdk1.7.0_03\jre\lib\deploy.jar;C:\Program Files (x86)\Java\jdk1.7.0_03\jre\lib\ext\dnsns.jar;C:\Program Files (x86)\Java\jdk1.7.0_03\jre\lib\ext\localedata.jar;C:\Program Files (x86)\Java\jdk1.7.0_03\jre\lib\ext\sunec.jar;C:\Program Files (x86)\Java\jdk1.7.0_03\jre\lib\ext\sunjce_provider.jar;C:\Program Files (x86)\Java\jdk1.7.0_03\jre\lib\ext\sunmscapi.jar;C:\Program Files (x86)\Java\jdk1.7.0_03\jre\lib\ext\sunpkcs11.jar;C:\Program Files (x86)\Java\jdk1.7.0_03\jre\lib\ext\zipfs.jar;C:\Program Files (x86)\Java\jdk1.7.0_03\jre\lib\javaws.jar;C:\Program Files (x86)\Java\jdk1.7.0_03\jre\lib\jce.jar;C:\Program Files (x86)\Java\jdk1.7.0_03\jre\lib\jsse.jar;C:\Program Files (x86)\Java\jdk1.7.0_03\jre\lib\management-agent.jar;C:\Program Files (x86)\Java\jdk1.7.0_03\jre\lib\plugin.jar;C:\Program Files (x86)\Java\jdk1.7.0_03\jre\lib\resources.jar;C:\Program Files (x86)\Java\jdk1.7.0_03\jre\lib\rt.jar;D:\NasaWorldWind\classes;D:\NasaWorldWind\jogl-all.jar;D:\NasaWorldWind\gluegen-rt.jar;D:\NasaWorldWind\gdal.jar;D:\DOWNLOADS\postgresql-42.2.1.jre7.jar" avr_dbpostgisapp.ApplicationTemplateTest
java.lang.InstantiationException: avr_dbpostgisapp.ApplicationTemplateTest$AppFrame
at java.lang.Class.newInstance0(Class.java:357)
at java.lang.Class.newInstance(Class.java:325)
at avr_dbpostgisapp.ApplicationTemplateTest.start(ApplicationTemplateTest.java:410)
at avr_dbpostgisapp.ApplicationTemplateTest.main(ApplicationTemplateTest.java:586)
Exception in thread "main" java.lang.NullPointerException
at avr_dbpostgisapp.ApplicationTemplateTest.connstart(ApplicationTemplateTest.java:502)
at avr_dbpostgisapp.ApplicationTemplateTest.main(ApplicationTemplateTest.java:588)
Process finished with exit code 1
So, I was able to manage the addition of the JTabbedPane to the left component of the JSplitPane.
The code is as follows,
CODE BLOCK
JSplitPane horizontalSplitPane = new JSplitPane();
horizontalSplitPane.setOrientation(JSplitPane.HORIZONTAL_SPLIT);
JTextArea ta = new JTextArea(200, 200);
JPanel p1 = new JPanel();
JPanel p2 = new JPanel();
p1.add(ta);
p2.add(ta);
JTabbedPane tp = new JTabbedPane();
tp.add("Layers",layerPanel);
tp.add("SQL",p2);
horizontalSplitPane.setLeftComponent(tp);
horizontalSplitPane.setRightComponent(wwjPanel);
horizontalSplitPane.setOneTouchExpandable(true);
horizontalSplitPane.setContinuousLayout(true);
I want to make a Java-Code, where I can insert as many Panels as I want. So that I can scroll down to see the Panels. I'm so far right now:
But my problem is, I can't scroll down. I tested the JScrollPane with JTextAreas which worked just fine.
Picture of my Program
package test;
import java.awt.*;
import javax.swing.*;
public class Scrollbar {
public static void main(String[] args) {
JFrame frame = new JFrame();
JPanel panel = new JPanel();
JPanel panel1 = new JPanel();
JPanel panel2 = new JPanel();
JPanel panel3 = new JPanel();
JTextField tFId = new JTextField("ID: ", 5);
JTextField tFName = new JTextField("Name: ", 5);
JTextField tFHersteller = new JTextField("Hersteller: ", 5);
JTextField tFId2 = new JTextField("ID: ", 5);
JTextField tFName2 = new JTextField("Name: ", 5);
JTextField tFHersteller2 = new JTextField("Hersteller: ", 5);
JTextField tFId3 = new JTextField("ID: ", 5);
JTextField tFName3 = new JTextField("Name: ", 5);
JTextField tFHersteller3 = new JTextField("Hersteller: ", 5);
frame.setSize(400, 400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new FlowLayout());
panel.setLayout(new FlowLayout());
panel.setPreferredSize(new Dimension(200, 85));
panel.add(panel1);
panel.add(panel2);
panel.add(panel3);
JScrollPane scrollPanel = new JScrollPane(panel, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
panel1.setLayout(new FlowLayout());
panel1.add(tFId);
panel1.add(tFName);
panel1.add(tFHersteller);
panel2.setLayout(new FlowLayout());
panel2.add(tFId2);
panel2.add(tFName2);
panel2.add(tFHersteller2);
panel3.setLayout(new FlowLayout());
panel3.add(tFId3);
panel3.add(tFName3);
panel3.add(tFHersteller3);
frame.add(scrollPanel);
frame.setVisible(true);
}
}
You are over-using FlowLayout.
Different layouts have diferent behaviors. First, you need to remove this line:
frame.setLayout(new FlowLayout());
The default layout for a frame’s content pane is a BorderLayout. You want to leave it that way, because a component added to a BorderLayout with no constraints will be placed in the center, where it will stretch to fill the entire space.
Second, you want to remove these:
panel.setLayout(new FlowLayout());
panel.setPreferredSize(new Dimension(200, 85));
Setting the preferred size interferes with the JScrollPane’s ability to manage its view (that is, panel). When you want to have your components appear on multiple rows, you should try to force FlowLayout to do it by constraining its width; rather, use a layout that is designed to place components on different rows. The best choice is GridBagLayout:
panel.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridwidth = GridBagConstraints.REMAINDER;
panel.add(panel1, gbc);
panel.add(panel2, gbc);
gbc.weighty = 1;
gbc.anchor = GridBagConstraints.NORTH;
panel.add(panel3, gbc);
The use of GridBagConstraints.REMAINDER in a constraint will tell the GridBagLayout to make a child component take up an entire row.
The use of weighty = 1 tells the GridBagLayout that the grid cell of the child about to be added should take up all extra vertical space, when the panel is larger than its children. Finally, GridBagConstraints.NORTH keeps that child placed at the top of that stretched grid cell, no matter how high the grid cell is.
I am creating a messaging program that has multiple chats. On the side of the chat window there is a JPanel containing a List Array of all the buttons to switch chats.I am having trouble getting all the buttons in the side panel to be the same width, no matter what they contain. But whatever I try doesn't seem to work and I am looking for some help. Please can you explain what the code does and how it can be used so I can learn it for next time. Sorry if the comments aren't the best it's a work in progress as I wait till code works before adding detailed comments otherwise I am constantly changing them. :(
This is what it looks like without any chats:
This is with multiple chats and you can see the width variation
Here is the code that is run when a new user is added:
public void newUser() {
JPanel dialogue = new JPanel();
dialogue.setLayout(new BoxLayout(dialogue, BoxLayout.Y_AXIS));
//Creating/adding dialogue components
JLabel Enter_ip = new JLabel("Enter the ip address");
JTextField Get_ip = new JTextField("");
dialogue.add(Enter_ip);
dialogue.add(Get_ip);
dialogue.add(Box.createHorizontalStrut(15));
JLabel Enter_name = new JLabel("Enter the user's name");
JTextField Get_name = new JTextField("");
dialogue.add(Enter_name);
dialogue.add(Get_name);
//Creating the dialogue box
JOptionPane.showConfirmDialog(null, dialogue, "New User", JOptionPane.OK_CANCEL_OPTION);
//Getting data from dialogue box
String ip = Get_ip.getText();
String name = Get_name.getText();
//Try connecting to other user here
//Adding user message data
int size = Users_Messages_Data.size();
Users_Messages_Data.add(new ArrayList());//New user
Users_Messages_Data.get(size).add(new ArrayList());//Messages
Users_Messages_Data.get(size).add(new ArrayList());//Details
Users_Messages_Data.get(size).get(1).add(name);
Users_Messages_Data.get(size).get(1).add(ip);
Users_Messages_Data.get(size).get(1).add("port number");
//adds new UserButton
int temp = users.size();
users.add(new JButton(Users_Messages_Data.get(size).get(1).get(0)));
users.get(temp).addActionListener(this);
users.get(temp).setSize(new Dimension(500, 500));
SelectUser.add(users.get(temp), gbc);
Messaging.revalidate();
pack();
}
and here is the initialisation method:
public void MessagingGUI() {
//Creates JFrame and pane
Messaging = new JFrame();
Container pane = getContentPane();
JLabel info = new JLabel("29/07/2016 15:36");
//Creates user chats panel
SelectUser = new JPanel(new GridBagLayout());
gbc = new GridBagConstraints();
gbc.weightx = 1;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridwidth = GridBagConstraints.REMAINDER;
SelectUser.setLayout(new BoxLayout(SelectUser, BoxLayout.Y_AXIS));
SelectUser.setSize(new Dimension(500, 500));
//Adds different chats
users = new ArrayList<JButton>();
int x;
for (x = 0; x < Users_Messages_Data.size(); x++) {
users.add(new JButton(Users_Messages_Data.get(x).get(1).get(0)));
users.get(x).addActionListener(this);
SelectUser.add(users.get(x), gbc);
}
JButton newUser = new JButton("+");
newUser.addActionListener(this);
SelectUser.add(newUser);
JScrollPane UserScroll = new JScrollPane(SelectUser,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
//Creates messages feed
JPanel Messages = new JPanel();
Messages.setLayout(new BoxLayout(Messages, BoxLayout.Y_AXIS));
AllMessages = new JTextArea(10, 30);
AllMessages.setBackground(Color.WHITE);
AllMessages.setEditable(false);
AllMessages.setBorder(BorderFactory.createLineBorder(Color.BLUE, 1));
JScrollPane MessageScroll = new JScrollPane(AllMessages,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
//Creates user text entry box
UserText = new JTextArea(5, 30);
//UserText.setLineWrap(true);
//UserText.setWrapStyleWord(true);
UserText.setBorder(BorderFactory.createLineBorder(Color.CYAN, 1));
UserText.setText("Enter Message. Press enter to send");
UserText.setFocusable(true);
UserText.addKeyListener(this);
UserText.setPreferredSize(new Dimension(5, 20));
//Adds all components to pane
Messages.add(info);
Messages.add(MessageScroll);
Messages.add(UserText);
pane.add(UserScroll, BorderLayout.WEST);
pane.add(Messages, BorderLayout.CENTER);
//JFrame setup
Messaging.setTitle("Messaging");
Messaging.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Messaging.setContentPane(pane);
Messaging.setVisible(true);
Messaging.setSize(400, 350);
Load_User(current_user);
}
First of all, variable names should NOT start with an upper case character. Some of your variable names are correct, other are not. Be consistent and follow Java conventions!!!
.I am having trouble getting all the buttons in the side panel to be the same width,
Don't use a BoxLayout. It does not automatically resize on the opposite axis of the layout.
Instead you can use:
a GridLayout to make all the buttons the same size. The GridLayout will also fill the area vertically which is not what you want so you will need to nest panels. So create a parent panel using a BorderLayout. Add your panel using the GridLayout with the buttons to the BorderLayout.PAGE_START of this parent panel. Then add the BorderLayout panel to the scroll pane.
a GridBagLayout. You will need to use the "fill" contstraint to have the component fill the width of the cell.
Read the Swing tutorial on Layout Managers for more information and examples.
Or maybe a different approach is to use a JList to display the users.
I wrote a little code to see how the scroll Pane functions but my code never worked.
here's the code,
public Fenetre(){
this.setTitle("Data Simulator");
this.setSize(300, 300);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLocationRelativeTo(null);
String hello = "hello";
int number = 69;
JPanel content = new JPanel();
content.setBackground(Color.LIGHT_GRAY);
//Box imad = Box.createHorizontalBox();
JTextArea textArea = new JTextArea(10, 10);
JLabel imad = new JLabel();
imad.setText(hello + " your favorite number is " + number + "\nRight?");
JScrollPane scrollPane = new JScrollPane();
setPreferredSize(new Dimension(450, 110));
scrollPane.setHorizontalScrollBarPolicy(HORIZONTAL_SCROLLBAR_ALWAYS);
scrollPane.setVerticalScrollBarPolicy(VERTICAL_SCROLLBAR_ALWAYS);
scrollPane.setEnabled(true);
scrollPane.setWheelScrollingEnabled(true);
scrollPane.setViewportView(textArea);
scrollPane.setViewportView(imad);
add(scrollPane, BorderLayout.CENTER);
//---------------------------------------------
//On ajoute le conteneur
scrollPane.add(textArea);
scrollPane.add(imad);
content.add(textArea);
content.add(imad);
content.add(scrollPane);
this.setContentPane(content);
this.setVisible(true);
this.setResizable(false);
}
When I run it, I get a little window with the textArea and next to the text area a very little white square, which is the scrollpane i suppose because when I remove it from the code, this square disappears. When I write in the text area and exceed the window's dimension, I can't scroll vertically using the mouse wheel, and not horizontally at all. I saw many examples on internet and I can't understand why my code doesn't work??
Any help explaining how scrollpane works?
scrollPane.setViewportView(textArea);
scrollPane.setViewportView(imad);
Only one component can be added to the viewport of the scroll pane, so the label replaces the text area.
content.add(textArea);
content.add(imad);
A component can only have a single parent. The above code removes the label from the scrollpane, so nothing is now in the scrollpane.
Try something like:
JScrollPane = new JScrollPane( textArea );
JPanel content = new JPanel( new BorderLayout() );
content.add(scrollPane, BorderLayout.CENTER);
content.add(imad, BorderLayout.PAGE_END);
setContentPane( content );
For a better solution, start with the working example found in the Swing tutorial on How to Use Text Areas and then modify the code. This way you will start with a better structured program that follows Swing standards.
With this code I will have the following window. I created 2 panels and added the mainp one to the frame and the panel to the mainp I did this in order to make window resizing dynamic (so the panel wont resize to the frame) I tried making my default panel size wider so that the text fields and label become wider but panel.setsize doesn't seem to do anything.
// creates the labels
studId = new JLabel("Student ID");
studAvg = new JLabel("Student Average");
studName = new JLabel("Student Name");
// creates the text fields
JTextField studIdText = new JTextField();
JTextField studAvgText = new JTextField();
JTextField studNameText = new JTextField();
JPanel mainp = new JPanel();
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(3, 2, 2, 2));
panel.setSize(300, 100);
// adds to the GridLayout
panel.add(studId);
panel.add(studIdText);
panel.add(studName);
panel.add(studNameText);
panel.add(studAvg);
panel.add(studAvgText);
mainp.add(panel);
add(BorderLayout.CENTER,mainp);
// verifies the textfields
studIdText.setInputVerifier(new IntVerifier());
studAvgText.setInputVerifier(new DoubleVerifier());
setTitle("Student Form");
setSize(300, 200);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
The method you are looking for is setPreferredSize. Use it instead of panel.setSize(300, 100).
panel.setPreferredSize(new Dimension(300, 100));
I would also recommend to not setting the size of your JFrame to the fixed value (300,200) but do pack() instead. This will set the size of your JFrame to fit the panels inside.
using Advise from #Dan and #MADprogrammer and #trashgod i came up with the following
JTextField studIdText = new JTextField(20);
JPanel panel = new JPanel();
panel.setLayout(new GridBagLayout());
GridBagConstraints r1 = new GridBagConstraints();
r1.fill = GridBagConstraints.HORIZONTAL;
r1.weightx = 0.0;
r1.gridx = 0;
r1.gridy = 0;
panel.add(studId,r1);
r1.weightx = 0.5;
r1.gridx = 1;
r1.gridwidth = GridBagConstraints.REMAINDER;
panel.add(studIdText,r1);
of course you can make GridBagConstraints for every row and just change the gridy
Set the layout for mainp as BorderLayout.
mainp.setLayout(new BorderLayout());
Then, in order to avoid having the textfields resize vertically and look strange, you can add panel to BorderLayout.NORTH, for instance.
mainp.add(panel, BorderLayout.NORTH);