I am working on a program that has a flow layout, inside are a set of labels and because there are so many they do not all display. Is there anyway to add a scroll pane to scroll through all of these labels horizontally?
JPanel mainpanel = new JPanel();
mainpanel.setLayout(new BoxLayout(mainpanel, BoxLayout.X_AXIS));
pane.add(mainpanel, BorderLayout.NORTH);
JPanel rightpanel = new JPanel();
rightpanel.setLayout(new FlowLayout());
for (int i = 0; i < 100; i++)
{
rightpanel.add(new JLabel("Label " + i));
}
mainpanel.add(new JLabel("Left label"));
mainpanel.add(new JScrollPane(rightpanel));
I'd suggest ot use JList or JTable with one Column or Row (depends or direction), Object in the JList or JTable is JLabel/JComponent by default
Not sure what your question really is, since you already know you need to use a JScrollPane. How about:
public class ScrollLabels
{
public static void main(String[] args)
{
JFrame frame = new JFrame("Labels");
JPanel mainpanel = new JPanel();
mainpanel.setLayout(new BoxLayout(mainpanel, BoxLayout.X_AXIS));
frame.add(mainpanel);
JPanel rightpanel = new JPanel();
rightpanel.setLayout(new FlowLayout());
for (int i = 0; i < 100; i++)
{
rightpanel.add(new JLabel("Label " + i));
}
mainpanel.add(new JLabel("Left label"));
mainpanel.add(new JScrollPane(rightpanel));
frame.setSize(500, 100);
frame.setVisible(true);
}
}
Related
public static void main(String[] args) {
JTextField text = new JTextField();
JFrame frame = new JFrame();
frame.setLayout(new BorderLayout());
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(5, 4));
JPanel panel2 = new JPanel();
panel2.setLayout(new FlowLayout());
JLabel imgLabel1 = new JLabel(new ImageIcon("C:\\Users\\Arthur\\Downloads\\abs.jpg"));
JLabel imgLabel2 = new JLabel(new ImageIcon("C:\\Users\\Arthur\\Downloads\\abss.jpg"));
imgLabel1.setPreferredSize(new Dimension(100,100));
imgLabel2.setPreferredSize(new Dimension(100,100));
panel2.add(imgLabel1);
panel2.add(imgLabel2);
for(int i=0; i<20; i++){
panel.add(panel2);
}
frame.add(text, BorderLayout.NORTH);
frame.add(panel, BorderLayout.CENTER);
frame.setPreferredSize(new Dimension(1280,700));
frame.pack();
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
I want to make a memory game, I need to put two images in each cell of the JPanel of 4x5. For this I created a JPanel 1x2 with two images inside and put it in the JPanel of 4x5. But the result is:
Result:
So, if understand correctly, you're problem is, you're not seeing 20 new panels, only one.
The problem is, a component can only reside in a single container, once, so doing something like...
for (int i = 0; i < 20; i++) {
panel.add(panel2);
}
is the equivalent of doing something like...
panel.add(panel2);
You actually need to create a new instance of the component on each iteration of the loop
What I would suggest you do is create a "wrapper" or "card" panel which can contain the two images. In my testing I just used coloured panels, but you get the idea...
public class WrapperPane extends JPanel {
public WrapperPane() {
setLayout(new FlowLayout());
add(makePanel(Color.RED));
add(makePanel(Color.GREEN));
// This is just for demonstration purposes
setBorder(new LineBorder(Color.DARK_GRAY));
}
protected JPanel makePanel(Color background) {
JPanel panel = new JPanel();
panel.setBackground(background);
panel.setPreferredSize(new Dimension(100, 100));
return panel;
}
}
The you'd just have to do something like...
JTextField text = new JTextField();
JFrame frame = new JFrame();
frame.setLayout(new BorderLayout());
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(5, 4));
for (int i = 0; i < 20; i++) {
panel.add(new WrapperPane());
}
frame.add(text, BorderLayout.NORTH);
frame.add(panel, BorderLayout.CENTER);
// Don't do this, just let the content make it's own
// calculations
//frame.setPreferredSize(new Dimension(1280, 700));
frame.pack();
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
And you'd end up with something like...
I have following source code...Can someone please give me an advice how to add jscrollpane onto jframe? I tried several time to add it to jframe but without any progress. It is not even showing.
public class Form3 {
JFrame jframe = new JFrame("Etiket print.");
JPanel panel1 = new JPanel();
JPanel panel2 = new JPanel();
JPanel panel3 = new JPanel();
JPanel panel4 = new JPanel();
JScrollPane scrollFrame = new JScrollPane(panel2);
Color myBlue1Color = new Color(168, 175, 247);
Color myBlue2Color = new Color(139, 146, 255);
public Form3(){
jframe.setMinimumSize(new Dimension(1280, 1000));
panel2.setLayout(new BoxLayout(panel2, BoxLayout.Y_AXIS));
panel2.setAutoscrolls(true);
jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//---------------------------------Header
panel1 = createSquareJPanel(Color.YELLOW, 600,200);
panel3 = createSquareJPanel(Color.GREEN, 400,200);
panel4 = createSquareJPanel(Color.white, 280,200);
JPanel container = new JPanel();
JPanel container1 = new JPanel();
JPanel container2 = new JPanel();
container.setLayout(new BoxLayout(container, BoxLayout.Y_AXIS));
container1.setLayout(new BoxLayout(container1, BoxLayout.Y_AXIS));
container2.setLayout(new BoxLayout(container2, BoxLayout.X_AXIS));
container1.add(panel1);
container2.add(container1);
container2.add(panel3);
container2.add(panel4);
container.add(container2);
container.add(panel2);
{
for (int i=0; i<25; i++){
JPanel harnessPanel= new JPanel();
harnessPanel.setMinimumSize(new Dimension(1280, 70));
harnessPanel.setMaximumSize(new Dimension(1280, 70));
harnessPanel.setPreferredSize(new Dimension(1280, 70));
if(i%2==0) {
harnessPanel.setBackground(myBlue1Color);
}
else {
harnessPanel.setBackground(myBlue2Color);
}
panel2.add(harnessPanel);
harnessPanel.repaint();
harnessPanel.validate();
}
panel2.repaint();
panel2.validate();
}
jframe.add(scrollFrame);
jframe.add(container);
jframe.pack();
jframe.setLocationRelativeTo(null);
jframe.setVisible(true);
}
private JPanel createSquareJPanel(Color color, int size1, int size2)
{
JPanel tempPanel = new JPanel();
tempPanel.setBackground(color);
tempPanel.setMinimumSize(new Dimension(size1, size2));
tempPanel.setMaximumSize(new Dimension(size1, size2));
tempPanel.setPreferredSize(new Dimension(size1, size2));
return tempPanel;
}
public static void main (String args[]){
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
Form3 myF=new Form3();
}
});
};
}
picture of my app:
actual state:
JFrame uses a BorderLayout by default. It's default position (if you don't specify one) is CENTER.
BorderLayout will only allow one component to occupy any of it's 5 available positions.
So when you do...
jframe.add(scrollFrame);
jframe.add(container);
It adds the scrollFrame to the center position and effectively removes it when you add container (it doesn't actually remove it, but the result is just about the same).
Try supplying a position constraint. For example...
jframe.add(scrollFrame);
jframe.add(container, BorderLayout.SOUTH);
See How to use BorderLayout for more details
I have following source code...Can someone please give me an advice how to add jscrollpane onto jframe? I tried several time to add it to jframe but without any progress. It is not even showing.
public class Form3 {
JFrame jframe = new JFrame("Etiket print.");
JPanel panel1 = new JPanel();
JPanel panel2 = new JPanel();
JPanel panel3 = new JPanel();
JPanel panel4 = new JPanel();
JScrollPane scrollFrame = new JScrollPane(panel2);
Color myBlue1Color = new Color(168, 175, 247);
Color myBlue2Color = new Color(139, 146, 255);
public Form3(){
jframe.setMinimumSize(new Dimension(1280, 1000));
panel2.setLayout(new BoxLayout(panel2, BoxLayout.Y_AXIS));
panel2.setAutoscrolls(true);
jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//---------------------------------Header
panel1 = createSquareJPanel(Color.YELLOW, 600,200);
panel3 = createSquareJPanel(Color.GREEN, 400,200);
panel4 = createSquareJPanel(Color.white, 280,200);
JPanel container = new JPanel();
JPanel container1 = new JPanel();
JPanel container2 = new JPanel();
container.setLayout(new BoxLayout(container, BoxLayout.Y_AXIS));
container1.setLayout(new BoxLayout(container1, BoxLayout.Y_AXIS));
container2.setLayout(new BoxLayout(container2, BoxLayout.X_AXIS));
container1.add(panel1);
container2.add(container1);
container2.add(panel3);
container2.add(panel4);
container.add(container2);
container.add(panel2);
{
for (int i=0; i<25; i++){
JPanel harnessPanel= new JPanel();
harnessPanel.setMinimumSize(new Dimension(1280, 70));
harnessPanel.setMaximumSize(new Dimension(1280, 70));
harnessPanel.setPreferredSize(new Dimension(1280, 70));
if(i%2==0) {
harnessPanel.setBackground(myBlue1Color);
}
else {
harnessPanel.setBackground(myBlue2Color);
}
panel2.add(harnessPanel);
harnessPanel.repaint();
harnessPanel.validate();
}
panel2.repaint();
panel2.validate();
}
jframe.add(scrollFrame);
jframe.add(container);
jframe.pack();
jframe.setLocationRelativeTo(null);
jframe.setVisible(true);
}
private JPanel createSquareJPanel(Color color, int size1, int size2)
{
JPanel tempPanel = new JPanel();
tempPanel.setBackground(color);
tempPanel.setMinimumSize(new Dimension(size1, size2));
tempPanel.setMaximumSize(new Dimension(size1, size2));
tempPanel.setPreferredSize(new Dimension(size1, size2));
return tempPanel;
}
public static void main (String args[]){
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
Form3 myF=new Form3();
}
});
};
}
picture of my app:
actual state:
JFrame uses a BorderLayout by default. It's default position (if you don't specify one) is CENTER.
BorderLayout will only allow one component to occupy any of it's 5 available positions.
So when you do...
jframe.add(scrollFrame);
jframe.add(container);
It adds the scrollFrame to the center position and effectively removes it when you add container (it doesn't actually remove it, but the result is just about the same).
Try supplying a position constraint. For example...
jframe.add(scrollFrame);
jframe.add(container, BorderLayout.SOUTH);
See How to use BorderLayout for more details
I have a JPanel and inside I use a GridLayout like this:
JPanel panel = new JPanel(new GridLayout(0, 1, 0, 0));
JPanel p1 = new JPanel(new FlowLayout());
JLabel label = new JLabel("SOMETHING");
JTextField tf = new JTextField(30);
JPanel p2 = new JPanel();
JTextArea txt = new JTextArea(6, 30);
JScrollPane sp = new JScrollPane(txt);
p1.add(label);
p1.add(tf);
p2.add(sp);
panel.add(p1);
panel.add(p2);
Unfortunately, the space between the JTextArea and the upper elements if very big.
What can I do to bring the JTextArea up?
http://img20.imageshack.us/img20/1086/screenshot1412201213550.png
Use BorderLayout and add the top panel to NORTH and the scroll pane to the CENTER.
Screenshot of the code below:
public static void main(String[] args) {
JFrame frame = new JFrame("Test");
frame.add(new JPanel(new FlowLayout()) {{
add(new JLabel("something"));
add(new JTextField(30));
}}, BorderLayout.NORTH);
frame.add(new JScrollPane(new JTextArea(6, 30)), BorderLayout.CENTER);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
I put my buttons in a JPane with GridLayout. Then I put JPanel into another JPanel with BoxLayout.Y_AXIS. I want buttons in the GridLayout to be square. I use tmp.setSize(30,30) and it does not work. I also try to use new GridLayout(X, Y, 4, 4) but I cannot figure out what X and Y are. So, what is the correct way to do this stuff?
ADDED:
I still cannot solve the problem. Here is the code of what I am trying to do:
import javax.swing.*;
import java.awt.*;
public class PanelModel {
public static void main(String[] args) {
JFrame frame = new JFrame("Colored Trails");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel mainPanel = new JPanel();
mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
JPanel firstPanel = new JPanel();
firstPanel.setLayout(new GridLayout(4, 4));
JButton btn;
for (int i=1; i<=4; i++) {
for (int j=1; j<=4; j++) {
btn = new JButton();
btn.setPreferredSize(new Dimension(100, 100));
firstPanel.add(btn);
}
}
JPanel secondPanel = new JPanel();
secondPanel.setLayout(new GridLayout(5, 13));
for (int i=1; i<=5; i++) {
for (int j=1; j<=13; j++) {
btn = new JButton();
btn.setPreferredSize(new Dimension(40, 40));
secondPanel.add(btn);
}
}
mainPanel.add(firstPanel);
mainPanel.add(secondPanel);
frame.add(mainPanel);
frame.setSize(400,600);
frame.setVisible(true);
}
}
The problem is that Java tries to make width of the firstPanel and secondPanel equal! Moreover, Java tries to to fill all height of the window. How can I remove this behavior?
The following bit of code does what you ask for. Just make sure that you assign enough space so that the text on the button becomes visible
JFrame frame = new JFrame("test");
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
JPanel panel = new JPanel(new GridLayout(4,4,4,4));
for(int i=0 ; i<16 ; i++){
JButton btn = new JButton(String.valueOf(i));
btn.setPreferredSize(new Dimension(40, 40));
panel.add(btn);
}
frame.setContentPane(panel);
frame.pack();
frame.setVisible(true);
The X and Y (two first parameters of the GridLayout constructor) specify the number of rows and columns in the grid (respectively). You may leave one of them as 0 if you want that value to be unbounded.
Edit
I've modified the provided code and I believe it now conforms to what is desired:
JFrame frame = new JFrame("Colored Trails");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel mainPanel = new JPanel();
mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
JPanel firstPanel = new JPanel();
firstPanel.setLayout(new GridLayout(4, 4));
firstPanel.setMaximumSize(new Dimension(400, 400));
JButton btn;
for (int i=1; i<=4; i++) {
for (int j=1; j<=4; j++) {
btn = new JButton();
btn.setPreferredSize(new Dimension(100, 100));
firstPanel.add(btn);
}
}
JPanel secondPanel = new JPanel();
secondPanel.setLayout(new GridLayout(5, 13));
secondPanel.setMaximumSize(new Dimension(520, 200));
for (int i=1; i<=5; i++) {
for (int j=1; j<=13; j++) {
btn = new JButton();
btn.setPreferredSize(new Dimension(40, 40));
secondPanel.add(btn);
}
}
mainPanel.add(firstPanel);
mainPanel.add(secondPanel);
frame.setContentPane(mainPanel);
frame.setSize(520,600);
frame.setMinimumSize(new Dimension(520,600));
frame.setVisible(true);
Basically I now set the preferred size of the panels and a minimum size for the frame.
Try with setPreferredSize instead of setSize.
UPDATE: GridLayout take up all space in its container, and BoxLayout seams to take up all the width in its container, so I added some glue-panels that are invisible and just take up space when the user stretches the window. I have just done this horizontally, and not vertically, but you could implement that in the same way if you want it.
Since GridLayout make all cells in the same size, it doesn't matter if they have a specified size. You have to specify a size for its container instead, as I have done.
import javax.swing.*;
import java.awt.*;
public class PanelModel {
public static void main(String[] args) {
JFrame frame = new JFrame("Colored Trails");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel mainPanel = new JPanel();
mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
JPanel firstPanel = new JPanel(new GridLayout(4, 4));
firstPanel.setPreferredSize(new Dimension(4*100, 4*100));
for (int i=1; i<=4; i++) {
for (int j=1; j<=4; j++) {
firstPanel.add(new JButton());
}
}
JPanel firstGluePanel = new JPanel(new BorderLayout());
firstGluePanel.add(firstPanel, BorderLayout.WEST);
firstGluePanel.add(Box.createHorizontalGlue(), BorderLayout.CENTER);
firstGluePanel.add(Box.createVerticalGlue(), BorderLayout.SOUTH);
JPanel secondPanel = new JPanel(new GridLayout(13, 5));
secondPanel.setPreferredSize(new Dimension(5*40, 13*40));
for (int i=1; i<=5; i++) {
for (int j=1; j<=13; j++) {
secondPanel.add(new JButton());
}
}
JPanel secondGluePanel = new JPanel(new BorderLayout());
secondGluePanel.add(secondPanel, BorderLayout.WEST);
secondGluePanel.add(Box.createHorizontalGlue(), BorderLayout.CENTER);
secondGluePanel.add(Box.createVerticalGlue(), BorderLayout.SOUTH);
mainPanel.add(firstGluePanel);
mainPanel.add(secondGluePanel);
frame.getContentPane().add(mainPanel);
//frame.setSize(400,600);
frame.pack();
frame.setVisible(true);
}
}
GridLayout is often not the best choice for buttons, although it might be for your application. A good reference is the tutorial on using Layout Managers. If you look at the GridLayout example, you'll see the buttons look a little silly -- way too big.
A better idea might be to use a FlowLayout for your buttons, or if you know exactly what you want, perhaps a GroupLayout. (Sun/Oracle recommend that GroupLayout or GridBag layout are better than GridLayout when hand-coding.)
This is how I did it.
JFrame.setDefaultLookAndFeelDecorated(true);
JDialog.setDefaultLookAndFeelDecorated(true);
JFrame frame = new JFrame("SAP Multiple Entries");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel(new GridLayout(10,10,10,10));
frame.setLayout(new FlowLayout());
frame.setSize(512, 512);
JButton button = new JButton("Select File");
button.setPreferredSize(new Dimension(256, 256));
panel.add(button);
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
JFileChooser fileChooser = new JFileChooser();
int returnValue = fileChooser.showOpenDialog(null);
if (returnValue == JFileChooser.APPROVE_OPTION) {
File selectedFile = fileChooser.getSelectedFile();
keep = selectedFile.getAbsolutePath();
// System.out.println(keep);
//out.println(file.flag);
if(file.flag==true) {
JOptionPane.showMessageDialog(null, "It is done! \nLocation: " + file.path , "Success Message", JOptionPane.INFORMATION_MESSAGE);
}
else{
JOptionPane.showMessageDialog(null, "failure", "not okay", JOptionPane.INFORMATION_MESSAGE);
}
}
}
});
frame.add(button);
frame.pack();
frame.setVisible(true);