This question already has answers here:
Why does the first panel added to a frame disappear?
(2 answers)
Closed 6 years ago.
I am building a simple program. I should represent data in JTable, but the table is not showing up in JFrame and I can't figure out why.
MAIN METHOD
setLayout(new BorderLayout());
panel.setLayout(new FlowLayout(FlowLayout.LEFT));
Stredisko prvni = new Stredisko("Krkonoše", 1, "Aldrov");
strediska.pridejStredisko(prvni);
panel.add(b_pridej);
panel.add(b_smaz);
panel.add(b_konec);
add(new JScrollPane(tbl),BorderLayout.CENTER);
setDefaultCloseOperation(EXIT_ON_CLOSE);
tbl.setModel(model);
add(panel, BorderLayout.CENTER);
setVisible(true);
pack();
model.fireTableDataChanged();
Adding two components at BorderLayout.CENTER will only display the last added one.
Consider adding your other panel elsewhere, say BorderLayout.NORTH for instance.
Related
This question already has answers here:
error upon assigning Layout: BoxLayout can't be shared
(4 answers)
Closed 4 years ago.
Trying to have 2 panels in a frame, one small one at the top and one filling up the rest of the frame. This code returns a "BoxLayout can't be shared" error though.
JFrame frame = new JFrame("Clients");
frame.setSize(1000,900);
JPanel sorters = new JPanel();
sorters.setSize(1000, 100);
frame.getContentPane().add(sorters);
JPanel rowPane = new JPanel();
JScrollPane scrPane = new JScrollPane(rowPane, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
scrPane.setSize(1000, 800);
frame.getContentPane().add(scrPane);
frame.getContentPane().setLayout(new BoxLayout(frame, BoxLayout.Y_AXIS));
A BoxLayout must have as first parameter the actual container it is applied to, in your case the container is the contentPane of the frame :
frame.getContentPane().setLayout(new BoxLayout(frame.getContentPane(), BoxLayout.Y_AXIS));
This question already has answers here:
Java: vertical alignment within JPanel
(3 answers)
Closed 4 years ago.
the task i am trying to do is simple. I want to add JButtons to a panel in a vertical way, but using a loop to adding it, i tried to do it using .setBounds() and .setLocation() mehtod, but i dont have any results.
In a simple way, i want to do this but adding the buttons vertically and keeping the JScroll bar...:
public class NewMain {
public static void main(String[] args) {
JFrame frame = new JFrame();
JPanel panel = new JPanel();
frame.setLayout(null);
for (int i = 0; i < 10; i++) {
JButton asd=new JButton("HOLA "+i);
asd.setLocation(i+20, i+20);
panel.add(asd);
}
JScrollPane scrollPane = new JScrollPane(panel);
scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
scrollPane.setBounds(50, 30, 300, 50);
JPanel contentPane = new JPanel(null);
contentPane.setPreferredSize(new Dimension(500, 400));
contentPane.add(scrollPane);
frame.setContentPane(contentPane);
frame.pack();
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setVisible(true);
}
Give the JPanel that holds the JButtons an appropriate layout manager that adds components in a vertical manner. A GridLayout(0, 1) would work, the parameters referring to 0 rows -- meaning variable number of rows, and 1 column. This will add the JButtons into a vertical grid, column of one
Other possible solutions include BoxLayout and GridBagLayout, both of which are a little more complex than the GridLayout.
Also avoid using null layouts as you're doing as this leads to inflexible GUI's painful debugging and changes.
This question already has answers here:
Java: Can't apply Gridlayout to a Jscrollpane. Get Get java.lang.ClassCastException
(1 answer)
ScrollPane adding to grid layout
(3 answers)
Closed 5 years ago.
I have a GridLayout (of JTextFields : 57*57) in a JPanel in a JFrame (resolution 800*800) and the result is really small and that's why I would like to have a JScrollPane (that would have a 800*800 resolution and the GridLayout resolution would be 3000*3000 for example).
I've tried many things but it was not working. Can someone please help me ?
Here is the concerned part of the code :
Thanks.
GridLayout grid = new GridLayout(thumbnail.getHeight(), thumbnail.getWidth(),0,0);
final_frame = new JFrame();
final_frame.setPreferredSize(new Dimension(800, 800));
JPanel myPanel = new JPanel();
myPanel.setLayout(grid);
//fill grid with colors and denomination
for (int yy=0;yy<thumbnail.getHeight();yy++){
for (int xx=0;xx<thumbnail.getWidth();xx++){
for (int kk=0;kk<colors.length;kk++){
if (thumbnail.getRGB(xx,yy)==colors[kk].getRGB()){
//System.out.println("Color : "+colors[kk]+" libel : H"+libel[kk]);
JTextField tf = new JTextField("H"+libel[kk]);
tf.setBackground(colors[kk]);
tf.setSize(10, 10); //do nothing ?
myPanel.add(tf);
}
}
}
}
final_frame.add(myPanel);
final_frame.pack();
final_frame.setVisible(true);
final_frame.setLocationRelativeTo(null);
final_frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
This question already has answers here:
How to add an image to a JPanel?
(14 answers)
Closed 6 years ago.
I need to show images and images numbers are unknown - maybe 5 maybe 10 maybe more and I have no idea how I should display them.
This is my code and display only one image.
public class NewClass extends JFrame {
public static void main(String []args) throws IOException{
BufferedImage img=ImageIO.read(new File("D:\\A-programmer-Life.jpg"));
ImageIcon icon=new ImageIcon(img);
JFrame frame=new JFrame();
frame.setLayout(new FlowLayout());
frame.setSize(500,500);
JLabel lbl=new JLabel();
lbl.setIcon(icon);
frame.add(lbl);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
Display the images in a JList. The list will allow you to control the number of rows of images and then you add the list to a JScrollPane so scrollbars can appear if necessary.
You can add an Icon to the list and it will render as an image.
Check out the section from the Swing tutorial on How to Use Lists for more information.
The other option is to use a panel with a GridLayout. The labels will wrap to a new row when the first row is full. Again you would add this panel to a JScrollPane. The tutorial also has an example of using a GridLayout.
This question already has answers here:
How to add JTable in JPanel with null layout?
(11 answers)
Closed 6 years ago.
Can any one help me?
Hello,How can i add two panels in one frame?
public class test{
public static void main(String[] args){
JFrame frame = new JFrame();
frame.setSize(400, 400);
frame.setLayout(null);
JPanel panel = new JPanel();
panel.setLayout(null);
panel.setBounds(5, 5, 300, 300);
JPanel panel2 = new JPanel();
panel2.setLayout(null);
panel2.setBounds(1,200,300,300);
JLabel label2 = new JLabel("asddas");
label2.setBounds(30,30,20,20);
panel2.add(label2);
JLabel label[] = new JLabel[10];
int count = 1;
for(int i = 0; i < 10; i++){
label[i] = new JLabel("ds");
label[i].setBounds(1,count,20,20);
count +=20;
panel.add(label[i]);
}
frame.add(panel,panel2);
frame.setVisible(true);
}
}
You can think of the JPanel as one big panel that contains the all of the other elements. So you can have a main JPanel and then put others inside it. You should set a layout that fits your needs to the main panel. A good introduction to layouts can be found here http://docs.oracle.com/javase/tutorial/uiswing/layout/index.html
Also see this answer
How to layout multiple panels on a jFrame? (java)