Adjusting GUI JPanels to fit - java

So for this program, I am trying to have a JToolBar on the left, and this spectrum panel on the right side. I am currently adding using a BorderLayout, but as you can see, the spectrum (in cyan) I add has a black border around it (the panel below). Why does it not fill the right side JPanel?
http://imgur.com/pTqMeGM
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Color;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JToolBar;
public class Spectrum extends JFrame{
/**
*
*/
private static final long serialVersionUID = 1L;
/*
*
*/
public Spectrum(){
JPanel main = new JPanel(new BorderLayout());
JPanel rightside = new JPanel();
JLabel spectrum = new JLabel("spectrum goes here");
JToolBar toolbar = new JToolBar(null, JToolBar.VERTICAL);
JButton button1 = new JButton("Icon 1");
JButton button2 = new JButton("Icon 2");
main.setBackground(Color.RED);
main.setPreferredSize(new Dimension(800, 500));
rightside.setBackground(Color.black);
spectrum.setPreferredSize(new Dimension(750,500));
spectrum.setOpaque(true);
spectrum.setBackground(Color.cyan);
toolbar.setPreferredSize(new Dimension(50, 500));
toolbar.setFloatable(false);
button1.setOpaque(true);
button2.setOpaque(true);
button1.setBackground(Color.blue);
button2.setBackground(Color.green);
toolbar.add(button1);
toolbar.add(button2);
rightside.add(spectrum);
main.add(toolbar, BorderLayout.WEST);
main.add(rightside, BorderLayout.EAST);
setContentPane(main);
pack();
setVisible(true);
}
}

I suggest you to do the following change.
main.add(rightside, BorderLayout.EAST);
to
main.add(rightside, BorderLayout.CENTER);

Related

Two JPanels instead of one

I tried to create a JFrame and put 5 JPanels on it.
The problem is, that the top panel appears twice.
This is the Frame class:
package Chess;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import Chess.NorthPanel;
public class Frame {
public static void main(String[] args) {
drawpanels();
}
public static void drawpanels() {
JFrame board=new JFrame("Board");
board.setLayout(new BorderLayout());
board.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
board.setVisible(true);
board.setSize(1000, 1000);
board.getContentPane().setBackground(new Color(224,224,224));
NorthPanel p1=new NorthPanel();
SouthPanel p2=new SouthPanel();
CenterPanel p3=new CenterPanel();
JPanel p4=new JPanel();
JPanel p5=new JPanel();
p4.setBackground(Color.green);
p5.setBackground(Color.red);
board.add(p1, BorderLayout.NORTH);
board.add(p2, BorderLayout.SOUTH);
board.add(p3, BorderLayout.CENTER);
board.add(p4, BorderLayout.EAST);
board.add(p5, BorderLayout.WEST);
board.validate();
}
}
And this is the NorthPanel class:
package Chess;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Image;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class NorthPanel extends JPanel{
private static final long serialVersionUID = 1L;
public NorthPanel() {
this.setBackground(new Color(128,128,128));
JLabel label=new JLabel();
label.setIcon(new ImageIcon("images/rlt.png"));
this.add(label);
}
}
(There are two other classes for SouthPanel and CenterPanel.)
And the result:
Why are there two rooks?
What other problems are there with this code?

ImageIcon padding and/or alignment in a JButton

