Change JFrame background color - java

Created a JFrame and tried to change the background but it does not work.
I have looked at so many solutions here and on the Internet and it doesnt work , What is wrong in my code
JFrame frame = new JFrame("Process");
JLabel label = new JLabel("Please wait...");
JProgressBar pb = new JProgressBar();
pb.setIndeterminate(true);
pb.setBackground(new java.awt.Color(248, 201, 171));
frame.setBackground(new java.awt.Color(242, 186, 152));
frame.setSize(400, 200);
frame.setLocationRelativeTo(null);
JPanel panel = new JPanel();
panel.setBackground(new java.awt.Color(242, 186, 152));
GridBagConstraints c = new GridBagConstraints();
c.insets = new Insets(10,10,10,10); // make spaces between components on screen
c.gridx = 0;
c.gridy = 0;
c.gridwidth = 20;
panel.add(label, c);
c.gridx = 1;
c.gridy = 1;
c.gridwidth = 20;
panel.add(pb, c);
frame.add(panel);
frame.setVisible(true);

try frame.getContentPane().setBackground(new java.awt.Color(242, 186, 152));

don't work on the frame, work on the frame.getContentPane(). This is also where your sub panel and layout should be set. Besides, you use gridbag constraints but you never set a gridbag layout first. Meanwhile, you add to the frame and even if you did add to the content pane, you also didn't specify a content pane layout.

After doing changes in my code I found that the problem is caused by the GridBagConstraints c = new GridBagConstraints();
so i changed this by using panel.setLayout(null); then used setBounds to adjust the position of the components label.setBounds(100, 30, 250, 10);
and everything os ok

Related

How many vertical field details can I have in a Java swing application?

