Place components to the right of tabs in a JTabbedPane - java

OK I'm trying to modify an existing answer from below link to fit my needs. In my case I need to set the JTabbedPane.TOP, but then the buttons disappear.
I'm not too familiar with Swing, so someone please let me know. Below is the full working code example from link.
How to place components beneath tabs in right oriented JTabbedPane
public class RightTabPaneButtonPanel {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
new RightTabPaneButtonPanel().makeUI();
}
});
}
public void makeUI() {
JTabbedPane tabbedPane = new JTabbedPane();
tabbedPane.setTabPlacement(JTabbedPane.TOP);
JPanel panel = new JPanel(new GridLayout(1, 0));
for (int i = 0; i < 3; i++) {
JPanel tab = new JPanel();
tab.setName("tab" + (i + 1));
tab.setPreferredSize(new Dimension(400, 400));
tabbedPane.add(tab);
JButton button = new JButton("B" + (i + 1));
button.setMargin(new Insets(0, 0, 0, 0));
panel.add(button);
}
JFrame frame = new JFrame();
frame.add(tabbedPane);
frame.pack();
Rectangle tabBounds = tabbedPane.getBoundsAt(0);
Container glassPane = (Container) frame.getGlassPane();
glassPane.setVisible(true);
glassPane.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.weightx = 1.0;
gbc.weighty = 1.0;
gbc.fill = GridBagConstraints.NONE;
int margin = tabbedPane.getWidth() - (tabBounds.x + tabBounds.width);
gbc.insets = new Insets(0, 0, 0, margin);
gbc.anchor = GridBagConstraints.SOUTHEAST;
panel.setPreferredSize(new Dimension((int) tabBounds.getWidth() - margin,
panel.getPreferredSize().height));
glassPane.add(panel, gbc);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}

Here is an approach that uses the OverlayLayout:
import java.awt.*;
import javax.swing.*;
import javax.swing.plaf.*;
public class TabbedPaneWithComponent
{
private static void createAndShowUI()
{
JPanel panel = new JPanel();
panel.setLayout( new OverlayLayout(panel) );
JTabbedPane tabbedPane = new JTabbedPane();
tabbedPane.add("1", new JTextField("one"));
tabbedPane.add("2", new JTextField("two"));
tabbedPane.setAlignmentX(1.0f);
tabbedPane.setAlignmentY(0.0f);
JCheckBox checkBox = new JCheckBox("Check Me");
checkBox.setOpaque( false );
checkBox.setAlignmentX(1.0f);
checkBox.setAlignmentY(0.0f);
JPanel right = new JPanel( new FlowLayout(FlowLayout.RIGHT, 5, 0) );
right.setOpaque( false );
right.setAlignmentX(1.0f);
right.setAlignmentY(0.0f);
right.add( new JCheckBox("Check Me") );
right.add( new JCheckBox("Or Check Me") );
right.setMaximumSize( right.getPreferredSize() );
panel.add( checkBox );
// panel.add( right );
panel.add(tabbedPane);
JFrame frame = new JFrame("TabbedPane With Component");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add( panel );
frame.setLocationByPlatform( true );
frame.setSize(400, 100);
frame.setVisible( true );
System.out.println(checkBox.getPreferredSize() + " : " + right.getPreferredSize());
}
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
createAndShowUI();
}
});
}
}

I guess this is what you want:
GridBagConstraints gbc = new GridBagConstraints();
gbc.weightx = 1.0;
gbc.weighty = 1.0;
gbc.fill = GridBagConstraints.NONE;
gbc.anchor = GridBagConstraints.NORTHEAST;
panel.setPreferredSize(new Dimension(panel.getPreferredSize().width,
(int) tabBounds.getHeight()));
glassPane.add(panel, gbc);

Related

why sometimes swing JPanel layout is automaticly resized to minimum

