JTextPane doesn't display JScrollPane and doesn't Wrap Text - java

I need to display links so I'm using JTextPane with setContentType. However, the content doesn't wrap and there's no scroll. The content of JTextPane will be returned from a RSS feed. Here's the full code:
import java.awt.*;
import javax.swing.*;
class Main extends JFrame
{
JFrame frame;
JTabbedPane tabbedPane;
JPanel home, news;
public Main()
{
setTitle("My Title" );
setSize( 900, 600 );
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
home();
news();
tabbedPane = new JTabbedPane();
tabbedPane.addTab( " Home", home );
tabbedPane.addTab( "News", news );
JPanel framePanel = new JPanel();
framePanel.setLayout(new BorderLayout());
framePanel.add( tabbedPane, BorderLayout.CENTER );
getContentPane().add( framePanel );
}
public void home()
{
home = new JPanel();
// some stuffs here
}
public void news()
{
news = new JPanel();
JTextPane newsTextPane = new JTextPane();
newsTextPane.setContentType("text/html");
newsTextPane.setEditable(false);
JScrollPane scrollPane = new JScrollPane(newsTextPane);
scrollPane.setVerticalScrollBarPolicy(javax.swing.ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
news.add(scrollPane);
RSS reader = RSS .getInstance();
reader.writeNews();
String rssNews = reader.writeNews();
newsTextPane.setText(rssNews);
}
public static void main( String args[] )
{
RSS reader = RSS.getInstance();
reader.writeNews();
Main mainFrame = new Main();
mainFrame.setVisible( true );
mainFrame.setDefaultCloseOperation( EXIT_ON_CLOSE );
}
}
My result:

I just used your code and it does not cause any problems:
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.SwingUtilities
public class TestScrolling {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
initUI();
});
}
public static void initUI() {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 100; i++) {
sb.append("loads loads loads loads of text here ");
}
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JTextPane newsTextPane = new JTextPane();
newsTextPane.setContentType("text/html");
newsTextPane.setEditable(false);
newsTextPane.setText(sb.toString());
JScrollPane scrollPane = new JScrollPane(newsTextPane);
scrollPane.setVerticalScrollBarPolicy(
javax.swing.ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
frame.add(scrollPane);
frame.setSize(300, 200);
frame.setVisible(true);
}
}
EDIT:
You have to force somehow the width of the scrollPane. In my example it is done implicitly by adding the scrollpane to the content pane of the frame, which by default uses the BorderLayout. In your case, you used a FlowLayout which allocates the preferred size of the scrollpane which is about the preferred size of the JTextPane.

Are you using a panel or something around your JScrollPane?
Taking the sscc of #Guillaume Polet with innapropriate size the example won't work :
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
public class TestScrolling {
public static void main(String[] args) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 100; i++) {
sb.append("loads loads loads loads of text here ");
}
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JTextPane newsTextPane = new JTextPane();
newsTextPane.setContentType("text/html");
newsTextPane.setEditable(false);
newsTextPane.setText(sb.toString());
JScrollPane scrollPane = new JScrollPane(newsTextPane);
scrollPane.setVerticalScrollBarPolicy(javax.swing.ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
JPanel pan = new JPanel();
pan.setMinimumSize(new Dimension(500,500));
pan.add(scrollPane);
frame.add(pan);
frame.setSize(500, 500);
frame.setVisible(true);
}
}
I see your adding your JscrollPane to panel. Can you provide the creation/modification you made on that panel and where this panel is used?

Related

How can I make the JMenuBar strape dissapear after the buttons?