*edited so it might be helpful to others:
I was struggling with why an ImageIcon was being padded in a JPanel, but not in a JToolBar even though they were added in the same way, using the same file (see the folder icon):
Cutting down the code to an sscce has shown that VGR's answer below is right - it's has to be how each component deals with JButtons, rather than the layout.
This code should run, but you'll have to have "images/open.png" in the source folder.
import java.awt.Color;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.URL;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JToolBar;
import javax.swing.border.Border;
class CandidatePanel extends JPanel{
private JPanel panel = new JPanel();
private JToolBar tb = new JToolBar();
private JButton tbButton = new JButton();
private JButton cvButton = new JButton();
public static void main(String[] args){
JFrame frame = new JFrame();
frame.add(new CandidatePanel());
frame.pack();
frame.setVisible(true);
}
public CandidatePanel(){
setLayout(new GridBagLayout());
tbButton.setIcon(new PanelHelper().createIcon("images/open.png", ""));
tb.add(tbButton);
cvButton.setIcon(new PanelHelper().createIcon("images/open.png", ""));
cvButton.setFocusPainted(false);
cvButton.setContentAreaFilled(false);
cvButton.setBorderPainted(true);
addComponents();
add(tb);
add(panel);
}
private void addComponents(){
panel.setLayout(new GridBagLayout());
int row =0;
//new row
JPanel cvButtonPanel = (JPanel)PanelHelper.addTestBorder(new JPanel(),Color.BLUE);
cvButtonPanel.add(cvButton);
cvButtonPanel.add(PanelHelper.addTestBorder(new JPanel(), Color.RED));
panel.add(tb, new GBC(1,row));
panel.add(cvButtonPanel, new GBC(1,++row));
}
}
class PanelHelper{
public static JComponent addTestBorder(JComponent comp, Color color){
Border border = BorderFactory.createLineBorder(color);
comp.setBorder(border);
return comp;
}
public ImageIcon createIcon(String path, String btnName){
URL url = getClass().getResource(path);
if(url == null)
System.err.println("\nThe "+btnName+" Icon Path Cannot Be Found: "+path);
return new ImageIcon(url);
}
}
it was just as simple as setting the button margin, as mentioned in the comments below.
button.setMargin(new Insets(0,0,0,0))

Components in JTabbedPane not showing up

I'm using a tabbed pane and can't get the tab to show the GUI that I want. I plan to have different Panel objects for each different tab so that I can setup their layouts with more versatility. Right now I don't have any listeners or functions, and am strictly trying to get the components to show up.
Edit: Code is now in the question, not a link.
Here is the code for the UI for the "General":
import javax.swing.JTabbedPane;
import javax.swing.JTable;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JComponent;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.KeyEvent;
public class GeneralGUI extends JPanel{
public GeneralGUI() {
JPanel topPanel = new JPanel();
JPanel panel1 = new JPanel();
JPanel panel2 = new JPanel();
JLabel subjectNum = new JLabel("Subject Street #:");
JLabel subjectStreet = new JLabel("Subject Street Name:");
JTable compTable = new JTable();
JTable subjectTable = new JTable();
JButton getRPT = new JButton("Get RPT file");
JButton getOrder = new JButton("Get Order/Contract");
JButton subjectDocs = new JButton("Get Subject Docs");
JButton compDocs = new JButton("Get Comp Docs");
panel1.add(subjectNum);
panel1.add(subjectStreet);
panel1.add(compTable);
panel2.add(getRPT);
panel2.add(getOrder);
panel2.add(subjectDocs);
panel2.add(compDocs);
panel2.add(subjectTable);
topPanel.add(panel1);
topPanel.add(panel2);
topPanel.setVisible(true);
}
}
Here is the code for the tabbed pane code:
import javax.swing.JTabbedPane;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JComponent;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.KeyEvent;
public class AppraisalTabs extends JPanel {
public AppraisalTabs() {
super(new GridLayout(1, 1));
JTabbedPane tabbedPane = new JTabbedPane();
GeneralGUI genGUI = new GeneralGUI();
// JComponent panel1 = makeTextPanel("General");
tabbedPane.addTab("General", genGUI);
tabbedPane.setMnemonicAt(0, KeyEvent.VK_1);
JComponent panel2 = makeTextPanel("Docs");
tabbedPane.addTab("Docs", panel2);
tabbedPane.setMnemonicAt(1, KeyEvent.VK_2);
JComponent panel3 = makeTextPanel("Subject");
tabbedPane.addTab("Subject", panel3);
tabbedPane.setMnemonicAt(2, KeyEvent.VK_3);
JComponent panel4 = makeTextPanel("Comps");
panel4.setPreferredSize(new Dimension(410, 300));
tabbedPane.addTab("Comps", panel4);
tabbedPane.setMnemonicAt(3, KeyEvent.VK_4);
JComponent panel5 = makeTextPanel("Report");
panel4.setPreferredSize(new Dimension(800, 800));
tabbedPane.addTab("Report", panel5);
tabbedPane.setMnemonicAt(4, KeyEvent.VK_5);
//Add the tabbed pane to this panel.
add(tabbedPane);
//The following line enables to use scrolling tabs.
tabbedPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
}
protected JComponent makeTextPanel(String text) {
JPanel panel = new JPanel(false);
JLabel filler = new JLabel(text);
filler.setHorizontalAlignment(JLabel.CENTER);
panel.setLayout(new GridLayout(1, 1));
panel.add(filler);
return panel;
}
private static void createAndShowGUI() {
//Create and set up the window.
JFrame frame = new JFrame("Appraisal Helper");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Add content to the window.
frame.add(new AppraisalTabs(), BorderLayout.CENTER);
//Display the window.
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
//Schedule a job for the event dispatch thread:
//creating and showing this application's GUI.
SwingUtilities.invokeLater(new Runnable() {
public void run() {
//Turn off metal's use of bold fonts
UIManager.put("swing.boldMetal", Boolean.FALSE);
createAndShowGUI();
}
});
}
}
My problem is that once I run the code the tabbed pane shows up, as well as the correctly-titled tabs, but the "General" tab isn't showing anything at all. I tried to setup the buttons and everything in it but it's still blank.
Any ideas?

