Java - extendes from JPanel adding new panels inside - java

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);
}
}
}

Related

How can I make this example with java GUI JFrame?

I want to make the tag (이름: 홍길동 학번: 1111111) looks like this,
이름:
홍길동
학번:
111111
but only I can make looks like this,
이름: 홍길동 학번: 111111
I made it to JLabel on SidePanel which extends JPanel. And \n is not working on JPanel I guess? ..and I don't know how to fix it.
Do I need to make some other JPanel on the SidePanel or use another Layout?? like.. Grid or null? or more JLabel??
Here's my code.
public class MyFrame extends JFrame {
private JButton proscons = new JButton();
private JLabel tag = new JLabel();
private JLabel num = new JLabel();
MyFrame() {
setTitle("융프2 기말고사");
Container cp = getContentPane();
cp.setLayout(new BorderLayout());
cp.add(new WestPanel(), BorderLayout.WEST);
cp.add(new MyPanel(), BorderLayout.CENTER);
setLocationRelativeTo(null); // 가운데서 GUI 창 뜨도록
setSize(400, 400);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
class WestPanel extends JPanel {
WestPanel() {
setBackground(Color.YELLOW);
setSize(100,400);
add(proscons);
proscons.setText("찬성");
add(tag);
tag.setText("이름: \n홍길동");
add(num);
num.setText("학번: \n11111111");
}
}
class MyPanel extends JPanel {
MyPanel() {
setBackground(Color.lightGray);
}
}
public static void main(String[] args) {
new MyFrame();
}
}
You can use HTML text formatting in JLabels.
Try doing this:
tag.setText("<html>이름:<br>홍길동</html>");
num.setText("<html>학번:<br>11111111</html>");

I'm trying to insert a JPanel into a JFrame but the JPanel dont show off

So i have 3 classes.The first one is for creating a frame :
public class DrawingFrame extends JFrame{
public DrawingFrame(){
JFrame abc = new JFrame("TEST");
abc.setSize(600,500);
abc.setLayout(null);
abc.setVisible(true);
abc.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Toolbar top = new Toolbar();
abc.add(top);
}
}
The second one is for JPanel :
public class Toolbar extends JPanel{
public Toolbar(){
JPanel top = new JPanel();
top.setLayout(null);
top.setVisible(true);
top.setPreferredSize(new Dimension(150,150));
top.setBackground(Color.RED);
JButton buton = new JButton("Hello!");
buton.setBounds(40, 40, 40, 40);
top.add(buton);
}
}
And this is the main class:
public class Main {
public static void main(String[] args) {
DrawingFrame a = new DrawingFrame();
}
}
My code prints out the frame but not with the panel.How i can fix this ?
First, you don't need to create an instance JPanel in a subclass of JPanel, same for JFrame. The instance is already one.
Use this to access the instance itself :
public Toolbar(){
this.setLayout(null);
this.setVisible(true);
this.setPreferredSize(new Dimension(150,150));
this.setBackground(Color.RED);
JButton buton = new JButton("Hello!");
buton.setBounds(40, 40, 40, 40);
this.add(buton);
}
Second, if you use a null layout, you need to set the bounds of each componennt, as mention in Doing Without a Layout Manager (Absolute Positioning)
Creating a container without a layout manager involves the following steps.
Set the container's layout manager to null by calling setLayout(null).
Call the Component class's setbounds method for each of the container's children.
Call the Component class's repaint method.
So adding the bounds to the JPanel will be enough with : top.setBounds(0,0,150,150); for example
class DrawingFrame extends JFrame{
public DrawingFrame(){
super("TEST");
this.setSize(600,500);
this.setLayout(null);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Toolbar top = new Toolbar();
top.setBounds(0, 0, 150, 150);
this.add(top);
}
}
class Toolbar extends JPanel{
public Toolbar(){
this.setLayout(null);
this.setVisible(true);
this.setPreferredSize(new Dimension(150,150));
this.setBackground(Color.RED);
JButton buton = new JButton("Hello!");
buton.setBounds(40, 40, 40, 40);
this.add(buton);
}
}
And this will look like what you asked (in term of dimension and absolute position)
Move abc.setVisible(true); to the end
Also, use this instead of create new panel in toolbar. Same is in the case of frame as well
So, the below code will work:
public static void main(String...s) {
DrawingFrame a = new DrawingFrame();
}
class DrawingFrame extends JFrame{
public DrawingFrame(){
super("TEST");
this.setLayout(new FlowLayout());
Toolbar top = new Toolbar();
this.add(top);
this.setSize(600,500);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
}
class Toolbar extends JPanel{
public Toolbar(){
this.setLayout(new FlowLayout());
this.setPreferredSize(new Dimension(150,150));
this.setBackground(Color.RED);
JButton buton = new JButton("Hello!");
buton.setBounds(40, 40, 40, 40);
this.add(buton);
this.setVisible(true);
}
}

JTabbedPane component's tabs not showing up

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();
}}