How can I make the JMenuBar streak dissapear after the menuitems? I want to put a 2nd menu in the middle of the frame, and it looks weird with this streak.
I tried to set the background to Panel.background but it doesn't work.
import java.awt.*;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenuBar;
import javax.swing.JPanel;
import javax.swing.UIManager;
public class SwingMenuDemo extends JPanel {
public SwingMenuDemo(){
setLayout(null);
JMenuBar menuBar = new JMenuBar();
menuBar.setBackground(UIManager.getColor("Panel.background"));
menuBar.setBounds(0, 0, 450, 25);
add(menuBar);
JButton btn1 = new JButton("Menu1");
menuBar.add(btn1);
JButton btn2 = new JButton("Menu2");
menuBar.add(btn2);
JButton btn3 = new JButton("Menu3");
menuBar.add(btn3);
}
private static void createAndShowGUI() {
JFrame frame = new JFrame("SwingMenuDemo");
frame.setPreferredSize(new Dimension(400,400));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(new BoxLayout(frame.getContentPane(),BoxLayout.PAGE_AXIS));
frame.getContentPane().add(new SwingMenuDemo());
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
If you want to create your own "menu" of several JButtons, then don't use JMenu and don't use null layout and setBounds(...) (you should avoid the latter just as a general rule). Instead nest JPanels, each using its own layout manager to allow for simple creation of complex GUI's.
For instance you could create a JPanel to hold the buttons, say called menuPanel, give it a new GridLayout(1, 0) layout, meaning it will hold a grid of components in 1 row, and a variable number of columns (that's what the 0 means). Then put your buttons in that.
Then place that JPanel into another JPanel that uses say FlowLayout.LEADING, 0, 0) as its layout -- it will push all its components to the left.
Then make the main GUI use a BorderLayout and add the above panel to it's top in the BorderLayout.PAGE_START position. For example:
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import javax.swing.*;
#SuppressWarnings("serial")
public class SwingMenuDemo2 extends JPanel {
private static final String[] MENU_TEXTS = {"Menu 1", "Menu 2", "Menu 3"};
private static final int PREF_W = 400;
private static final int PREF_H = PREF_W;
public SwingMenuDemo2() {
JPanel menuPanel = new JPanel(new GridLayout(1, 0));
for (String menuText : MENU_TEXTS) {
menuPanel.add(new JButton(menuText));
}
JPanel topPanel = new JPanel(new FlowLayout(FlowLayout.LEADING, 0, 0));
topPanel.add(menuPanel);
setLayout(new BorderLayout());
add(topPanel, BorderLayout.PAGE_START);
}
#Override
public Dimension getPreferredSize() {
if (isPreferredSizeSet()) {
return super.getPreferredSize();
}
return new Dimension(PREF_W, PREF_H);
}
private static void createAndShowGui() {
SwingMenuDemo2 mainPanel = new SwingMenuDemo2();
JFrame frame = new JFrame("Swing Menu Demo 2");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> createAndShowGui());
}
}
You can do that with
menuBar.setBorderPainted(false);

Placing the JPanel in JScrollPane [duplicate]

I've got a probelm with my swing ui lately. Everything works fine,untill i trigger a tooltip from a JButton.After that moving the mouse over the rest of the ui is causing weird artifacts and glitching.
Bugged:
I can't show the whole code because its too much but here im initialising the button :
GridBagConstraints bottompane_gbc = new GridBagConstraints();
toggleTorConnectionButton = new JButton();
toggleTorConnectionButton.setToolTipText("Toggles Tor Connection.");
toggleTorConnectionButton.setIcon(new ImageIcon(ResourceHandler.Menueicon3_1));
toggleTorConnectionButton.setMinimumSize(new Dimension(removeFinishedDownloads.getMinimumSize().width, toggleTorConnectionButton.getIcon().getIconHeight()+5));
toggleTorConnectionButton.addActionListener(); // unimportant
bottompane_gbc.gridy = 1;
bottompane_gbc.fill = GridBagConstraints.BOTH;
bottompane_gbc.insets = new Insets(0,15,10,5);
bottompane.add(ToggleTorConnectionButton,bottompane_gbc);
this.add(bottompane,BorderLayout.PAGE_END);
If anybody needs more information to help me pls feel free to ask.Im kind of desperated. XD
EDIT:
After some tinkering im guessing that the problem is related to swing and my use of it.Currently im using alot of Eventlisteners (is this bad?), that might slow down the awt thread ?
Here is a brief extract from HPROF:
http://www.pastebucket.com/96444
EDIT 2:
I was able to recreate the error in a handy and simple example. When you move over the button,wait for the tooltip and then over the ui.You will see ghosting :(.
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
public class Main_frame {
public static void main(String[] args) {
new Main_frame();
}
public Main_frame() {
JFrame frame = new JFrame("LOL");
frame.setFocusable(true);
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(new Dimension(400, 500));
frame.setLocationRelativeTo(null);
Download_window download_window = new Download_window();
JTabbedPane tabbedPane = new JTabbedPane();
tabbedPane.addTab("Download", null, download_window, "Main Download Window.");
for (int i = 0; i < 5; i++) {
JPanel pane = new JPanel();
Dimension dim = new Dimension(370, 60);
pane.setPreferredSize(dim);
pane.setMaximumSize(dim);
pane.setBackground(Color.blue);
pane.setMinimumSize(dim);
download_window.jobpanel.add(pane);
}
download_window.jobpanel.repaint();
download_window.jobpanel.revalidate();
frame.add(tabbedPane);
frame.setVisible(true);
}
public class Download_window extends JPanel {
JPanel jobpanel;
public Download_window() {
this.setLayout(new BorderLayout());
jobpanel = new JPanel();
jobpanel.setLayout(new BoxLayout(jobpanel, BoxLayout.Y_AXIS));
JPanel bottompane = new JPanel();
bottompane.setPreferredSize(new Dimension(385, 40));
JButton toggleTorConnectionButton = new JButton();
toggleTorConnectionButton.setPreferredSize(new Dimension(100, 50));
toggleTorConnectionButton.setToolTipText("Toggles Tor Connection.");
bottompane.add(toggleTorConnectionButton);
this.add(bottompane, BorderLayout.PAGE_END);
JScrollPane jobScrollPane = new JScrollPane(jobpanel);
jobScrollPane.getVerticalScrollBar().setUnitIncrement(16);
this.add(jobScrollPane, BorderLayout.CENTER);
}
}
}
Edit 3: Concerning trashgods ideas, I used the EventDispatchThread, I modified the setter to override the getter for size and i crossed out incompatibility by using trashgods code and it was working fine.... So where is the actual difference?
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
public class Main_frame {
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
new Main_frame();
}
});
}
public Main_frame() {
JFrame frame = new JFrame("LOL");
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(new Dimension(400, 500));
Download_window download_window = new Download_window();
JTabbedPane tabbedPane = new JTabbedPane();
tabbedPane.addTab("Download", null, download_window, "Main Download Window.");
frame.add(tabbedPane);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public class Download_window extends JPanel {
JPanel jobpanel;
public Download_window() {
this.setLayout(new BorderLayout());
jobpanel = new JPanel();
jobpanel.setLayout(new BoxLayout(jobpanel, BoxLayout.Y_AXIS));
for (int i = 0; i < 5; i++) {
JPanel pane = new JPanel(){
#Override
public Dimension getPreferredSize() {
return new Dimension(370, 60);
}
#Override
public Dimension getMaximumSize() {
return new Dimension(370, 60);
}
#Override
public Dimension getMinimumSize() {
return new Dimension(370, 60);
}
};
pane.setBackground(Color.blue);
jobpanel.add(pane);
}
JPanel bottompane = new JPanel(){
#Override
public Dimension getPreferredSize() {
return new Dimension(385, 40);
}
};
JButton toggleTorConnectionButton = new JButton("Button"){
#Override
public Dimension getPreferredSize() {
return new Dimension(100, 30);
}
};
toggleTorConnectionButton.setToolTipText("Toggles Tor Connection.");
bottompane.add(toggleTorConnectionButton);
this.add(bottompane, BorderLayout.PAGE_END);
JScrollPane jobScrollPane = new JScrollPane(jobpanel);
jobScrollPane.getVerticalScrollBar().setUnitIncrement(16);
this.add(jobScrollPane, BorderLayout.CENTER);
}
}
}
Could anyone please verify that strange behavior himself? You just need to copy&paste the code from above in Edit3.
Your code exhibits none of the glitches shown above when run on my platform.
Verify that you have no painting problems e.g. neglecting super.paintComponent() as discussed here.
Verify that you have no driver incompatibilities, as discussed here.
Construct and modify all GUI objects on the event dispatch thread.
Don't use set[Preferred|Maximum|Minimum]Size() when you really mean to override get[Preferred|Maximum|Minimum]Size(), as discussed here. The example below overrides getPreferredSize() on the scroll pane, but you can implement Scrollable, as discussed here.
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
/** #see https://stackoverflow.com/a/34319260/230513 */
public class MainFrame {
private static final int H = 64;
public static void main(String[] args) {
EventQueue.invokeLater(() -> new MainFrame());
}
public MainFrame() {
JFrame frame = new JFrame("LOL");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JTabbedPane tabbedPane = new JTabbedPane();
JPanel panel = new JPanel(new GridLayout(0, 1, 5, 5));
for (int i = 0; i < 8; i++) {
panel.add(new DownloadPanel());
}
JScrollPane jsp = new JScrollPane(panel) {
#Override
public Dimension getPreferredSize() {
return new Dimension(6 * H, 4 * H);
}
};
tabbedPane.addTab("Download", null, jsp, "Main Download Window.");
tabbedPane.addTab("Options", null, null, "Options");
frame.add(tabbedPane);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
private static class DownloadPanel extends JPanel {
JPanel jobPanel = new JPanel();
public DownloadPanel() {
this.setLayout(new BorderLayout());
this.setBackground(Color.lightGray);
JProgressBar jpb = new JProgressBar();
jpb.setIndeterminate(true);
this.add(jpb);
JPanel buttonPane = new JPanel();
JButton toggleTorConnectionButton = new JButton("Button");
toggleTorConnectionButton.setToolTipText("Toggles Tor Connection.");
buttonPane.add(toggleTorConnectionButton);
this.add(buttonPane, BorderLayout.WEST);
}
#Override
public Dimension getPreferredSize() {
return new Dimension(4 * H, H);
}
}
}

ScrollPane in TextArea Layout(null)

Here is part of my code:
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
public class WindowTest {
JFrame window = new JFrame();
JPanel panel = new JPanel();
JTextArea text = new JTextArea();
JScrollPane scroll = new JScrollPane(text);
private WindowTest() {
createWindow();
}
public void createWindow() {
window.setLayout(null);
window.setVisible(true);
panel.setVisible(true);
text.setBounds(20, 100, 320, 270);
scroll.setVisible(true);
window.add(scroll);
}
public static void main(String[] args) {
new WindowTest().createWindow();
}
}
I'm wondering how to add scroll bar to TextArea "text". It's a database app and it sends String of data to TextArea. I want the app to show scrollbar (vertical or horizontal) if necessary - too many Strings in TextArea. I have been trying many things but nothing works. Layout has to be null because I made all components manually and I dont want to set everything from beginning (its only part of the code).
text.setBounds(20, 100, 320, 270);
Don't set bounds on the JTextArea component. It needs to become larger to show the entire text. The JScrollPane will then show a portion of the JTextArea.
Update: also, use a suitable layout manager, don't use absolute positioning.
Corrected code:
import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
public class WindowTest {
JFrame window = new JFrame();
JTextArea text = new JTextArea();
JScrollPane scroll = new JScrollPane(text);
private WindowTest() {
createWindow();
}
public void createWindow() {
window.setLayout(new BorderLayout());
window.add(scroll, BorderLayout.CENTER);
window.setVisible(true);
}
public static void main(String[] args) {
new WindowTest().createWindow();
}
}
Your mistakes:
You must set bounds of the scrollpane not the textarea.
window.setVisible(true); this must be called at the end.
You must set size of your JFrame.
Code below working for me.
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
public class WindowTest {
JFrame window = new JFrame();
JTextArea text = new JTextArea();
JScrollPane scroll = new JScrollPane(text);
private WindowTest() {
createWindow();
}
public void createWindow() {
window.setLayout(null);
scroll.setBounds(20, 100, 320, 270);
window.add(scroll);
window.setSize(500, 500);
window.setVisible(true);
}
public static void main(String[] args) {
new WindowTest().createWindow();
}
}

JScrollPane not scrolling in a MigLayout

In this code I am trying to insert a JScrollPane into my panel which is using the MigLayout.
import java.awt.*;
import javax.swing.*;
import net.miginfocom.swing.MigLayout;
public class Simple2
{
JFrame simpleWindow = new JFrame("Simple MCVE");
JPanel simplePanel = new JPanel();
JLabel lblTitle;
JLabel lblSimple;
JTextArea txtSimple;
JScrollPane spSimple;
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
String []fontFamilies = ge.getAvailableFontFamilyNames();
public void numberConvertGUI()
{
simpleWindow.setBounds(10, 10, 285, 170);
simpleWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
simpleWindow.setLayout(new GridLayout(1,1));
createSimplePanel();
simpleWindow.getContentPane().add(simplePanel);
simpleWindow.setVisible(true);
simpleWindow.setResizable(false);
}
public void createSimplePanel()
{
MigLayout layout = new MigLayout("" , "[][grow]");
simplePanel.setLayout(layout);
lblTitle = new JLabel();
lblTitle.setText("This is a Title");
simplePanel.add(lblTitle, "wrap, align center,span 2");
lblSimple = new JLabel();
lblSimple.setText("Next to me is a JTextArea: ");
simplePanel.add(lblSimple);
txtSimple = new JTextArea();
txtSimple.setLineWrap(false);
txtSimple.setWrapStyleWord(true);
spSimple = new JScrollPane(txtSimple);
simplePanel.add(txtSimple,"width 100:100:100 , height 100:100:100");
}
public static void main(String[] args)
{
Simple2 s = new Simple2();
s.numberConvertGUI();
}
}
However when the text gets to the end of the JTextArea and continues off the screen there are no scroll bars horizontally or vertically. I am unsure of what I am doing wrong.
With txtSimple.setLineWrap(false);
With txtSimple.setLineWrap(true);
Edit
My desired outcome is to have scroll bars on the scroll pane
The code which provided these samples is
import java.awt.*;
import javax.swing.*;
import java.lang.Object.*;
import javax.swing.event.*;
import javax.swing.text.*;
import java.awt.event.*;
import java.awt.Checkbox;
public class TextAreaSample extends JFrame implements ActionListener
{
JFrame myMainWindow = new JFrame("This is my title");
JTabbedPane myTabs = new JTabbedPane();
JPanel firstPanel = new JPanel(); //a panel for first tab
//first panel components
JTextArea txtSimple;
JLabel lblSimple;
JScrollPane myScrollTable;
JCheckBox TextWrap;
//end first panel
public void runGUI()
{
myMainWindow.setBounds(10, 10, 800, 800); //set position, then dimensions
myMainWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myMainWindow.setLayout(new GridLayout(1,1));
createFirstPanel(); //call method to create each panel
myMainWindow.getContentPane().add(firstPanel); //adds the tabbedpane to mainWindow
myMainWindow.setVisible(true); //make the GUI appear
}
public void createFirstPanel()
{
firstPanel.setLayout(null);
txtSimple = new JTextArea();
txtSimple.setLineWrap(false);
txtSimple.setWrapStyleWord(true);
myScrollTable = new JScrollPane(txtSimple);
myScrollTable.setSize(700,700);
myScrollTable.setLocation(20,20);
firstPanel.add(myScrollTable);
System.out.println("Creating compare table");
lblSimple = new JLabel();
lblSimple.setText("Text Wrap");
lblSimple.setSize(100,25);
lblSimple.setLocation(20,730);
lblSimple.setHorizontalAlignment(JLabel.RIGHT);
firstPanel.add(lblSimple);
TextWrap = new JCheckBox();
TextWrap.setLocation(125,730);
TextWrap.setSize(25,25);
TextWrap.addActionListener(this);
firstPanel.add(TextWrap);
}
public void actionPerformed(ActionEvent e)
{
if(TextWrap.isSelected())
{
txtSimple.setLineWrap(true);
}
else
{
txtSimple.setLineWrap(false);
}
}
public static void main(String[] args)
{
TextAreaSample TSA = new TextAreaSample();
TSA.runGUI();
}
}
Here's the issue:
spSimple = new JScrollPane(txtSimple);
simplePanel.add(txtSimple,"width 100:100:100 , height 100:100:100");
You're not adding the JScrollPane to the layout. You need this:
spSimple = new JScrollPane(txtSimple);
simplePanel.add(spSimple,"width 100:100:100 , height 100:100:100");
Notice the use of spSimple in the second line.

How to put JOutlookBar into a JPanel in Java

How can I put a JOutlookBar into a JPanel?
Here is my code:
JFrame frame = new JFrame("JOutlookBar Test");
JOutlookBar outlookBar = new JOutlookBar();
outlookBar.addBar("One", getDummyPanel1("one"));
outlookBar.addBar("Two", getDummyPanel2("Two"));
outlookBar.addBar("Three", getDummyPanel3("Three"));
outlookBar.addBar("Four", getDummyPanel4("Four"));
outlookBar.addBar("Five", getDummyPanel5("Five"));
outlookBar.setVisibleBar(0);
frame.getContentPane().add(outlookBar);
frame.setSize(800, 600);
Adding frame.setVisible(true); allowed me to view your code working correctly.
Below is full working example of the code you provided, copy and paste it into a file named Library.java and it'll be grand!
import java.awt.BorderLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
public class Library {
public static void main(String[] args) {
JFrame frame = new JFrame("JOutlookBar Test");
JPanel left = new JPanel();
JPanel center = new JPanel();
JOutlookBar outlookBar = new JOutlookBar();
outlookBar.addBar("One", getDummyPanel1("one"));
outlookBar.addBar("Two", getDummyPanel1("Two"));
outlookBar.addBar("Three", getDummyPanel1("Three"));
outlookBar.addBar("Four", getDummyPanel1("Four"));
outlookBar.addBar("Five", getDummyPanel1("Five"));
outlookBar.setVisibleBar(0);
//Add outlookbar to left panel
left.add(outlookBar);
//Add some content to center panel...
center.add(new JTextArea());
//Add left and center panels with layout styles
frame.setLayout(new BorderLayout());
frame.add(left, BorderLayout.WEST);
frame.add(center, BorderLayout.CENTER);
frame.setSize(800, 600);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
//Show JFrame
frame.setVisible(true);
}
public static JPanel getDummyPanel1(String num) {
JPanel panel = new JPanel();
panel.add(new JButton("num"));
return panel;
}
}

Categories