Dynamically add fixed-height panels to a JScrollPane - java

JPanel panel = new JPanel(new GridLayout(0,1));
JScrollPane contentpane = new JScrollPane(panel);
JButton add = new JButton("ADD");
add.actionListener(new ActionListener() {
public void actionPerformed(){
MyPanel newpanel = new MyPanel("title","Button"); //MyPanel is a class which extends JPanel and contains constructor MyPanel(String TitleToSet ,String ButtonTitleTOAdd)
panel.add(newpanel);
panel.repaint();
}) ;
I have used this code thinking that it will add the MyPanel to the grid layout dynamically and "panel" would be scrollable if more "MyPanel"s are added. However, this was not the case, 1st "MyPanel" filled whole "panel" and on adding second "MyPanel" (by clicking button "Add"), the 1st "MyPanel" was shrinked to make space for second one to be added.. and on adding more, all the "MyPanel"s were fit in the viewport instead of making the "panel" scrollable.. How to add those "MyPanel"s dynamically and making the "panel" scrollable on adding more of those?? Any help would be appreciated.

http://docs.oracle.com/javase/7/docs/api/javax/swing/JScrollPane.html :
By default JScrollPane uses ScrollPaneLayout to handle the layout of
its child Components. ScrollPaneLayout determines the size to make the
viewport view in one of two ways:
[...]
getPreferredSize is used.
You should add the line
panel.setPreferredSize(new Dimension(0, panel.getComponents().size() * SUB_PANEL_HEIGHT));
to your ActionListener.
Full example:
package main;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.Timer;
class Test {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setPreferredSize(new Dimension(400, 400));
frame.setSize(400, 400);
JPanel panel = new JPanel(new GridLayout(0, 1));
panel.add(new JLabel("BOO"));
panel.add(new JButton("BBBB"));
JScrollPane contentpane = new JScrollPane(panel);
frame.add(contentpane);
new Timer(1000, new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
JPanel newpanel = new JPanel();
newpanel.add(new JLabel("LOL"));
panel.add(newpanel);
System.out.println(100 * panel.getComponents().length);
panel.setPreferredSize(new Dimension(0, 100 * panel.getComponents().length));
contentpane.validate();
}
}).start();
frame.setVisible(true);
}
}

Related

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?

JPanel's border make JLabel disappear

There is a frame. There a panel in that frame with BoxLayout. In that panel there is a ScrollPane. There is another panel in ScrollPane with SpringLayout. There is a label in that inner panel.
Here is a code:
package test;
import java.awt.Color;
import java.awt.Dimension;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.SpringLayout;
import javax.swing.SwingUtilities;
class MainPanel extends JPanel {
MainPanel() {
JPanel panel = new JPanel();
BoxLayout boxLayout = new BoxLayout(panel, BoxLayout.Y_AXIS);
panel.setLayout(boxLayout);
panel.setBorder(BorderFactory.createLineBorder(Color.black));
JPanel innerPanel = new JPanel();
SpringLayout springLayout = new SpringLayout();
innerPanel.setLayout(springLayout);
JLabel label = new JLabel("test");
innerPanel.add(label);
springLayout.putConstraint(SpringLayout.WEST, label, 5, SpringLayout.WEST, innerPanel);
springLayout.putConstraint(SpringLayout.NORTH, label, 5, SpringLayout.NORTH, innerPanel);
//innerPanel.setBorder(BorderFactory.createLineBorder(Color.black));
innerPanel.setPreferredSize(new Dimension(300, 100));
panel.add(innerPanel);
JScrollPane scrollPane = new JScrollPane(panel);
this.add(scrollPane);
}
}
class MainFrame extends JFrame {
MainFrame() {
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.add(new MainPanel());
this.pack();
this.setVisible(true);
}
}
public class Test {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
new MainFrame();
}
});
}
}
Label "test" is visible as expected. But when I add a border to inner panel (uncomment the line, see below) the label is disappeare.
//innerPanel.setBorder(BorderFactory.createLineBorder(Color.black));
Can someone explane why?
Try to change
innerPanel.setBorder(BorderFactory.createLineBorder(Color.black));
to
panel.setBorder(BorderFactory.createLineBorder(Color.black));
I hope that's what you meant to do & it fixed your problem :)