I am aware of the NORTH, CENTER and SOUTH field details for setting a component (e.g. BorderLayout) to either the top, middle or bottom respectively, however, I was wondering if it's possible to have more than three components and if so, how would I go about doing this with additional field details.
Currently, I have the following.
North - BorderLayout
Center - GridLayout
South - GridBagLayout
I am wanting to add an additional GridBagLayout to the bottom of the program (either below or above existing GridBagLayout). How would I go about adding an additional layout so that it does not interfere with and merge with the current SOUTH area?
Code:
public class boggleView extends JFrame {
public boggleView(){
super("Boggle");
setResizable(false);
setLocationRelativeTo(null);
setMinimumSize(new Dimension(400, 400));
JPanel northPanel = new JPanel();
JPanel middlePanel = new JPanel();
JPanel southPanel = new JPanel();
JPanel southPanelBottom = new JPanel();
// North Panel
getContentPane().add(northPanel, BorderLayout.NORTH);
northPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
JLabel gameBoardTitle = new JLabel("<html><font size = 5>Game Board</font></html>");
northPanel.add(gameBoardTitle);
// Middle Panel
getContentPane().add(middlePanel, BorderLayout.CENTER);
middlePanel.setLayout(new GridLayout(4,4));
for(int button=0 ; button<16 ; button++){
JButton diceButton = new JButton(String.valueOf("<html><font size = 10>E</font></html>"));
diceButton.setBackground(Color.WHITE);
diceButton.setBorder(new LineBorder(Color.BLACK));
diceButton.setPreferredSize(new Dimension(100, 100));
middlePanel.add(diceButton);
}
// South Panel
southPanel.setLayout (new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.insets = new Insets(10,10,0,10);
c.fill = GridBagConstraints.HORIZONTAL;
Border wordBorder = BorderFactory.createLineBorder(Color.BLACK, 1);
Border textFieldBorder = BorderFactory.createLineBorder(Color.BLACK, 2);
JButton button;
JLabel textField = new JLabel("");
textField.setBorder(textFieldBorder);
c.gridx = 0;
c.gridy = 0;
c.gridwidth = 8;
textField.setPreferredSize(new Dimension(0,35));
southPanel.add(textField, c);
button = new JButton("Submit");
c.gridx = 0;
c.gridy = 1;
c.gridwidth = 1;
button.setBackground(Color.GREEN);
button.setPreferredSize(new Dimension(80, 35));
southPanel.add(button, c);
button = new JButton("Cancel");
c.gridx = 2;
c.gridy = 1;
c.gridwidth = 1;
button.setBackground(Color.RED);
button.setPreferredSize(new Dimension(80, 35));
southPanel.add(button, c);
button = new JButton("Restart");
c.gridx = 4;
c.gridy = 1;
c.gridwidth = 1;
button.setBackground(Color.CYAN);
button.setPreferredSize(new Dimension(80, 35));
southPanel.add(button, c);
button = new JButton("Info");
c.gridx = 6;
c.gridy = 1;
c.gridwidth = 1;
button.setBackground(Color.CYAN);
button.setPreferredSize(new Dimension(80, 35));
southPanel.add(button, c);
JLabel wordListTitle = new JLabel("<html><font size = 5>Recent Words</font></html>");
c.gridx = 0;
c.gridy = 2;
c.gridwidth = 3;
c.insets = new Insets(0,5,0,0);
southPanel.add(wordListTitle, c);
// Bottom South Panel
southPanelBottom.setLayout (new GridBagLayout());
GridBagConstraints d = new GridBagConstraints();
d.fill = GridBagConstraints.HORIZONTAL;
d.insets = new Insets(0,0,0,0);
JLabel word = new JLabel("<html><font size = 5>HAT</font></html>");
word.setBorder(wordBorder);
d.gridx = 0;
d.gridy = 3;
d.gridwidth = 2;
southPanelBottom.add(word, d);
JLabel word2 = new JLabel("<html><font size = 5>1</font></html>");
word2.setBorder(wordBorder);
d.gridx = 2;
d.gridy = 3;
d.gridwidth = 1;
southPanelBottom.add(word2, d);
getContentPane().add(southPanel, BorderLayout.SOUTH);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
setVisible(true);
}
}
use a new helper Panel with layout Borderlayout.
put your new GridBagLayout panel to this new helper Panel's South and put the existing GridBagLayout panel (currently at SOUTH) this panel's North.
then put this new helper Panel to SOUTH replacing old GridBagLayout panel.
JPanel southHelperPanel = new JPanel(new BorderLayout());
southHelperPanel.add(southPanel, BorderLayout.NORTH);
southHelperPanel.add(southPanelBottom, BorderLayout.SOUTH);
getContentPane().add(southHelperPanel, BorderLayout.SOUTH);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
setVisible(true);
You can just combine layouts as you need them. Please have a look at the below example.. In this example, the main panel has a BorderLayout. In the different directions of this panel I'm adding new panels with other layouts.
public class Test extends JFrame {
public static void main(String[] args) {
Test test = new Test();
test.setVisible(true);
}
public Test() {
JPanel main = new JPanel(new BorderLayout());
JPanel top = new JPanel(new FlowLayout());
JPanel center = new JPanel(new GridBagLayout());
JPanel bottom = new JPanel(new BorderLayout());
main.add(top, BorderLayout.PAGE_START);
main.add(center, BorderLayout.CENTER);
main.add(bottom, BorderLayout.PAGE_END);
this.getContentPane().add(main);
this.setSize(800, 500);
}
}
You could also give your main panel a different layout (like GridBagLayout) if you need more "directions" in the beginning.
BorderLayout cannot itself do this; it can only lay out 3 things vertically. Switch the container (window.getContentPane(), presumably) to a different layout.
Something similar-ish to BorderLayout is to use BoxLayout; I'd need to know a little more about exactly what's in those subcomponents to be sure if it's the right replacement here.

Java JButton Size

I know there are some other questions similar to this but I haven't seen my specific question answered. I want to change the size of a JButton WITHOUT changing the look. For example if I use setPreferredSize, I lose the white background color and rounded corners. I just want to remove the surrounding padding so the button is only slightly bigger then it's label.
JFrame.setDefaultLookAndFeelDecorated(true);
JFrame jFrame = new JFrame("Label");
jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.insets = new Insets(10, 10, 10, 10);
JPanel jPanel = new JPanel(new GridBagLayout());
JButton jButtonUp = new JButton("Upd");
//jButtonUp.setPreferredSize(new Dimension(30, 20));
jPanel.add(jButtonUp, gbc);
gbc.gridy = 1;
JButton jButtonDn = new JButton("Del");
//jButtonDn.setPreferredSize(new Dimension(30, 20));
jPanel.add(jButtonDn, gbc);
jFrame.add(jPanel);
jFrame.pack();
jFrame.setVisible(true);
Using Mac OSX Sierra.
What I have is this:
What I get with setPreferredSize is this:
What I want is this:
Check this answer here, Simply do
After the line:
JButton jButtonUp = new JButton("Upd");
Add:
jButtonUp.setBorder(null);
jButtonUp.setMargin(new Insets(0,0,0,0));
Then add the button to the panel:
jPanel.add(jButtonUp, gbc);

Java - GridBagLayout position JLabel

I use a GridBagLayout and place a JLabel on it as the picture shows. I managed to set the size of the JLabel to the desired one via ipadx and ipady but I don't seem to work with the position of it. It seems it is always centered on the middle while I'd like to start from the red dot and don't reposition itself as I resize the window. What should I do?
Thanks. The code I use is also:
GridBagLayout gbl = new GridBagLayout();
setLayout(gbl);
GridBagConstraints gbc = new GridBagConstraints();
JLabel jl = new JLabel("This is a jlabel!", SwingConstants.CENTER);
jl.setBorder(BorderFactory.createLineBorder(Color.black));
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.ipadx = 87;
gbc.ipady = 220;
add(jl, gbc);
I improved it... now you can work with insets to position that label around JFrame, but after some research I would recommend to choose different layout manager.
So your code would look like:
Dimension d = new Dimension(350, 400);
GridBagLayout gbl = new GridBagLayout();
JFrame frame = new JFrame("Heloo");
frame.setLayout(gbl);
GridBagConstraints gbc = new GridBagConstraints();
JLabel jl = new JLabel("This is a jlabel!", SwingConstants.CENTER);
jl.setBorder(BorderFactory.createLineBorder(Color.black));
gbc.ipadx = 87;
gbc.ipady = 220;
gbc.insets = new Insets(0, 0, 360, 340);// here work with JFrame size!!
Hope this code will help you :)

