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();
}}
Related
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());
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.
I'm creating a notepad program in Java and would like to have different tabs to for each document opened. I'm having trouble getting the tabs to be displayed. This is my test document so far that I have looked over here at first and I modified it for the text document. How to add a JScrollPane onto a JTabbedPane using a null layout?.
This is what I currently have:
import java.awt.*;
import javax.swing.*;
public class Test extends JFrame {
private static void CreateAndShowGui() {
JFrame frame = new JFrame("Program");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JTextPane txt = new JTextPane();
JPanel noWrapPanel = new JPanel(new BorderLayout());
noWrapPanel.add(txt);
JScrollPane scroll = new JScrollPane(noWrapPanel);
JPanel topPanel = new JPanel();
topPanel.setLayout(new BorderLayout());
frame.add(topPanel);
JPanel panel = new JPanel();
panel.setLayout(new BorderLayout());
panel.add(scroll, BorderLayout.CENTER);
JTabbedPane tabbedPane = new JTabbedPane();
tabbedPane.addTab("Welcome", panel);
topPanel.add(tabbedPane);
frame.add(scroll);
frame.setSize(400, 200);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
UIManager.put("swing.boldmetal", Boolean.FALSE);
CreateAndShowGui();
}
});
}
}
What am I doing wrong? :(
you are adding scroll pane to frame .
frame.add(scroll);
but you should add scroll pane to jpanel and add pane to scroll pane.you have done that part.
so remove this incorrect line.
frame.add(scroll);
this is complete code without using extends jframe .
this is complete code using extends jframe
note: you have extends your class by jframe class but you are creating a new frame .you don't need to create a frame variable .you can either remove extends part or directly use your class as a jframe without creating a new frame.
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)
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class File
{
private JFrame frame1;
private JPanel panel1;
private JPanel panel2;
private JLabel labelWeight;
private JLabel labelHeight;
File()
{
frame1 = new JFrame();
panel1 = new JPanel();
panel2 = new JPanel();
labelWeight = new JLabel("Weight :");
labelHeight = new JLabel("Height :");
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
panel1.setLayout(new FlowLayout());
panel1.add(labelWeight);
panel2.setLayout(new FlowLayout());
panel2.add(labelHeight);
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
frame1.setLayout(new BoxLayout(frame1,BoxLayout.X_AXIS));
panel1.setAlignmentY(0);
panel2.setAlignmentY(0);
frame1.add(panel1);
frame1.add(panel2);
frame1.setSize(400, 200);
frame1.setDefaultCloseOperation(frame1.EXIT_ON_CLOSE);
frame1.setVisible(true);
}
public static void main (String args[])
{
new File();
}
}
It gives BoxLayout Sharing error at runtime
Generally, LayoutManagers are set on a JPanel. I guess JFrame implements this method to forward it to the content pane of the frame. I would suggest you try:
Container contentPane = frame1.getContentPane();
contentPane.setLayout(new BoxLayout(contentPane,BoxLayout.X_AXIS));
If you still have problems take a look at the Swing tutorial on How to Use Box Layout for working examples.
Swing components should be created in the Event Dispatch Thread. Try this in your main():
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
new File();
}
});
But your problem may be the same as this question.