JTextArea Won't Update Unless I Resize JFrame

Whenever I run my program my JTextArea does not follow the dimension that I have given it, but if I resize my JFrame it updates and sets its size to what I put.
What is the issue?
public ControlPanel() {
// create our list of players
list = new JList(model);
// create our scroll panes
userspane = new JScrollPane(list);
consolepane = new JScrollPane(console);
// set sizes
userspane.setSize(100, 500);
jta.setSize(100, 500);
list.setSize(100, 500);
consolepane.setSize(100, 500);
console.setSize(100, 500);
// add to panel
panel.add(userspane, BorderLayout.CENTER);
panel.add(kick);
panel.add(ban);
panel.add(info);
panel.add(consolepane, BorderLayout.CENTER);
// set frame properties
setTitle("RuneShadows CP");
setSize(280, 400);
//setResizable(false);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setContentPane(panel);
setVisible(true);
}
Don't set the sizes to anything.
For JTextArea you can use the constructor JTextArea(int rows, int charSpaces)
Just pack() the JFrame and it will respect all the preferred sizes of the components inside.
Also instead of setting the content pane to the panel, just add the panel. That will respect the preffered size of the panel when pack() is called
I'm not exactly sure what variable was what (or the sizes you wanted the), so I assumed text areas, and others as well. See this example where I just used the JTextArea constructor I mentioned and just packed.
EDITED with no sizes set
import java.awt.BorderLayout;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import static javax.swing.JFrame.EXIT_ON_CLOSE;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;
public class ControlPanel extends JFrame {
JScrollPane userspane;
JList list;
DefaultListModel model = new DefaultListModel();
JScrollPane consolepane;
JTextArea console = new JTextArea(20, 50);
JTextArea jta = new JTextArea(6, 50);
JPanel panel = new JPanel();
JButton kick = new JButton("Kick");
JButton ban = new JButton("Ban");
JButton info = new JButton("Info");
public ControlPanel() {
// create our list of players
list = new JList(model);
// create our scroll panes
userspane = new JScrollPane(list);
consolepane = new JScrollPane(console);
// add to panel
panel.add(userspane, BorderLayout.CENTER);
panel.add(kick);
panel.add(ban);
panel.add(info);
panel.add(consolepane, BorderLayout.CENTER);
add(panel);
pack();
setTitle("RuneShadows CP");
//setResizable(false);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable(){
public void run(){
new ControlPanel();
}
});
}
}
UPDATE - with positioning
Keep in mind also, with BorderLayout you need to specify a position for every component you add or else it will default to CENTER and each position an only have one component. I noticed you trying to add two components to the CENTER
import java.awt.BorderLayout;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import static javax.swing.JFrame.EXIT_ON_CLOSE;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;
public class ControlPanel extends JFrame {
JScrollPane userspane;
JList list;
DefaultListModel model = new DefaultListModel();
JScrollPane consolepane;
JTextArea console = new JTextArea(20, 50);
JTextArea jta = new JTextArea(6, 50);
JPanel panel = new JPanel(new BorderLayout());
JButton kick = new JButton("Kick");
JButton ban = new JButton("Ban");
JButton info = new JButton("Info");
public ControlPanel() {
// create our list of players
list = new JList(model);
// create our scroll panes
userspane = new JScrollPane(list);
consolepane = new JScrollPane(console);
// add to panel
panel.add(userspane, BorderLayout.SOUTH);
JPanel buttonPanel = new JPanel();
buttonPanel.add(kick);
buttonPanel.add(ban);
buttonPanel.add(info);
panel.add(buttonPanel, BorderLayout.CENTER);
panel.add(consolepane, BorderLayout.NORTH);
add(panel);
pack();
setTitle("RuneShadows CP");
//setResizable(false);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable(){
public void run(){
new ControlPanel();
}
});
}
}

Top layer in JLayeredPane not displaying

