Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to add swing components that are outside of main file?
    primarykey
    data
    text
    <p>Example of my problem:</p> <p>I have a main file:</p> <pre><code>public class APP extends JFrame { private Dimension screen = Toolkit.getDefaultToolkit().getScreenSize(); public APP() { setLayout(new BorderLayout()); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setJMenuBar(new MenuBar()); JPanel content = new JPanel(new GridLayout(0, 8, 2, 2)); add(new JScrollPane(content, 22, 32), BorderLayout.CENTER); pack(); setLocationByPlatform(true); setResizable(false); setVisible(true); } public Dimension getPreferredSize() { return new Dimension(screen.width / 10 * 7, screen.height / 10 * 6); } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { public void run() { APP program = new APP(); } }); } } </code></pre> <p>And I have an external object that I'm trying to add as a JMenuBar:</p> <pre><code>public class MenuBar extends JMenuBar { public MenuBar() { JMenu file = new JMenu("File"); file.setMnemonic(KeyEvent.VK_F); add(file); JMenuItem item; item = new JMenuItem("Add New"); item.setMnemonic(KeyEvent.VK_N); item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N, ActionEvent.ALT_MASK)); item.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { //createThumb(); } }); file.add(item); } } </code></pre> <p>However, my menu bar doesn't show up at all. When I create a JMenuBar function inside the main file, such as... createMenuBar() and have the same exact code within it, it shows up when I add it to the frame, but when I have it as an external object, it doesn't.</p> <p>What am I doing wrong?</p> <p><strong>EDIT:</strong> Fixed the error. Refer to the code above.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload