I have a class that extends JPanel, where I have overridden the paintComponent(Graphics g) method. However, I can't see the rectangles or the scroll bars I have drawn.
In the main function I have the following code below:
MyClass mainPanel = new MyClass();
mainPanel.setPreferredSize(new Dimension(1000, 1000));
mainPanel.setLayout(new BorderLayout());
JPanel scrollPanel = new JPanel();
scrollPanel.setSize(new Dimension(2000, 2000));
JScrollPane scrollPane = new JScrollPane(scrollPanel, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); //Let all scrollPanel has scroll bars
scrollPane.setViewportView(scrollPanel);
scrollPane.setOpaque(true);
scrollPanel.revalidate();
mainPanel.add(scrollPane);
JFrame frame = new JFrame("Scrollable Panel");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(mainPanel);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
I have solved this issue. MyClass extends JPanel. And I have overwritten paintComponent(Graphics g) method in which I draw rectangles through Graphics.
MyClass mainPanel = new MyClass()
mainPanel.setPreferredSize(new Dimension(7000, 1000));
mainPanel.setLayout(new BorderLayout());
JScrollPane scrollPane = new JScrollPane(mainPanel, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); //Let all scrollPanel has scroll bars
scrollPane.setPreferredSize(new Dimension(1000, 900));
JFrame frame = new JFrame("Scrollable JPanel");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(scrollPane);
frame.setSize(1000, 900);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
Related
I'm trying to resize a JPanel to 100% width within a JFrame when window is resized. But the JPanel never gets resized...
I'm using IntelliJ
Here is my code:
public static void createAndShowGUI() {
frame = new JFrame("MyApplication");
frame.setLayout(new GridBagLayout());
menuPanel = new MenuPanel();
contentPanel = new ContentPanel();
MainFrame mainFrame = new MainFrame();
vimInitialiser = new VimInitialiser();
frame.addWindowListener(mainFrame);
frame.addComponentListener(mainFrame);
JComponent newContentPane = mainFrame;
newContentPane.setOpaque(true);
frame.setContentPane(newContentPane);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setMinimumSize(new Dimension(1200, 500));
frame.pack();
frame.setVisible(true);
frame.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
frame.add(menuPanel.getMenuPanel());
frame.add(contentPanel.getContentPanel());
}
Here is my JPanel classes:
public class ContentPanel extends JPanel{
public JPanel getContentPanel(){
JPanel contentPanel = new JPanel(true);
JLabel label = new JLabel("ContentPanel");
Border border = BorderFactory.createLineBorder(Color.black);
contentPanel.setPreferredSize(new Dimension(940, 480));
contentPanel.setBorder(border);
contentPanel.add(label);
return contentPanel;
}
}
public class MenuPanel extends JPanel{
public JPanel getMenuPanel(){
JPanel menuPanel = new JPanel(true);
JLabel label = new JLabel("MenuPanel");
Border border = BorderFactory.createLineBorder(Color.black);
menuPanel.setPreferredSize(new Dimension(200, 480));
menuPanel.setBorder(border);
menuPanel.add(label);
return menuPanel;
}
}
Pleas help me whit this problem.
Try using a BorderLayout.
frame.setLayout(new BorderLayout());
And then add the panes like this:
frame.add(menuPanel.getMenuPanel(), BorderLayout.NORTH);
frame.add(contentPanel.getContentPanel(), BorderLayout.CENTER);
I believe the mistake is that you are not setting the 'fill' and 'weight' for the gridbagconstraints. I think this will solve the problem.
GridBagConstraints gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
gridBagConstraints.weightx = 1.0;
gridBagConstraints.weighty = 1.0;
frame.add(menuPanel, gridBagConstraints);
frame.add(contentPanel, gridBagConstraints);
You might need to play around a little with the constraints to get it to line up exactly how you want.
I have three radio buttons with background colors as shown below.
I need to stretch all of them to same size so that the background colors are uniform(with same width).Tried adding setWidth(Dimension d) but it's not working.
public class TrafficLights {
JFrame frame;
JRadioButton stop,go,wait;
JTextField signal;
ButtonGroup grp;
Dimension dim = new Dimension(200,30);
public TrafficLights(){
frame = new JFrame("Traffic Lights");
frame.setLayout(new BoxLayout(frame.getContentPane(),BoxLayout.Y_AXIS));
stop = new JRadioButton("Red");
stop.setBackground(Color.RED);
stop.setSize(dim);
wait = new JRadioButton("Orange");
wait.setBackground(Color.ORANGE);
wait.setSize(dim);
go = new JRadioButton("Green");
go.setBackground(Color.GREEN);
go.setSize(dim);
grp = new ButtonGroup();
grp.add(stop);grp.add(wait);grp.add(go);
frame.getContentPane().add(stop);
frame.getContentPane().add(wait);
frame.getContentPane().add(go);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
frame.setMinimumSize(new Dimension(300,200));
frame.pack();
frame.setLocationRelativeTo(null);
}
Use a JPanel with a GridLayout, then add the buttons to the panel and the the panel to the frame:
JPanel panel = new JPanel( new GridLayout(0, 1) );
panel.add(button1);
...
frame.add(panel, BorderLayout.PAGE_START);
You can use a GridLayout(int rows, int cols):
frame = new JFrame("Traffic Lights");
frame.setLayout(new BoxLayout(frame.getContentPane(), BoxLayout.Y_AXIS));
JPanel panel = new JPanel( new GridLayout(3, 1) );
frame.add(panel);
stop = new JRadioButton("Red");
stop.setBackground(Color.RED);
stop.setSize(dim);
wait = new JRadioButton("Orange");
wait.setBackground(Color.ORANGE);
wait.setSize(dim);
go = new JRadioButton("Green");
go.setBackground(Color.GREEN);
go.setSize(dim);
grp = new ButtonGroup();
grp.add(stop);
grp.add(wait);
grp.add(go);
panel.add(stop);
panel.add(wait);
panel.add(go);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
frame.setMinimumSize(new Dimension(300, 200));
frame.pack();
frame.setLocationRelativeTo(null);
For more infos see: GridLayout
I have tried to use a Jlist but I have not been able to get it to show up in my JFrame.
Here is my code:
private static void list(){
JFrame frame = new JFrame();
frame.setTitle("Menu");
frame.setSize(500, 500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
frame.setLayout(new FlowLayout());
listModel = new DefaultListModel();
listModel.addElement("Add Member");
listModel.addElement("Add Meeting");
listModel.addElement("Record Attendance");
list = new JList(listModel);
list.setVisibleRowCount(3);
list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
list.setSelectedIndex(0);
JScrollPane listScrollPane = new JScrollPane(list);
frame.add(listScrollPane, BorderLayout.CENTER);
}
Currently when I run the program the frame will open, but it is blank. Any help?
Make frame.setVisible(true); the last line of the function.
You have set the layout as frame.setLayout(new FlowLayout()); but you have used
frame.add(listScrollPane, BorderLayout.CENTER); try changing the layout to border layout eg: frame.setLayout(new BorderLayout());
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 have a frame with 4 JPanels and 1 JScrollPane, the 4 panels are in border layout north, east, south, west and the scrollpane in the center.
I have been trying to get the pack method for a frame functioning but when run you just get the title bar of the window.
Any Ideas?
JFrame conFrame;
JPanel panel1;
JPanel panel2;
JPanel panel3;
JPanel panel4;
JScrollPane listPane;
JList list;
Object namesAr[];
...
...
...
namesAr= namesA.toArray();
list = new JList(namesAr);
list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
list.setLayoutOrientation(JList.HORIZONTAL_WRAP);
list.setVisibleRowCount(-3);
list.addListSelectionListener(this);
listPane = new JScrollPane(list);
panel1 = new JPanel();
panel2 = new JPanel();
panel3 = new JPanel();
panel4 = new JPanel();
conFrame.setLayout(new BorderLayout());
panel1.setPreferredSize(new Dimension(100, 100));
panel2.setPreferredSize(new Dimension(100, 100));
panel3.setPreferredSize(new Dimension(100, 100));
panel4.setPreferredSize(new Dimension(100, 100));
panel1.setBackground(Color.red);
panel2.setBackground(Color.red);
panel3.setBackground(Color.red);
panel4.setBackground(Color.red);
conFrame.pack();
conFrame.add(panel1, BorderLayout.NORTH);
conFrame.add(panel2, BorderLayout.EAST);
conFrame.add(panel3, BorderLayout.SOUTH);
conFrame.add(panel4, BorderLayout.WEST);
conFrame.add(listPane, BorderLayout.CENTER);
conFrame.setVisible(true);
You need to add the panels to the frame "before" you do the pack() otherwise there is nothing to pack.
Also, the default layout for a frame is the BorderLayout.