when I double click the swing jar,it displays normal .button,textfield and label are displayed as expected.
but after some time,JPanel is automaticly resized to minimum as below shows.
I have already set frame setResizable(false)
anyone can give some ideas?
public TestSwing() {
super(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
log = new JTextArea(18, 45);
log.setMargin(new Insets(5, 5, 5, 5));
log.setEditable(true);
logScrollPane = new JScrollPane(log);
DefaultCaret caret = (DefaultCaret) log.getCaret();
caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE);
btn_source = new JButton("open");
btn_dest = new JButton("open");
lbl_sourceFile = new JLabel("Source Excel File:");
lbl_destFile = new JLabel("Destination XML Folder:");
txt_source = new JTextField(45);
txt_dest = new JTextField(45);
btn_convert = new JButton("Convert");
c.fill = GridBagConstraints.VERTICAL;
c.insets = new Insets(0, 0, 0, 0);
c.gridx = 0;
c.gridy = 0;
c.gridwidth = 1;
add(lbl_sourceFile, c);
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 1;
c.gridy = 0;
c.gridwidth = 1;
add(txt_source, c);
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 2;
c.gridy = 0;
c.insets = new Insets(0, 10, 2, 0);
c.gridwidth = 1;
add(btn_source, c);
}
public void actionPerformed(ActionEvent e) {}
private static void createAndShowGUI() {
JFrame frame = new JFrame("TestLink Converter");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TestSwing());
frame.setLocationRelativeTo(null);
frame.setResizable(false);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
UIManager.put("swing.boldMetal", Boolean.FALSE);
createAndShowGUI();
}
});
}
}
frame.add(new TLSwing());
We don't know what "TLSing()" class is, but I would guess it is a panel that uses a GridBagLayout. When there is no enough space to show the component at its preferred size, the component will be displayed at its minimum size so your text components shrink.
I have already set frame setResizable(false)
You should set the resize state BEFORE you pack() the frame.
frame.setResizable(false);
frame.pack();
frame.setVisible(true);
//frame.setResizable(false);

Is there an easier way to shift 'older' components down when adding newer components (whenever a button is pressed)?