GridBagLayout erratic resizing and displaying

I'm making a program that uses a GridBagLayout in a container. My primary use for this is to have two JPanels, which occupy 75% and 25% of the horizontal space of the window. For some reason though, the two panels look more like 90/10, and when resizing, the smaller one rapidly changes in size, between it's apparent minimum size, and what I believe is the desired 25%.
Here is the relevant code.
frmReedreadV = new JFrame();
frmReedreadV.setBounds(x, y, width, height);
frmReedreadV.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frmReedreadV.getContentPane().setLayout(new BoxLayout(frmReedreadV.getContentPane(), BoxLayout.Y_AXIS));
JPanel stretchyPanel = new JPanel();
frmReedreadV.getContentPane().add(stretchyPanel);
stretchyPanel.setLayout(new CardLayout(0, 0));
JPanel textAndUsers = new JPanel(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.fill = GridBagConstraints.BOTH;
gbc.weighty = 1;
textArea = new JTextArea();
textArea.setMargin(new Insets(2, 5, 5, 2));
textArea.setLineWrap(true);
textArea.setWrapStyleWord(true);
textArea.setEditable(false);
scrollPane = new JScrollPane(textArea);
scrollPane.setCursor(Cursor.getPredefinedCursor(Cursor.TEXT_CURSOR));
gbc.weightx = 0.8;
textAndUsers.add(scrollPane, gbc);
list = new FriendsList(listUpdate);
gbc.weightx = 0.2;
textAndUsers.add(list.frmUserList, gbc);
stretchyPanel.add(textAndUsers);
FriendsList is a JList contained in a JPanel.
There are other buttons and text fields in the main CardLayout content pane, but those shouldn't affect what is inside of this GridBagLayout, correct?
I made another copy of this JPanel as a standalone application, and it displays and resizes perfectly. See here:
JFrame frame = new JFrame();
frame.getContentPane().setLayout((new BoxLayout(frame.getContentPane(), BoxLayout.Y_AXIS)));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setBounds(100, 100, 550, 600);
JPanel stretchyPane = new JPanel();
frame.getContentPane().add(stretchyPane);
stretchyPane.setLayout(new CardLayout(0, 0));
JPanel panel = new JPanel(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
JTextArea text = new JTextArea();
text.setMargin(new Insets(2, 5, 5, 2));
JScrollPane panel1 = new JScrollPane(text);
FriendsList panel2 = new FriendsList(new Object());
c.fill = GridBagConstraints.BOTH;
c.weightx = .8;
c.weighty = 1;
panel.add(panel1, c);
c.weightx = .2;
//c.fill = GridBagConstraints.HORIZONTAL;
panel.add(panel2.frmUserList, c);
stretchyPane.add(panel);
frame.setVisible(true);
What could be causing the difference between the two, since I've replicated my original line by line into the copy?
The weightx and weighty properties might appear to act as proportional sizes, but that is not what they do. In fact they determine the distribution of extra space in the layout.
If you set everything to its preferred size by calling pack() on your JFrame, there will be no extra space. Which means the weightx and weighty properties have no effect while it's in that state.
Once the user starts resizing the window to be larger, there will be extra space, and only then will GridBagLayout consult the weightx and weighty properties to determine how to apportion that extra space to each column and row. Until then, it's entirely possible for a component with a small weightx to be wider than a component with a larger weightx, if their preferred sizes dictate it.
Hopefully this simple program will demonstrate this concept. Try using the mouse (or keyboard) to resize the window to be wider, and observe how each of the textfields grows:
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class GridBagProportions {
static void buildAndShowWindow() {
JTextField small = new JTextField("small (0.8)", 5);
JTextField large = new JTextField("LARGE (0.2)", 30);
small.setMinimumSize(small.getPreferredSize());
large.setMinimumSize(large.getPreferredSize());
JPanel panel = new JPanel(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.insets.left = 6;
gbc.insets.top = 6;
gbc.insets.bottom = 6;
gbc.weightx = 0.8;
panel.add(small, gbc);
gbc.weightx = 0.2;
gbc.insets.right = 6;
panel.add(large, gbc);
JFrame frame = new JFrame("GridBagLayout Proportions");
frame.getContentPane().add(panel);
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
buildAndShowWindow();
}
});
}
}
So what can be done about it? Well, this is one layout scenario that GridBagLayout cannot do. I would try using a SpringLayout:
SpringLayout layout = new SpringLayout();
JPanel textAndUsers = new JPanel(layout);
SpringLayout.Constraints scrollPaneConstraints =
new SpringLayout.Constraints(scrollPane);
Spring scrollPaneWidth = scrollPaneConstraints.getWidth();
SpringLayout.Constraints listConstraints =
new SpringLayout.Constraints(scrollPaneWidth,
scrollPaneConstraints.getY(),
Spring.scale(scrollPaneWidth, 0.25f),
scrollPaneConstraints.getHeight());
layout.putConstraint(SpringLayout.EAST, textAndUsers, 0,
SpringLayout.EAST, frmUserList);
layout.putConstraint(SpringLayout.SOUTH, textAndUsers, 0,
SpringLayout.SOUTH, scrollPane);
textAndUsers.add(scrollPane, scrollPaneConstraints);
textAndUsers.add(frmUserList, listConstraints);
Notice that the creation of listConstraints specifies a width argument which is Spring.scale(scrollPaneWidth, 0.25f). This ensures the list is always one-fourth as wide as the scrollPane containing the JTextArea.
SpringLayout is tricky to use, in that you have to make sure to link the far edges of the layout container to child components explicitly, because SpringLayout won't grow to accommodate all the child components automatically. That's what the putConstraint calls are doing.

