I am trying to add three panels on a single JFrame form. if i am only adding three panels they are being displayed but if i add the panel on split pane nothing is being displayed
suggest the error in following code
import javax.swing.*;
import java.awt.*;
class paneltest extends JFrame{
paneltest()
{
Container cp=this.getContentPane();
cp.setLayout(null);
panel1 p1= new panel1();
panel2 p2= new panel2();
panel3 p3= new panel3();
cp.add(p1);
cp.add(p2);
cp.add(p3);
Dimension screenSize=Toolkit.getDefaultToolkit().getScreenSize();
p1.setBounds(0,0,screenSize.width/3,screenSize.height);
p2.setBounds(screenSize.width/3,0,screenSize.width/3,screenSize.height);
p3.setBounds(2*(screenSize.width/3),0,screenSize.width/3,screenSize.height);
try{
JSplitPane splitPaneLeft = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
JSplitPane splitPaneRight = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
splitPaneLeft.setLeftComponent( p1 );
splitPaneLeft.setRightComponent( p2 );
splitPaneRight.setLeftComponent( splitPaneLeft );
splitPaneRight.setRightComponent( p3 );
JPanel panelSplit = new JPanel();
panelSplit.add(splitPaneRight);
cp.add(panelSplit);
panelSplit.setVisible(true);
}
catch(Exception ex)
{
JOptionPane.showMessageDialog(null,"exception occured"+ex);
}
}
public static void main(String arsg[])
{
paneltest frm= new paneltest();
frm.show ();
}
}
class panel1 extends JPanel
{
panel1()
{
setLayout(new FlowLayout());
JLabel l1= new JLabel("panel1");
add(l1);
}
}
class panel2 extends JPanel
{
panel2()
{
setLayout(new FlowLayout());
JLabel l1= new JLabel("panel2");
add(l1);
}
}
class panel3 extends JPanel
{
panel3()
{
setLayout(new FlowLayout());
JLabel l1= new JLabel("panel3");
add(l1);
}
}
Remove the line cp.setLayout(null). This will fix the initial problem.
After that:
indent the code
respect Java naming conventions
don't add panels to the content pane if you add them to the splitpanes right after. A component can be added to a single parent. It doesn't make sense to add them to both
don't use setBounds(). That's the role of the layout manager
don't extend JPanel and JFrame. Use them
Respect Swing's threading policy.
Don't catch (Exception)
Related
I'm trying to create a canvas for my oval shape, and I want it to be different from the main JFrame color. So far, using setSize upon the panel doesn't work, it ended up creating a small box that I couldn't draw in. Here is the panel design that I intended it to be, with the white-colored part as the main frame.
PanelDesign
As I've said, using all three Layout modes (Border, Flow, and Grid) only creates a yellow small box in the upper middle part of the frame. This is the code that I use.
How can I create the panel design similar to the image posted above?
setTitle("Oval Shape Mover");
setSize(500, 200);
setLayout(new BorderLayout());
JPanel mainpanel, panel1, panel2;
mainpanel = new JPanel();
panel1 = new JPanel();
panel2 = new JPanel();
panel1.setBackground(Color.YELLOW);
mainpanel.add(panel1, BorderLayout.CENTER);
mainpanel.add(panel2);
add(mainpanel);
setVisible(true);
The layouts used to make Java Swing GUIs will more often honor the preferred size than the size. Having said that, a custom rendered component should override (rather than set) getPreferredSize().
This example suggests a preferred size by using a JLabel to display the icon, and empty borders to pad the GUI out.
import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
import java.net.*;
public class RedDotLayout {
private JComponent ui = null;
String urlToRedCircle = "https://i.stack.imgur.com/wCF8S.png";
RedDotLayout() {
try {
initUI();
} catch (MalformedURLException ex) {
ex.printStackTrace();
}
}
public final void initUI() throws MalformedURLException {
ui = new JPanel(new BorderLayout());
ui.setBackground(Color.YELLOW);
ui.setBorder(new LineBorder(Color.BLACK, 2));
JLabel label = new JLabel(new ImageIcon(new URL(urlToRedCircle)));
label.setBorder(new CompoundBorder(
new LineBorder(Color.GREEN.darker(), 2),
new EmptyBorder(20, 200, 20, 200)));
ui.add(label, BorderLayout.CENTER);
JPanel bottomPanel = new JPanel();
bottomPanel.setBackground(Color.WHITE);
bottomPanel.setBorder(new EmptyBorder(30, 50, 30, 50));
ui.add(bottomPanel, BorderLayout.PAGE_END);
}
public JComponent getUI() {
return ui;
}
public static void main(String[] args) {
Runnable r = () -> {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception useDefault) {
}
RedDotLayout o = new RedDotLayout();
JFrame f = new JFrame(o.getClass().getSimpleName());
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
f.setLocationByPlatform(true);
f.setContentPane(o.getUI());
f.pack();
f.setMinimumSize(f.getSize());
f.setVisible(true);
};
SwingUtilities.invokeLater(r);
}
}
Given a JFrame whose contentPane's layout is set to null, I would like to add two tabs one for Publisher, the other is for Subscriber such as :
public class PubSubGUI extends JFrame{
private JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
private JPanel pubPanel = new JPanel();
private JPanel subPanel = new JPanel();
public PubSubGUI(Controller controller) {
getContentPane().setLayout(null);
getContentPane().add(tabbedPane);
//add Publisher components to pubPanel
tabbedPane.addTab("Publlisher", pubPanel);
//add Subscriber components to pubPanel
tabbedPane.addTab("Subscriber", subPanel);
//Rest of the constructor's source code is omitted
}
//Rest of the class' source code is omitted
}
When running the application neither the components nor the tabs are displayed. All I am getting is an empty JFrame. I tried to set different LayoutManagers to each of pubPanel and subPanel, still the problem persists. Any hints or suggestions please.
Refer This:
import javax.swing.*;
public class TabbedPaneExample {
JFrame f;
TabbedPaneExample(){
f=new JFrame();
JTextArea ta=new JTextArea(200,200);
JPanel p1=new JPanel();
p1.add(ta);
JPanel p2=new JPanel();
JPanel p3=new JPanel();
JTabbedPane tp=new JTabbedPane();
tp.setBounds(50,50,200,200);
tp.add("main",p1);
tp.add("visit",p2);
tp.add("help",p3);
f.add(tp);
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
}
public static void main(String[] args) {
new TabbedPaneExample();
}}
I am simply trying to add a tabbed pane with 5 tabs onto a panel, although only the final tab (tab e) is being shown.
I am obviously doing something fundamentally wrong here, I have tried changing the layout manager of the Panel the tabbed pane is being added to but I don't think this is the problem. Any adivce would be helpful thanks!
Main Class Code:
public static void main(String[] args) {
JFrame frame = new JFrame("Data Structures Program");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(600, 600);
GraphicPanel G = new GraphicPanel();
frame.add(G.getPanel());
frame.setVisible(true);
}
Graphics Class
public class GraphicPanel {
public JPanel topPanel;
public GraphicPanel() {
JPanel Panel = new JPanel();
Panel.setLayout(new GridLayout(1, 1));
JTabbedPane tabbedPane = new JTabbedPane();
tabbedPane.addTab("a", Panel);
tabbedPane.addTab("b", Panel);
tabbedPane.addTab("c", Panel);
tabbedPane.addTab("d", Panel );
tabbedPane.addTab("e", Panel );
topPanel = new JPanel();
topPanel.setLayout(new GridLayout(1, 1));
topPanel.add(tabbedPane);
}
public JPanel getPanel(){
return topPanel;
}
}
you must creates new instance of JPanel if you want to show in JTabbedPane
try this code:
JTabbedPane tabbedPane = new JTabbedPane();
tabbedPane.addTab("a", new Panel());
tabbedPane.addTab("b", new Panel());
tabbedPane.addTab("c", new Panel());
tabbedPane.addTab("d", new Panel());
tabbedPane.addTab("e", new Panel());
I want to add an object of type JPanel to a JFrame.
I'm trying this, but the Jpanel is not added.
the idea is: Add to P2 a P5 that has the components defined in class P5.
What could be happening ?, I do not want to create all the JPanel in the class First_view, since the code would be messed up a lot.
CODE:
import javax.swing.*;
import java.awt.*;
public class First_view extends JFrame {
Container Frame;
public First_view() {
super("title");
Frame = this.getContentPane();
Frame.setLayout(new BorderLayout());
Frame.add((new P2()), BorderLayout.WEST);
setSize(900, 500);
setLocationRelativeTo(null);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}
class P2 extends JPanel {
public P2() {
this.setLayout(new BorderLayout());
add((new P5()), BorderLayout.NORTH);
}
}
class P5 extends JPanel {
JScrollPane informacion = new JScrollPane(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
JTextArea T1 = new JTextArea();
public P5() {
setLayout(new FlowLayout());
setBorder(BorderFactory.createEmptyBorder(20, 10, 0, 0));
add(setInformacion());
}
private JScrollPane setInformacion() {
T1.append("Information, bla bla bla");
T1.setEditable(false);
T1.setBorder(BorderFactory.createLineBorder(Color.BLACK));
T1.setLineWrap(true);
informacion.add(T1);
informacion.setBorder(BorderFactory.createLineBorder(Color.BLACK));
return informacion;
}
}
PIC:
The component to display in the JScrollPane should not be added, use setViewportView instead.
private JScrollPane setInformacion() {
T1.append("Information, bla bla bla");
...
informacion.setViewportView(T1);
...
informacion.setBorder(BorderFactory.createLineBorder(Color.BLACK));
return informacion;
}
Obs: the arguments passed to the constructor of JScrollPane are in the wrong order, that is, the vertical police comes first:
JScrollPane informacion = new JScrollPane(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
Edit: as Andrew commented it is not a good idea to extend a class just to use it (JFrame, JPanel). Example, I tried not to change too much of your original flow:
package cfh.test;
import javax.swing.*;
import java.awt.*;
public class FirstView {
private JFrame frame;
private JTextArea informacionText; // not sure if that is needed as field
public FirstView() {
informacionText = new JTextArea();
informacionText.setEditable(false);
informacionText.setBorder(BorderFactory.createLineBorder(Color.BLACK));
informacionText.setLineWrap(true);
informacionText.append("Information, bla bla bla");
JScrollPane scrollPane = new JScrollPane(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
scrollPane.setBorder(BorderFactory.createLineBorder(Color.BLACK));
scrollPane.setViewportView(informacionText);
JPanel infoPanel = new JPanel();
infoPanel.setLayout(new FlowLayout());
infoPanel.add(scrollPane);
JPanel leftPanel = new JPanel();
leftPanel.setLayout(new BorderLayout());
leftPanel.add(infoPanel, BorderLayout.NORTH);
// TODO consider moving above code to an own method returning the left panel
frame = new JFrame("title");
frame.setLayout(new BorderLayout());
frame.add(leftPanel, BorderLayout.WEST);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(900,500);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
Selected code from SCMain.java:
public JPanel createContentPane() {
//Create the content-pane-to-be.
JPanel contentPane = new JPanel(new BorderLayout());
contentPane.setOpaque(true);
return contentPane;
}
public JPanel populateContentPane() {
JPanel container = new JPanel();
container.setLayout(new BoxLayout(container, BoxLayout.X_AXIS));
JPanel panel1 = new AddAccountForm(this, this.getInputSet());;
JPanel panel2 = new JPanel();
container.add(Box.createRigidArea(new Dimension(0, 5)));
container.add(panel1);
container.add(Box.createRigidArea(new Dimension(0, 5)));
container.add(panel2);
container.add(Box.createGlue());
return container;
}
/**
* Create the GUI and show it. For thread safety,
* this method should be invoked from the
* event-dispatching thread.
*/
private void createAndShowGUI() {
//Create and set up the window.
JFrame frame = new JFrame("Sole Commando v1.0");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Create and set up the content pane.
frame.setJMenuBar(this.createMenuBar());
frame.setContentPane(this.createContentPane());
// Add split panels
frame.add(populateContentPane(), BorderLayout.CENTER);
frame.pack();
//Display the window.
frame.setSize(1080, 1080);
frame.setVisible(true);
}
Selected code from AddAccountForm.java:
public class AddAccountForm extends JPanel implements ActionListener{
AddAccountForm(SCMain main, Set<String> InputSet) {
//Combobox setup
setSize(300, 300);
System.out.println(Arrays.toString(storeNameList.toArray()));
submitButton.addActionListener(this);
}
public JPanel getAddAccountRoot() {
return addAccountRoot;
}
private void createUIComponents() {
// TODO: place custom component creation code here
storeNames = new JComboBox();
}
}
I tested using the AddAccountForm.java as a JFrame (extend JFrame instead of extend JPanel, and adding pack(), setContentPane(addAccountRoot) to AddAccountForm.java) and it brought up the correct AddAccountForm GUI if I just did:
SCMain new1 = new SCMain();
AddAccountForm new2 = new AddAccountForm(new1, new1.getInputSet());
However, when using it as a JPanel (panel1 in the above SCMain.java code) and running SCMain, AddAccountForm GUI does not show up at all.
Note: The JPanel AddAccountForm was created in IntelliJ GUI Builder, but as I said previously it works as a JFrame so the code must be somewhat correct.