I've tried looking for an answer to my question, but couldn't find anything similar. If it's already been asked, please link. Thanks in advance.
The layout of the main panel, mainPanel, is GridBagLayout. It has three buttons. Two of them are duds (for the purpose of this question). The middle button, butt2, creates a JPanel with other components in it every time butt2 is pressed.
Because butt2 is in the middle, and butt3 is directly below it, I have an int variable, tracker2, that tracks the gridy of butt2. Every time butt2 is pressed, I create a new JPanel that goes under butt2, increment tracker2, and then remove butt3 and add it below the newer component.
import java.util.List;
import java.util.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import javax.swing.*;
public class Demo implements ActionListener
public static void main(String[] args) {
Demo demo = new Demo();
}
private JFrame frame;
private JPanel mainPanel;
private JButton butt1, butt2, butt3;
private GridBagConstraints gb;
private List<JTextField> list;
private int count, tracker2;
public Demo() {
frame = new JFrame("Demo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(true);
frame.setBounds(0, 0, 800, 800);
list = new ArrayList<JTextField>();
count = 0;
tracker2 = 0;
commence();
}
private void commence() {
gb = new GridBagConstraints();
gb.anchor = GridBagConstraints.FIRST_LINE_START;
gb.weightx = 1;
gb.insets = new Insets(50, 5, 0, 20);
mainPanel = new JPanel();
mainPanel.setBackground(Color.white);
mainPanel.setLayout( new GridBagLayout() );
butt1 = new JButton("One");
butt1.setPreferredSize( new Dimension(100, 50) );
// Add to panel
gb.gridx = 0;
gb.gridy = 0;
mainPanel.add( butt1, gb);
butt2 = new JButton("Two");
butt2.setPreferredSize( new Dimension(100, 50) );
butt2.addActionListener(this);
// Add to panel
gb.gridy++;
tracker2 = gb.gridy;
mainPanel.add( butt2, gb );
butt3 = new JButton("Three");
butt3.setPreferredSize( new Dimension(100, 50) );
// Add to panel
gb.gridy++;
mainPanel.add( butt3, gb );
frame.add(mainPanel);
frame.setVisible(true);
frame.repaint();
}
#Override
public void actionPerformed(ActionEvent e) {
if(e.getSource().equals( butt2 )) {
commence2();
}
}
private void commence2() {
gb.insets = new Insets( 0, 0, 0, 0 );
list.add( new JTextField(30) );
JLabel label = new JLabel("LABEL 2 ");
label.setDisplayedMnemonic( KeyEvent.VK_N );
label.setLabelFor( list.get(count) );
JPanel panel = new JPanel( new FlowLayout(FlowLayout.LEFT, 10, 3));
panel.setBackground( Color.white );
panel.add(label);
panel.add(list.get( count ));
// Add to mainPanel
tracker2++;
gb.gridy = tracker2;
mainPanel.add( panel, gb );
updateFrame();
// Increment count
count++;
frame.revalidate();
frame.repaint();
}
private void updateFrame() {
mainPanel.remove( butt3 );
gb.insets = new Insets(50, 5, 0, 20);
gb.gridy = tracker2 + 1;
mainPanel.add( butt3, gb );
}
}
Is there an easier way to do this or a Layout that automatically does this for me?
Yes, there is an easier way. Instead of adding the new text fields to your mainPanel use an additional Container. E.g.
public class Demo2 implements ActionListener {
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
new Demo2();
}
});
}
private JFrame frame;
private JPanel textPanel;
private JButton butt1, butt2, butt3;
public Demo2() {
frame = new JFrame("Demo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(true);
frame.setBounds(0, 0, 800, 800);
commence();
}
private void commence() {
GridBagConstraints gb = new GridBagConstraints();
gb.anchor = GridBagConstraints.FIRST_LINE_START;
gb.weightx = 1;
gb.insets = new Insets(50, 5, 0, 20);
JPanel mainPanel = new JPanel();
mainPanel.setBackground(Color.white);
mainPanel.setLayout( new GridBagLayout() );
butt1 = new JButton("One");
butt1.setPreferredSize( new Dimension(100, 50) );
// Add to panel
gb.gridx = 0;
gb.gridy = 0;
mainPanel.add( butt1, gb);
butt2 = new JButton("Two");
butt2.setPreferredSize( new Dimension(100, 50) );
butt2.addActionListener(this);
// Add to panel
gb.gridy++;
gb.insets = new Insets(50, 5, 0, 0);
mainPanel.add( butt2, gb );
textPanel = new JPanel(new GridLayout(0, 1));
// Add to panel
gb.gridy++;
gb.insets = new Insets(0, 5, 0, 20);
mainPanel.add( textPanel, gb );
butt3 = new JButton("Three");
butt3.setPreferredSize( new Dimension(100, 50) );
// Add to panel
gb.gridy++;
gb.insets = new Insets(50, 5, 0, 20);
mainPanel.add( butt3, gb );
frame.add(mainPanel);
frame.setVisible(true);
frame.repaint();
}
#Override
public void actionPerformed(ActionEvent e) {
if(e.getSource().equals( butt2 )) {
commence2();
}
}
private void commence2() {
JTextField jtf = new JTextField(30);
JLabel label = new JLabel("LABEL 2 ");
label.setDisplayedMnemonic(KeyEvent.VK_N );
label.setLabelFor( jtf );
JPanel panel = new JPanel( new FlowLayout(FlowLayout.LEFT, 10, 3));
panel.setBackground( Color.white );
panel.add(label);
panel.add( jtf );
// Add to mainPanel
textPanel.add( panel );
textPanel.revalidate();
frame.revalidate();
frame.repaint();
}
}
In the above code, textPanel serves as container for the new text fields.

How to use any other layout manager for jscrollpane with it instead of using null layout