Custom JPanel class does not show up in BoxLayout of Container

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.

How to change CardLayout panels from a separate panel?

My software layout is kinda wizard-base. So the base panel is divided into two JPanels. One left panel which never changes. And one right panel that works with CardLayout. It has many sub-panels and show each one of them by a method.
I can easily go from one inner panel to another one. But I want to have a button in left panel and change panels of the right side.
Here is a sample code which you can run it:
BASE:
public class Base {
JFrame frame = new JFrame("Panel");
BorderLayout bl = new BorderLayout();
public Base(){
frame.setLayout(bl);
frame.setSize(800, 600);
frame.add(new LeftBar(), BorderLayout.WEST);
frame.add(new MainPanel(), BorderLayout.CENTER);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
/**
* #param args the command line arguments
*/
public static void main(String[] args) throws IOException {
// TODO code application logic here
new Base();
}
}
Left side
public class LeftBar extends JPanel{
JButton button;
MainPanel mainPanel = new MainPanel();
public LeftBar(){
setPreferredSize(new Dimension(200, 40));
setLayout(new BorderLayout());
setBackground(Color.black);
button = new JButton("Show Second Page");
button.addActionListener(new ActionListener(){
#Override
public void actionPerformed(ActionEvent ae) {
mainPanel.showPanel("secondPage");
}
});
add(button, BorderLayout.NORTH);
}
}
Right Side
public class MainPanel extends JPanel {
private CardLayout cl = new CardLayout();
private JPanel panelHolder = new JPanel(cl);
public MainPanel(){
FirstPage firstPage = new FirstPage(this);
SecondPage secondPage = new SecondPage(this);
setLayout(new GridLayout(0,1));
panelHolder.add(firstPage, "firstPage");
panelHolder.add(secondPage, "secondPage");
cl.show(panelHolder, "firstPage");
add(panelHolder);
}
public void showPanel(String panelIdentifier){
cl.show(panelHolder, panelIdentifier);
}
}
Inner panels for right side:
public class FirstPage extends JPanel {
MainPanel mainPanel;
JButton button;
public FirstPage(MainPanel mainPanel) {
this.mainPanel = mainPanel;
setBackground(Color.GRAY);
button = new JButton("Show page");
button.addActionListener(new ActionListener(){
#Override
public void actionPerformed(ActionEvent ae) {
mainPanel.showPanel("secondPage");
}
});
add(button);
}
}
public class SecondPage extends JPanel{
MainPanel mainPanel;
JButton button;
public SecondPage(MainPanel mainPanel){
this.mainPanel = mainPanel;
setBackground(Color.white);
add(new JLabel("This is second page"));
}
}
And this is a picture to give you the idea:
As I explained, I can travel "from first" page to "second page" by using this method: mainPanel.showPanel("secondPage"); or mainPanel.showPanel("firstPage");.
But I also have a JButton in the left bar, which I call the same method to show the second panel of the CardLayout. But it does not work. It doesnt give any error though.
Any idea how to change these CardLayout panels from outside of panels?
The problem is that LeftBar has mainPanel member that is initialized to a new instance of MainPanel. So you have two instances of MainPanel, one allocated in Base and added to the frame, the other one allocated in LeftBar.
So LeftBar executes mainPanel.showPanel("secondPage"); on a second instance of MainPanel which is not even a part of a visual hierarchy. To fix this just pass an existing instance of MainPanel to the constructor of LeftBar. You already do this in FirstPage and SecondPage.

Categories

Resources