JTextArea stretches when setWrapLine - java

I have the following code: the problem is that when i run the application, the JPanel containing those components stretches because of setLineWrap(true) I've inserted the JTextArea in a JScrollPane as suggested here
http://docs.oracle.com/javase/tutorial/uiswing/components/textarea.html
but with no result
titolo=new JLabel("title");
text=new JTextArea("");
scrollPane = new JScrollPane(text);
text.setLineWrap(true);
griglia=new GridBagLayout();
gbc=new GridBagConstraints();
setLayout(griglia);
buildConstraints(gbc,0,0,1,1,100,1);
griglia.setConstraints(titolo,gbc);
buildConstraints(gbc,0,1,1,1,100,100);
griglia.setConstraints(scrollPane,gbc);
add(titolo);
add(scrollPane);

Check GridBagConstraint here in the fill - The initial fill value use GridBagConstraints.NONE.

Related

Java JScrollPane ve

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.

Aligning JLabel to Left or Right inside BoxLayout with Y_AXIS Constraint of JPanel

I have a JPanel with Constraint's of Y_Axis so that whenever I add a new Component it will automatically be Added on a new Line.But the Problem is that the Label inside is not Aligned to Left or Right. It is displayed at some distance above the JTable. How can JLabel be displayed at desired Alginment.
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel,BoxLayout.Y_AXIS));
Then I added a JLabel inside panel.
JLabel labelSemester = new JLabel("Semester 1: ",SwingConstants.LEFT);
panel.add(labelSemester);
After label, I added a new JTable inside panel,
// Column Names for the Table
Object[] col_names = {"ID", "Name", "CH", "Marks", "Grade"};
// row data for the table
Object[][] table_rows = {{"CS123","Introduction to Computing",3,80,"A-"}};// One row only
JTable table = new JTable(table_rows, col_names);
panel.add(new JScrollPane(table));
Then I added a JFrame and added the Panel to show in the frame
JFrame frame = new JFrame();
// frame Title
frame.setTitle("DMC");
frame.setSize(400,400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
// adding panel inside frame
frame.add(panel);
// displaying frame
frame.show()
Note:
I have added code for auto Adjustment of column width of JTable.
Output can be seen from attached Image
All components added to the BoxLayout need the same alignmentX, otherwise you can get some weird layouts:
//JLabel labelSemester = new JLabel("Semester 1: ",SwingConstants.LEFT);
JLabel labelSemester = new JLabel("Semester 1: ");
label.semester.setAlignmentX(JLabel.LEFT_ALIGNMENT);
panel.add(labelSemester);
...
JTable table = new JTable(table_rows, col_names);
//panel.add(new JScrollPane(table));
JScrollPane scrollPane = new JScrollPane( table );
scrollPane.setAlignmentX(JScrollPane.LEFT_ALIGNMENT);
panel.add( scrollPane );
Read the section from the Swing BoxLayout tutorial on Fixing Alignment Problems for more information. Keep a link to the tutorial handy for all Swing basics.

How to set left alignement JLabel in JPanel

I have a JPanel that I insert into my JFrame.
Now, I want to display a JLabel on the left at my JPanel. So I have this code:
JPanel panelHeader = new JPanel();
panelHeader.setBackground(new Color(52,151,236));
panelHeader.setPreferredSize(new Dimension(300,25));
GridBagConstraints GBC = new GridBagConstraints();
Container CR = new Container();
GridBagLayout GBL = new GridBagLayout();
CR.setLayout(GBL);
panelHeader.add(CR);
LabelFormat label = new LabelFormat("EasyManagement",new Font("Arial Narrow", Font.PLAIN, 16),Color.white);
GBC = new GridBagConstraints();
CR.add(label);
GBC.gridx=0;
GBC.gridy=0;
GBC.insets.left = 1;
GBC.insets.top=0;
GBC.fill = GridBagConstraints.BOTH;
GBC.anchor= GridBagConstraints.EAST;
GBL.setConstraints(label,GBC);
With this code, I'm not able to display the JLabel at the left on JPanel but I see it on the center.
How can I fixed it?
-You might wish to set the JLabel's horizontalAlignment property. One way is via its constructor. Try:
JLabel label = new JLabel(lText, SwingConstants.LEFT);
This can also be done via the expected setter method:
label.setHorizontalAlignment(SwingConstants.LEFT);
-You can also try using LEFT alignment for your JLabel JPanel container
myJPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
-Final way :
You could wrap the label in JPanel with FlowLayout.LEADING
JPanel wrapper = new JPanel(new FlowLayout(FlowLayout.LEADING,0, 0));
wrapper.add(Jlabel);
panel.add(wrapper);
Also remember to follow Java naming convention. variables begin with lower case letters using camel casing: Jlabel → jLabel

JScrollPane in Java

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.

how do you increase panel width?

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);

Categories