I had created this view by using null layout:
This is the Code i had used to create above layout
public class Page1311 extends JPanel {
// private JTable table;
// public JScrollPane pane=null;
public JPanel panel=null;
public JButton back=null;
/**
* Create the panel.
*/
public Page1311() {
setLayout(null);
back = new JButton("back");
back.setFont(new Font("Arial", Font.PLAIN, 20));
back.setBounds(10,10, 150, 27);
add(back);
List<List<String>> list=new ArrayList<List<String>>();
for(int j=0;j<=5;j++)
{
List<String> list1=new ArrayList<>();
list1.add("Honda Showroom"+j);
list1.add("Mandsaur");
list1.add("25 Chakrawati Colony Railway Station Road");
list1.add("Activa");
list1.add("2017");
list1.add("Honda");
list.add(list1);
}
getLayout(list,this);
back = new JButton("back");
back.setFont(new Font("Arial", Font.PLAIN, 20));
back.setBounds(10,10, 150, 27);
add(back);
}
public static void getLayout(List<List<String>> list,JPanel pane)
{
int i=0;
int x=100;
int y=100;
int height=20;
int width=200;
int size=list.size();
JLabel[] lblSName=new JLabel[size];
JLabel[] lblSAddress=new JLabel[size];
JLabel[] lblSCity=new JLabel[size];
JLabel[] lblVName=new JLabel[size];
JLabel[] lblVVersion=new JLabel[size];
JLabel[] lblVCompanies=new JLabel[size];
JButton[] lblGo=new JButton[size];
Iterator<List<String>> it=list.iterator();
while(it.hasNext())
{
System.out.println(x+" "+y+" "+width+" "+height+" "+i);
Iterator iit=it.next().iterator();
lblSName[i]= new JLabel();
lblSName[i].setText("Name:"+iit.next());
lblSName[i].setFont(new Font("Monotype Corsiva", Font.ITALIC, 20));
lblSName[i].setBounds(x,y,width,height);
pane.add(lblSName[i]);
lblSCity[i] = new JLabel();
lblSCity[i].setText("City:"+iit.next());
lblSCity[i].setFont(new Font("Monotype Corsiva", Font.ITALIC, 20));
System.out.println((x+240)+" "+y+" "+width+" "+height+" "+i);
lblSCity[i].setBounds(x+240,y,width,height);
pane.add(lblSCity[i]);
lblSAddress[i]= new JLabel();
lblSAddress[i].setText("Address:"+iit.next());
lblSAddress[i].setFont(new Font("Monotype Corsiva", Font.ITALIC, 20));
System.out.println((x+470)+" "+y+" "+(width+256)+" "+height+" "+i);
lblSAddress[i].setBounds(x+470,y, width+256, height);
pane.add(lblSAddress[i]);
lblVName[i]= new JLabel();
lblVName[i].setText("Vehicle Name:"+iit.next());
lblVName[i].setFont(new Font("Monotype Corsiva", Font.ITALIC, 20));
System.out.println(x+" "+(y+35)+" "+width+" "+height+" "+i);
lblVName[i].setBounds(x,y+35,width, height);
pane.add(lblVName[i]);
lblVVersion[i] = new JLabel();
lblVVersion[i].setText("Vehicle Version:"+iit.next());
lblVVersion[i].setFont(new Font("Monotype Corsiva", Font.ITALIC, 20));
System.out.println((x+240)+" "+y+35+" "+width+" "+height+" "+i);
lblVVersion[i].setBounds(x+240,y+35, width, height);
pane.add(lblVVersion[i]);
lblVCompanies[i]= new JLabel();
lblVCompanies[i].setText("Vehicle Companies:"+iit.next());
lblVCompanies[i].setFont(new Font("Monotype Corsiva", Font.ITALIC, 20));
System.out.println((x+470)+" "+(y+35)+" "+(width+256)+" "+height+" "+i);
lblVCompanies[i].setBounds(x+470,y+35, width+256,height);
pane.add(lblVCompanies[i]);
lblGo[i]= new JButton("Go ");
lblGo[i].setFont(new Font("Monotype Corsiva", Font.ITALIC, 15));
System.out.println(x+" "+(y+70)+" "+(width-130)+" "+height+" "+i);
lblGo[i].setBounds(x,y+70,width-130, height);
pane.add(lblGo[i]);
i++;
y=y+160;
System.out.println("new height"+y);
}
}
public static void main(String[] args) {
JFrame frame=new JFrame();
frame.add(new Page1311());
frame.setExtendedState(frame.MAXIMIZED_BOTH);
frame.setLocation(0, 0);
frame.setVisible(true);
}
}
But now i found that i cant use jscrollpane with null layout manager.So i want to create same layout by using any other layout manager.Can any one please help with with this or you can provide me any other way to use jscrollpane with null layout manager.
Thanks in advance
I'd use a combination of a GridBagLayout for each 'show room' panel, then a single column GridLayout panel to stack the collection of show room panels in a single container. The second panel would go in the scroll pane.
import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
public class ShowRoomLayout {
private JComponent ui = null;
ShowRoomLayout() {
initUI();
}
private JPanel getShowRoomPanel(int num) {
JPanel p = new JPanel(new GridBagLayout());
p.setBorder(new TitledBorder("GridBagLayout"));
GridBagConstraints gbc = new GridBagConstraints();
gbc.insets = new Insets(5, 5, 5, 5);
gbc.anchor = GridBagConstraints.WEST;
p.add(new JLabel("Name:Honda Showroom" + num), gbc);
gbc.gridx = 1;
p.add(new JLabel("City:Mandsaur"), gbc);
gbc.gridx = 2;
p.add(new JLabel("Address:25 Chakrawati Colony Railway Station Road"), gbc);
gbc.gridy = 1;
gbc.gridx = 0;
p.add(new JLabel("Vehicle Name:Activa"), gbc);
gbc.gridx = 1;
p.add(new JLabel("Vehicle Version:2017"), gbc);
gbc.gridx = 2;
p.add(new JLabel("Vehicle Companies:Honda"), gbc);
gbc.gridy = 2;
gbc.gridx = 0;
p.add(new JButton("Go"), gbc);
return p;
}
public void initUI() {
if (ui != null) {
return;
}
ui = new JPanel(new BorderLayout(4, 4));
ui.setBorder(new EmptyBorder(4, 4, 4, 4));
JPanel pList = new JPanel(new GridLayout(0, 1, 3, 3));
pList.setBorder(new TitledBorder("GridLayout"));
for (int ii = 1; ii < 21; ii++) {
pList.add(getShowRoomPanel(ii));
}
JScrollPane scrollPane = new JScrollPane(pList,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
ui.add(scrollPane);
}
public JComponent getUI() {
return ui;
}
public static void main(String[] args) {
Runnable r = new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception useDefault) {
}
ShowRoomLayout o = new ShowRoomLayout();
JFrame f = new JFrame(o.getClass().getSimpleName());
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
f.setLocationByPlatform(true);
f.setContentPane(o.getUI());
f.pack();
Dimension d = f.getSize();
f.setSize(new Dimension(d.width, 400));
f.setMinimumSize(f.getSize());
f.setVisible(true);
}
};
SwingUtilities.invokeLater(r);
}
}
JScrollPane has its own layout: ScrollPaneLayout. If you want to use any other, you don't set it on the scroll pane itself, you put a JPanel inside it and set the layout on that panel.
Another way without the need to set any layout manager for the JScrollPane is the method
JScrollPane.setViewportView(Component);
This is my preferred way. No size or layout handling on the fly.
If you want to place several Components to the same JScrollPane, just put a JPanel in between. The JPanel can have your preferred Layout.
If your layout uses a null layout manager, you need to tell the scroll pane the size of your component
JPanel pane = new JPanel(){
Dimension d = new Dimension(400, 400);
#Override
public Dimension getMinimumSize(){
return d;
}
#Override
public Dimension getPreferredSize(){
return d;
}
#Override
public Dimension getMaximumSize(){
return d;
}
#Override
public void paintComponent(Graphics g){
g.setColor(Color.RED);
g.fill3DRect(25, 25, 350, 350, true);
}
};
pane.setLayout(null);
JFrame frame = new JFrame("check");
frame.setSize(200, 200);
frame.add(new JScrollPane(pane));
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
For this, I set the size of the pane to be 400, 400. The scroll pane will respect this. If you comment out the overriden methods, you'll see the scroll doesn't work anymore.
For OP to apply this technique they would just have to change the line where they add the panel to the JFrame.
frame.add(new JScrollPane(new Page1311()));
Since Page1311 doesn't use a layout manager then they need to override, getMin/Max/Preferred. as I did in my example. That would wrap the custom JPanel with a scroll pane, and the content would be scrollable.