Use a layout in place of a component

I'd like to create a main window frame with a BorderLayout that contains other layouts as its components.
How can I add, say, a FlowLayout to my BorderLayout's NORTH position?
Here's a little program that shows you:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.LineBorder;
public class LayoutExample {
public static void main (String[] args) {
JFrame frame = new JFrame("Frame with BorderLayout");
frame.setLayout(new BorderLayout());
JPanel flow = new JPanel();
JLabel label = new JLabel("This is a flowlayout.");
flow.setBorder(new LineBorder(Color.BLACK));
flow.setLayout(new FlowLayout());
flow.add(label);
frame.add(flow, BorderLayout.NORTH);
frame.setSize(300,300);
frame.setVisible(true);
}
}
That's how it looks like at the end:

Java JPannel not Visible

I tried creating simple GUI using JFrame with below code.
package sorting_array_gui;
package sorting_array_gui;
import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.WindowConstants;
import javax.swing.table.DefaultTableModel;
public class userwindow extends JFrame {
private static final long serialVersionUID = 1L;
public userwindow() {
super("A Programm to Sort Your Array");
setSize(1000,600);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setVisible(true);
JPanel p1= new JPanel();
JButton b1= new JButton("Click Here");
p1.add(b1);
JTextField t1= new JTextField();
p1.add(t1);
JLabel l1= new JLabel("This is a Lable");
p1.add(l1);
add(p1,BorderLayout.CENTER);
}
}
When I added JTextfield , JPlane misbehaved and even JButton and JLabel stoped showing.
Why is that happening.
"When I added JTextfield , JPlane misbehaved and even JButton and JLabel stoped showing."
I don't get this behavior with your code. But you should note the below.
setVisible(true); should be that last thing you do after adding all components.
public userwindow() {
super("A Programm to Sort Your Array");
JPanel p1= new JPanel();
JButton b1= new JButton("Click Here");
p1.add(b1);
JTextField t1= new JTextField();
p1.add(t1);
JLabel l1= new JLabel("This is a Lable");
p1.add(l1);
add(p1,BorderLayout.CENTER);
pack(); <--- PACK frame
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setVisible(true); <--- LAST
}
Also, you should set a size to your text field using the constructor that sets the column size
JTextField t1 = new JTextField(20);
Also, you should use pack() instead of setSize(). If you just pack(), everything should be visible, as the preferred sizes of all the components are respected.
Also note, if you want to add any other components to the JFrame you need to specify a BorderLayout position for each component, with no positions being used more than once.
See Laying out Components Within a Container

Categories

Resources