I'm trying to create a panel that uses a JLayeredPane to have a panel centered above another, larger panel. I can't seem to get the smaller panel to display though. Any ideas as to what I'm doing wrong?
import java.awt.Dimension;
import javax.swing.BoxLayout;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLayeredPane;
import javax.swing.JPanel;
public class MainPanel extends JPanel {
private JLayeredPane pane;
private AllPlayersPanel players; //Larger panel, uses circleLayout
private GamePanel game; //Smaller panel, simple BorderLayout
public MainPanel(){
super();
setSize(900, 900);
setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
pane = new JLayeredPane();
pane.setPreferredSize(new Dimension(900, 900));
players = new AllPlayersPanel();
players.setPreferredSize(players.getPreferredSize());
players.setLocation(0,0);
//players.setOpaque(false);
pane.add(players, new Integer(0));
game = new GamePanel();
game.setPreferredSize(game.getPreferredSize());
game.setLocation(385, 405);
//game.setOpaque(false);
pane.add(game, new Integer(2));
add(pane);
}
public static void main(String[] args) {
JFrame frame = new JFrame();
JComponent newPane = new MainPanel();
newPane.setOpaque(true);
frame.setContentPane(newPane);
frame.pack();
frame.setVisible(true);
}
}
I've tried every combination of .setOpaque() I can think of as well.
CircleLayout can be found here
Basically, you're using setSize when you shouldn't be and preferredSize when you don't need to...
For example.
In you constructor of the MainPanel you call setSize(900, 900); when you should have overridden getPreferredSize then on the panels you are adding to the JLayeredPane you're calling setPreferredSize but the JLayeredPane has no layout manager with which to check this value, so the size of these panels remain as 0x0.
Besides, I'm not sure what you expected to gain from calling players.setPreferredSize(players.getPreferredSize());, which basically will set the preferred size to 0x0 any way :P
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import javax.swing.BoxLayout;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLayeredPane;
import javax.swing.JPanel;
import javax.swing.border.LineBorder;
public class MainPanel extends JPanel {
private JLayeredPane pane;
private JPanel players; //Larger panel, uses circleLayout
private JPanel game; //Smaller panel, simple BorderLayout
public MainPanel() {
super();
// setSize(900, 900);
// setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
setLayout(new BorderLayout());
pane = new JLayeredPane();
// pane.setPreferredSize(new Dimension(900, 900));
pane.setBorder(new LineBorder(Color.RED));
players = new JPanel();
players.setBackground(Color.RED);
players.setSize(getPreferredSize());
players.setLocation(0, 0);
//players.setOpaque(false);
pane.add(players, new Integer(0));
game = new JPanel();
game.setSize(game.getPreferredSize());
game.setBackground(Color.BLUE);
game.setLocation(385, 405);
//game.setOpaque(false);
pane.add(game, new Integer(2));
add(pane);
}
#Override
public Dimension getPreferredSize() {
return new Dimension(900, 900);
}
public static void main(String[] args) {
JFrame frame = new JFrame();
JComponent newPane = new MainPanel();
newPane.setOpaque(true);
frame.setContentPane(newPane);
frame.pack();
frame.setVisible(true);
}
}

How do I center an object in java

I want to make a simple program that will have one button and multiple fields. When I was planning this out in my head I wanted to use a gridlayout, or at least cent the button at first since I am learning. Here is what I have so far, my question that I am leading to is where do I put in my grid layout, or do I set the alignment center in the panel, frame or button?
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Normal {
public static void main(String[] args) {
JFrame frame = new JFrame("test");
JButton button = new JButton("why");
JPanel panel = new JPanel();
JTextField field= new JTextField();
//button
button.setSize(50, 50);
//Field
field.setSize(250, 25);
//Frame
frame.setSize(500, 500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
frame.add(panel);
frame.add(field);
frame.add(button);
}
}
Always add the components in a Container of the JFrame. Set the Layout of Container as GridLayout. For example You can change your code as follows:
import java.awt.GridLayout;
import java.awt.Container;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Normal {
public static void main(String[] args) {
JFrame frame = new JFrame("test");
JButton button = new JButton("why");
JPanel panel = new JPanel();
JTextField field= new JTextField();
Container c = frame.getContentPane();
c.setLayout(new GridLayout(3,1));//Devides the container in 3 rows and 1 column
c.add(panel);//Add in first row
c.add(button);//Add in second row
c.add(field);//Add in third row
frame.setSize(500, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
In an approach where you extend your class from JFrame, you could simply set where you would like to add the components. If you have a panel with ex. a button, you could add it like this:
add(panel, BorderLayout.CENTER);
Hope this helps.

Categories

Resources