Why does GridBagLayout provide strange result?

I want the various components to spread out and fill the entire window.
Have you tried anything else? Yes, I tried GridLayout but then the buttons look huge. I also tried pack() which made the window small instead. The window should be 750x750 :)
What I was trying is this:
These 4 buttons on the top as a thin strip
The scroll pane with JPanels inside which will contain all the video conversion tasks. This takes up the maximum space
A JPanel at the bottom containing a JProgressBar as a thin strip.
But something seems to have messed up somewhere. Please help me solve this
SSCCE
import java.awt.*;
import javax.swing.*;
import com.explodingpixels.macwidgets.*;
public class HudTest {
public static void main(String[] args) {
HudWindow hud = new HudWindow("Window");
hud.getJDialog().setSize(750, 750);
hud.getJDialog().setLocationRelativeTo(null);
hud.getJDialog().setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
JPanel buttonPanel = new JPanel(new FlowLayout());
JButton addVideo = HudWidgetFactory.createHudButton("Add New Video");
JButton removeVideo = HudWidgetFactory.createHudButton("Remove Video");
JButton startAll = HudWidgetFactory.createHudButton("Start All Tasks");
JButton stopAll = HudWidgetFactory.createHudButton("Stop All Tasks");
buttonPanel.add(addVideo);
buttonPanel.add(startAll);
buttonPanel.add(removeVideo);
buttonPanel.add(stopAll);
JPanel taskPanel = new JPanel(new GridLayout(0,1));
JScrollPane taskScrollPane = new JScrollPane(taskPanel);
IAppWidgetFactory.makeIAppScrollPane(taskScrollPane);
for(int i=0;i<10;i++){
ColorPanel c = new ColorPanel();
c.setPreferredSize(new Dimension(750,100));
taskPanel.add(c);
}
JPanel progressBarPanel = new JPanel();
JComponent component = (JComponent) hud.getContentPane();
component.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
Insets in = new Insets(2,2,2,2);
gbc.insets = in;
gbc.gridx = 0;
gbc.gridy = 0;
gbc.gridwidth = 10;
gbc.gridheight = 1;
gbc.fill = GridBagConstraints.BOTH;
component.add(buttonPanel,gbc);
gbc.gridy += 1;
gbc.gridheight = 17;
component.add(taskScrollPane,gbc);
gbc.gridy += 17;
gbc.gridheight = 2;
component.add(progressBarPanel,gbc);
hud.getJDialog().setVisible(true);
}
}
Use this
gbc.weightx = 1;
gbc.weighty = 1;
gbc.fill = GridbagConstraints.BOTH
Why not simply place three JPanels on top of one JPanel with BorderLayout as Layout Manager, where the middle JPanel with all custom panels with their respective sizes can be accommodated inside a JScrollPane, as shown in the below example :
import javax.swing.*;
import java.awt.*;
import java.util.Random;
/**
* Created with IntelliJ IDEA.
* User: Gagandeep Bali
* Date: 5/17/13
* Time: 6:09 PM
* To change this template use File | Settings | File Templates.
*/
public class PlayerBase
{
private JPanel contentPane;
private JPanel buttonPanel;
private JPanel centerPanel;
private CustomPanel[] colourPanel;
private JPanel progressPanel;
private JButton addVideoButton;
private JButton removeVideoButton;
private JButton startAllButton;
private JButton stopAllButton;
private JProgressBar progressBar;
private Random random;
public PlayerBase()
{
colourPanel = new CustomPanel[10];
random = new Random();
}
private void displayGUI()
{
JFrame playerWindow = new JFrame("Player Window");
playerWindow.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
contentPane = new JPanel(new BorderLayout(5, 5));
contentPane.setBorder(
BorderFactory.createEmptyBorder(5, 5, 5, 5));
buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 5, 5));
addVideoButton = new JButton("Add New Video");
removeVideoButton = new JButton("Remove Video");
startAllButton = new JButton("Start all tasks");
stopAllButton = new JButton("Stop all tasks");
buttonPanel.add(addVideoButton);
buttonPanel.add(removeVideoButton);
buttonPanel.add(startAllButton);
buttonPanel.add(stopAllButton);
contentPane.add(buttonPanel, BorderLayout.PAGE_START);
JScrollPane scroller = new JScrollPane();
centerPanel = new JPanel(new GridLayout(0, 1, 2, 2));
for (int i = 0; i < colourPanel.length; i++)
{
colourPanel[i] = new CustomPanel(new Color(
random.nextInt(255), random.nextInt(255)
, random.nextInt(255)));
centerPanel.add(colourPanel[i]);
}
scroller.setViewportView(centerPanel);
contentPane.add(scroller, BorderLayout.CENTER);
progressPanel = new JPanel(new BorderLayout(5, 5));
progressBar = new JProgressBar(SwingConstants.HORIZONTAL);
progressPanel.add(progressBar);
contentPane.add(progressPanel, BorderLayout.PAGE_END);
playerWindow.setContentPane(contentPane);
playerWindow.pack();
//playerWindow.setSize(750, 750);
playerWindow.setLocationByPlatform(true);
playerWindow.setVisible(true);
}
public static void main(String[] args)
{
Runnable runnable = new Runnable()
{
#Override
public void run()
{
new PlayerBase().displayGUI();
}
};
EventQueue.invokeLater(runnable);
}
}
class CustomPanel extends JPanel
{
public CustomPanel(Color backGroundColour)
{
setOpaque(true);
setBackground(backGroundColour);
}
#Override
public Dimension getPreferredSize()
{
return (new Dimension(750, 100));
}
}
OUTPUT :

