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.
Related
I'm using a JPanel and tring to create a two new panels programatically inside this JPanel
public class MainWindow extends javax.swing.JFrame {
/**
* Creates new form MainWindow
*/
private javax.swing.JPanel jviewer;
public MainWindow() {
initComponents();
jviewer = new ImageRender(123);
}
}
For that reason, I have the next extension:
public class ImageRender extends JPanel {
JPanel mainViewer = new JPanel();
JPanel galleryViewer = new JPanel();
public ImageRender(Integer itemnum) {
setLayout(null);
mainViewer = new JPanel();
mainViewer.setBackground(Color.red);
mainViewer.setBounds(0, 0, 200, 200);
galleryViewer = new JPanel();
galleryViewer.setBackground(Color.green);
galleryViewer.setBounds(210, 0, 50, 200);
this.add(mainViewer);
add(galleryViewer);
mainViewer.setVisible(true);
setVisible(true);
System.out.println("Se ha finalizado esta tarea");
}
}
But, at this point, is not displaying any of the JPanel created in the ImageRender.java also any errors.
Someone has an idea about how to fix my implementation?
Reason of that is that you create an ImageRender instance, but it is never being added to the JFrame. Use add method in order to achieve that. Also do not use setLayout(null) and setBounds. Use a layout manager instead. Components will be validated automatically, plus you have a resizable window.
The fact ImageRender extends JPanel means that an ImageRender object is also a JPanel, hence it can be added to the frame. (Already mentioned in comments)
Take a look at an example (assume ImageRenderer class is in another file):
public class Test extends JFrame {
public Test() {
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(400, 400);
setLocationRelativeTo(null);
getContentPane().setLayout(new BorderLayout());
getContentPane().add(new ImageRenderer()); //Create and add a new ImageRendere panel
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> new Test().setVisible(true));
}
private static class ImageRenderer extends JPanel {
public ImageRenderer() {
super(new GridLayout());
JPanel leftPanel = new JPanel(new BorderLayout());
leftPanel.setBackground(Color.green);
add(leftPanel);
JPanel rightPanel = new JPanel(new BorderLayout());
rightPanel.setBackground(Color.blue);
add(rightPanel);
}
}
}
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 have two classes - one representing a JFrame and the other representing a JPanel. On the JPanel I have JComponents that I would want to use in capturing user input. On the JFrame I have a JButton that displays the JPanel on JDialog when a user hit it. Below is the code to achieve this:
A class that is extending a JFrame.
public class ControlPanel extends JFrame {
public JButton display;
ControlPanel() {
this.createUI();
// button action
this.display.addActionListener((ActionEvent) -> {
AddUser ad = new AddUser();
JDialog dialog = new JDialog(this,
Dialog.ModalityType.APPLICATION_MODAL);
dialog.setVisible(true);
dialog.setSize(300, 300);
dialog.add(ad);
});
}
public void createUI() {
this.setTitle("Dispalying a JPanel on JDialog");
this.setSize(new Dimension(300, 300));
this.setVisible(true);
this.setLayout(new BorderLayout(5, 5));
this.display = new JButton("Display");
this.add(new JLabel("North", JLabel.CENTER), BorderLayout.NORTH);
this.add(this.display, BorderLayout.CENTER);
this.add(new JLabel("South", JLabel.CENTER), BorderLayout.SOUTH);
this.add(new JLabel("East"), BorderLayout.EAST);
this.add(new JLabel("West"), BorderLayout.WEST);
}
public static void main(String[] args) {
ControlPanel cp = new ControlPanel();
}
}
A class that is extending a JPanel.
This panel is the one that I would want to be added on to the JDialog.
public class AddUser extends JPanel {
public AddUser() {
this.prepareUI();
}
public void prepareUI() {
this.setPreferredSize(new Dimension(200, 200));
this.setLayout(new GridLayout(1, 4, 5, 5));
this.add(new JLabel("Label1"));
this.add(new JLabel("Label2"));
this.add(new JLabel("Label2"));
this.add(new JLabel("Label2"));
/* Code to test if the panel is displaying fine.
* The panel is displaying fine.
* JFrame fr = new JFrame();
fr.setVisible(true);
fr.setSize(300, 300);
fr.setLayout(new BorderLayout(5, 5));
fr.add(this, BorderLayout.CENTER);
fr.pack();*/
}
public static void main(String[] args) {
AddUser ad = new AddUser();
}
}
NOTE : I am doing this because I want the panel to be modal - the user must not interact with components on the JFrame before he is finished with the JPanel.
Problem : The Jpanel [this one:AddUser ad = new AddUser();] is not showing when the JDialog is shown.
Question : What went wrong with my code and how do I solve it?
Below is the out put when the display button is clicked.
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)