I created two java classes one of which is my main class main.java
and other is supporting class which creates a panel with some added components CPanel.java
. The layout of my application is as follows :
Main class creates a main panel with BoxLayout and add a toolbar to the top and then creates a CPanel object(a panel with added components) and add it to the main panel. The CPanel object also have the BoxLayout for its components. The problem is, the CPanel panel is not incorporating the BoxLayout instead it just stick to flow layout. Here is the code of my classes..
public class MainFile extends JFrame {
private JToolBar navbar ;
private JButton backBtn, forwardBtn, homeBtn ;
private CPanel content ;
private static JPanel app = new JPanel() ;
public MainFile(){
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
navbar = new JToolBar() ;
navbar.setMaximumSize(new Dimension(1000, 50));
setSize(500, 800) ;
app.setLayout(new BoxLayout(app, BoxLayout.PAGE_AXIS));
app.add(navbar , BorderLayout.NORTH ) ;
ImageIcon leftButtonIcon = createImageIcon("images/right.gif");
ImageIcon middleButtonIcon = createImageIcon("images/middle.gif");
ImageIcon rightButtonIcon = createImageIcon("images/left.gif");
backBtn = new JButton(leftButtonIcon) ;
forwardBtn = new JButton(rightButtonIcon) ;
homeBtn = new JButton(middleButtonIcon) ;
navbar.add(forwardBtn) ;
navbar.add(Box.createGlue());
navbar.add(homeBtn) ;
navbar.add(Box.createGlue());
navbar.add(backBtn) ;
content = new CPanel() ;
app.add(content) ;
setContentPane(app) ;
}
protected static ImageIcon createImageIcon(String path) {
java.net.URL imgURL = MFrame.class.getResource(path);
if (imgURL != null) {
return new ImageIcon(imgURL);
} else {
System.err.println("Couldn't find file: " + path);
return null;
}
}
public static void showGUI(){
MFrame Reader = new MFrame() ;
//Reader.pack();
//Reader.setContentPane(app);
Reader.setVisible(true) ;
}
public static void main(String args[]){
SwingUtilities.invokeLater(new Runnable(){
@Override
public void run(){
showGUI() ;
}
});
}
}
Here is my CPanel Class
public class CPanel extends JPanel {
private JScrollPane scrollPane ;
private JPanel content ;
private JEditorPane demo ;
public CPanel(){
scrollPane = new JScrollPane() ;
content = new JPanel() ;
scrollPane.setViewportView(content);
content.setLayout(new BoxLayout(content, BoxLayout.Y_AXIS));
try{
java.net.URL text = CPanel.class.getResource("demo.txt") ;
demo = new JEditorPane(text) ;
}catch(Exception e){
System.out.println("Something bad happened with the file") ;
}
add(demo) ;
JButton demob = new JButton("Button 1") ;
JButton demob2 = new JButton("Button 2") ;
add(demob) ;
add(demob2) ;
}
}
It seems that you want the CPanel
to have a JScrollPane
, which appears as a component with BoxLayout
. You were right in creating a JPanel
, setting its layout and adding it to the JScrollPane
, but you still need to add the JScrollPane
to your CPanel
and the buttons and demo
to content
public class CPanel extends JPanel {
private JScrollPane scrollPane ;
private JPanel content ;
private JEditorPane demo ;
public CPanel(){
scrollPane = new JScrollPane() ;
content = new JPanel() ;
scrollPane.setViewportView(content);
content.setLayout(new BoxLayout(content, BoxLayout.Y_AXIS));
try{
java.net.URL text = CPanel.class.getResource("demo.txt") ;
demo = new JEditorPane(text) ;
}catch(Exception e){
System.out.println("Something bad happened with the file") ;
}
//These need to be added to the contentpanel
content.add(demo) ;
JButton demob = new JButton("Button 1") ;
JButton demob2 = new JButton("Button 2") ;
content.add(demob) ;
content.add(demob2) ;
//Here we need to add the scrollPane, to which the JPanel
//with BoxLayout has been added
this.setLayout(new BorderLayout());
this.add(scrollPane, BorderLayout.CENTER);
}
}