Nested JPanels and GridBagLayout Not "Packing" Components

I've attached a screenshot for which the following Border legend applies:
Yellow = JPanel with BorderLayout
Blue = JPanel with GridBagLayout
Fuchsia = JPanel with FlowLayout
There are two panels not blocked out in colors that warrant mentioning:
1) The title panel where the word "Primary" is displayed; this panel is at BorderLayout.NORTH in "Yellow" panel.
2) The image panel where the image of the device is located; this panel is a sibling to "Fuchsia"
"Blue" is at BorderLayout.CENTER in "Yellow" while "Fuchsia" and the image panel are given the following constraints:
GridBagConstraints c = new GridBagConstraints();
c.weightx = 1.0;
c.weighty = 1.0;
c.gridx = 0;
c.gridy = 0;
c.anchor = GridBagConstraints.NORTHWEST;
c.insets = new Insets(0, 10, 0, 0);
c.fill = GridBagConstraints.BOTH;
//"Blue".add(imagePanel, c);
c.weighty = 0.80;
c.gridy = 1;
c.fill = GridBagConstraints.HORIZONTAL;
//"Blue".add("Fuchsia", c);
As you can probably tell from the image, I'm trying to get rid of the "wasted" space in "Blue" right below "Fuchsia". I don't seem to be able to do it with GridBagConstraints, so am I just using the wrong LayoutManager? It looks to me like "Blue", who is at CENTER in the BorderLayout is just giving each child JPanel half of the available space and reserving the remainder space instead of contracting upward. What am I missing here? Is this simply a matter of setting a preferred or maximum size on "Fuchsia"? it doesn't seem like that will get me where I want to be, since the border around "Fuchsia" (which is covered by my color coding) is where I want the end of the component to be.
Have a look at this output, from this code example :
import java.awt.*;
import javax.swing.*;
public class LayoutTest
{
private void displayGUI()
{
JFrame frame = new JFrame("Layout Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel contentPane = new JPanel();
contentPane.setOpaque(true);
contentPane.setBackground(Color.YELLOW);
contentPane.setLayout(new BorderLayout(2, 2));
JPanel topPanel = new JPanel();
topPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
JLabel headingLabel = new JLabel("Primary");
topPanel.add(headingLabel);
contentPane.add(topPanel, BorderLayout.PAGE_START);
JPanel centerPanel = new JPanel();
centerPanel.setOpaque(true);
centerPanel.setBackground(Color.BLUE);
centerPanel.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.anchor = GridBagConstraints.FIRST_LINE_START;
gbc.fill = GridBagConstraints.BOTH;
gbc.weightx = 1.0;
gbc.weighty = 0.2;
gbc.gridx = 0;
gbc.gridy = 0;
JPanel imagePanel = new JPanel();
JLabel imageLabel = null;
try
{
imageLabel = new JLabel(
new ImageIcon(
new java.net.URL(
"http://pscode.org/"
+ "tame/screenshot/"
+ "landscape/slider1.gif")));
}
catch(Exception e)
{
e.printStackTrace();
}
imagePanel.add(imageLabel);
centerPanel.add(imagePanel, gbc);
JPanel detailsPanel = new JPanel();
detailsPanel.setOpaque(true);
detailsPanel.setBackground(Color.WHITE);
detailsPanel.setBorder(
BorderFactory.createEmptyBorder(
5, 5, 5, 5));
detailsPanel.setLayout(new GridLayout(0, 1, 5, 5));
JLabel statusLabel = new JLabel("Chassis Status : ");
JLabel usageLabel = new JLabel("Bandwidth Usage : ");
JLabel fanLabel = new JLabel("Fan Status : ");
detailsPanel.add(statusLabel);
detailsPanel.add(usageLabel);
detailsPanel.add(fanLabel);
gbc.fill = GridBagConstraints.BOTH;
gbc.weighty = 0.8;
gbc.gridx = 0;
gbc.gridy = 1;
gbc.gridheight = 3;
centerPanel.add(detailsPanel, gbc);
contentPane.add(centerPanel, BorderLayout.CENTER);
frame.setContentPane(contentPane);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String... args)
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
new LayoutTest().displayGUI();
}
});
}
}
Without using GridBagLayout could be
import java.awt.BorderLayout;
import java.awt.Color;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.border.LineBorder;
public class NestedLayout {
private JFrame frame = new JFrame();
private JPanel yellowNorthPanel = new JPanel();
private JPanel yellowPanel = new JPanel();
private JPanel bluePanel = new JPanel();
private JPanel fuchsiaTopPanel = new JPanel();
private JPanel fuchsiaBottonPanel = new JPanel();
public NestedLayout() {
yellowNorthPanel.setBorder(new LineBorder(Color.yellow, 5));
yellowPanel.setLayout(new BorderLayout());
yellowPanel.setBorder(new LineBorder(Color.yellow, 5));
bluePanel.setLayout(new BorderLayout(5, 5));
bluePanel.setBorder(new LineBorder(Color.blue, 5));
fuchsiaTopPanel.setBorder(new LineBorder(Color.cyan, 5));
fuchsiaBottonPanel.setBorder(new LineBorder(Color.cyan, 5));
bluePanel.add(fuchsiaTopPanel, BorderLayout.NORTH);
bluePanel.add(fuchsiaBottonPanel);
yellowPanel.add(bluePanel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(yellowNorthPanel, BorderLayout.NORTH);
frame.add(yellowPanel);
//frame.pack();
frame.setSize(400, 300);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
new NestedLayout();
}
});
}
}

Categories