GridBagLayout - Height of one row causes the width of next row to change

The UI I am working on displays a panel which lets a user select a movie and play. There are controls to play, pause, etc.
The layout seems to look the way I want. The panel uses a GridBagLayout. Row 2 displays a text area for status messages and row 3 displays a panel with buttons and a progress bar.
The problem I am running into is that when I have too many lines of text in the text area, the buttons in row 3 wrap around. This is irrespective of the height of the outer frame.
The height in row 2 is affecting the width in row 3. I don't understand this behavior. I am wondering if someone can tell me what is it that I am doing wrong and how I can fix it? I have attached the code.
On a slightly different topic, if you are looking at the code, can you also suggest a way to leave a margin between the bottom-most component and the outermost panel?
Thank you in advance for your help.
Regards,
Peter
private static JButton CreateImageButton(String fileName) {
JButton retVal = new JButton("xxx");
return retVal;
}
public MoviePanel() {
this.setLayout(new GridBagLayout());
this.setBackground(Color.WHITE);
JButton btnRefresh = CreateImageButton("refresh.png");
GridBagConstraints c = new GridBagConstraints();
c.gridx=0;
c.gridy=0;
c.fill = GridBagConstraints.NORTH;
c.insets.left = 10; c.insets.right = 10; c.insets.top = 10;
this.add(btnRefresh, c);
JComboBox cbMovieList = new JComboBox();
c = new GridBagConstraints();
c.gridx = 1;
c.gridy = 0;
c.fill = GridBagConstraints.HORIZONTAL;
c.insets.right = 10; c.insets.top = 10;
c.weightx = 1.0;
this.add(cbMovieList, c);
JButton btnAuthorize = new JButton("Get Info");
c = new GridBagConstraints();
c.gridx = 1;
c.gridy = 1;
c.anchor = GridBagConstraints.WEST;
c.insets.top = 10;
this.add(btnAuthorize, c);
JTextArea txtInfo = new JTextArea();
txtInfo.setFont( new Font("SansSerif", Font.BOLD, 12));
txtInfo.setBackground(Color.cyan);
// txtInfo.setText("abc\ndef");
txtInfo.setText("abc\ndef\nghi\njkl\nmno\npqr\nstu\nvwx\nyz");
c = new GridBagConstraints();
c.gridx = 1;
c.gridy = 2;
c.anchor = GridBagConstraints.NORTHWEST;
c.weighty = 1.0;
c.insets.top = 10;
this.add(txtInfo, c);
JPanel controllerOuter = new JPanel();
controllerOuter.setLayout(new BoxLayout(controllerOuter, BoxLayout.Y_AXIS));
controllerOuter.setBorder(BorderFactory.createRaisedBevelBorder());
FlowLayout controllerLayout = new FlowLayout(FlowLayout.CENTER);
controllerLayout.setHgap(0);
JPanel controller = new JPanel(controllerLayout);
controller.setBorder(new EmptyBorder(10, 10, 10, 10));
Dimension dim = new Dimension(60, 40);
JButton btnPlay = CreateImageButton("play.png");
btnPlay.setPreferredSize(dim);
controller.add(btnPlay);
JButton btnPause = CreateImageButton("pause.png");
btnPause.setPreferredSize(dim);
controller.add(btnPause);
JButton btnStop = CreateImageButton("stop.png");
btnStop.setPreferredSize(dim);
controller.add(btnStop);
JButton btnForward = CreateImageButton("forward.png");
btnForward.setPreferredSize(dim);
controller.add(btnForward);
JComboBox cbAspectRatio = new JComboBox();
cbAspectRatio.setPreferredSize(new Dimension(100, 40));
cbAspectRatio.setBorder(new EmptyBorder(0, 10, 0, 0));
controller.add(cbAspectRatio);
controllerOuter.add(controller);
JProgressBar pbProgress = new JProgressBar(0, 100);
pbProgress.setPreferredSize(new Dimension(350, 40));
pbProgress.setBorder(new EmptyBorder(0, 10, 10, 10));
pbProgress.setValue(50);
pbProgress.setString("50/100");
pbProgress.setStringPainted(true);
pbProgress.setForeground(Color.BLUE);
pbProgress.setBorderPainted(true);
controllerOuter.add(pbProgress);
c = new GridBagConstraints();
c.gridx = 0;
c.gridy = 3;
c.gridwidth = 2;
c.weightx = 1.0;
this.add(controllerOuter, c);
}
I see several things in your code:
You force the preferredSize of the JButton's. If possible, I would remove that because this will often get you more problems than solutions. If you want to force the preferredSize, you should also pay attention to set the minimum and maximum sizes as well, otherwise you get weird behaviour like the one you are observing
You use a BoxLayout to display the controls. While this is perfectly acceptable, BoxLayout also relies on min/max size to perform the layout, which you did not set.
You use imbricated layouts. This is fine too, but why not use only the GridBagLayout of your MoviePanel?
Usually TextAreas are wrapped in JScrollPane, in case the text is too big. You can also setLineWrap(true) on the TextArea, so that it does not go too far on the right. By setting rows/columns on the TextArea, you will define its preferreSize (to prevent it from depending of the text it contains).
On your GridBagConstraints, the fill property can only be: NONE, VERTICAL, HORIZONTAL or BOTH (You used VERTICAL for one of them). Also, it is not needed to recreate a new instance, you can reuse the same GridBagConstraint over and over, it is automatically cloned by the LayoutManager when you set the constraint for the component.
Now for the solutions, I found several:
When you add the contollerOuter, also specify c.fill = GridBagConstraints.HORIZONTAL; (This is the easiest way to solve your issues)
When you set the preferredSize of the JButtons, also force their minimumSize to the same value.
Use only the GridBagLayout to layout all components. (This would be my favorite)
Replace the FlowLayout by a BoxLayout with a X_AXIS.
Rember that GridBagConstraints properties :
gridx, gridy: specifies the location
gridwidth, gridheight: specifies the colspan/rowspan
weightx, weighty: specifies who gets the extra horizontal/vertical space and in what proportion
anchor: specifies the alignement of the component withing its "cell", if the "cell" is bigger than the component
fill: specifies if the component should stretch to the cell width/height
Just adding one JPanel each for Center and Bottom will do the trick for you, so till your JTextArea your GridBagLayout will server the purpose and after that the BorderLayout of the MAIN JPanel will do. Moreover, adding JScrollPane also to the whole thing reduces the effort needed at other areas. Have a look at the code and output :
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
public class JTextPaneExample extends JPanel
{
private Icon info = UIManager.getIcon("OptionPane.informationIcon");
private Icon error = UIManager.getIcon("OptionPane.errorIcon");
private static JButton CreateImageButton(String fileName) {
JButton retVal = new JButton("xxx");
return retVal;
}
private void createAndDisplayGUI()
{
JFrame frame = new JFrame("JTextPane Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLayout(new BorderLayout());
this.setBackground(Color.WHITE);
JPanel centerPanel = new JPanel();
centerPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
centerPanel.setLayout(new GridBagLayout());
centerPanel.setBackground(Color.WHITE);
JButton btnRefresh = CreateImageButton("refresh.png");
GridBagConstraints c = new GridBagConstraints();
c.gridx=0;
c.gridy=0;
c.fill = GridBagConstraints.NORTH;
c.insets.left = 10; c.insets.right = 10; c.insets.top = 10;
centerPanel.add(btnRefresh, c);
JComboBox cbMovieList = new JComboBox();
c = new GridBagConstraints();
c.gridx = 1;
c.gridy = 0;
c.fill = GridBagConstraints.HORIZONTAL;
c.insets.right = 10; c.insets.top = 10;
c.weightx = 1.0;
centerPanel.add(cbMovieList, c);
JButton btnAuthorize = new JButton("Get Info");
c = new GridBagConstraints();
c.gridx = 1;
c.gridy = 1;
c.anchor = GridBagConstraints.WEST;
c.insets.top = 10;
centerPanel.add(btnAuthorize, c);
JTextArea txtInfo = new JTextArea();
txtInfo.setFont( new Font("SansSerif", Font.BOLD, 12));
txtInfo.setBackground(Color.cyan);
// txtInfo.setText("abc\ndef");
txtInfo.setText("abc\ndef\nghi\njkl\nmno\npqr\nstu\nvwx\nyz");
JScrollPane scroller = new JScrollPane();
scroller.setViewportView(txtInfo);
c = new GridBagConstraints();
c.gridx = 1;
c.gridy = 2;
c.anchor = GridBagConstraints.NORTHWEST;
c.fill = GridBagConstraints.HORIZONTAL;
c.weighty = 1.0;
c.insets.top = 10;
centerPanel.add(scroller, c);
JPanel controllerOuter = new JPanel();
controllerOuter.setLayout(new BoxLayout(controllerOuter, BoxLayout.Y_AXIS));
controllerOuter.setBorder(BorderFactory.createRaisedBevelBorder());
FlowLayout controllerLayout = new FlowLayout(FlowLayout.CENTER);
controllerLayout.setHgap(0);
JPanel controller = new JPanel(controllerLayout);
controller.setBorder(new EmptyBorder(10, 10, 10, 10));
Dimension dim = new Dimension(60, 40);
JButton btnPlay = CreateImageButton("play.png");
btnPlay.setPreferredSize(dim);
controller.add(btnPlay);
JButton btnPause = CreateImageButton("pause.png");
btnPause.setPreferredSize(dim);
controller.add(btnPause);
JButton btnStop = CreateImageButton("stop.png");
btnStop.setPreferredSize(dim);
controller.add(btnStop);
JButton btnForward = CreateImageButton("forward.png");
btnForward.setPreferredSize(dim);
controller.add(btnForward);
JComboBox cbAspectRatio = new JComboBox();
cbAspectRatio.setPreferredSize(new Dimension(100, 40));
cbAspectRatio.setBorder(new EmptyBorder(0, 10, 0, 0));
controller.add(cbAspectRatio);
controllerOuter.add(controller);
JProgressBar pbProgress = new JProgressBar(0, 100);
pbProgress.setPreferredSize(new Dimension(350, 40));
pbProgress.setBorder(new EmptyBorder(0, 10, 10, 10));
pbProgress.setValue(50);
pbProgress.setString("50/100");
pbProgress.setStringPainted(true);
pbProgress.setForeground(Color.BLUE);
pbProgress.setBorderPainted(true);
controllerOuter.add(pbProgress);
add(centerPanel, BorderLayout.CENTER);
add(controllerOuter, BorderLayout.PAGE_END);
frame.getContentPane().add(this);
frame.pack();
frame.setVisible(true);
}
public static void main(String... args)
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
new JTextPaneExample().createAndDisplayGUI();
}
});
}
}
Here is the output as you add more lines :

Categories