1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

Java Programming Question Eclipse

Discussion in 'Software Development' started by Simmers, Aug 6, 2012.

Thread Status:
Not open for further replies.
  1. Simmers

    Simmers Thread Starter

    Sep 28, 2007

    I am a beginner programmer and have Eclipse Java EE IDE for Web Developers (Version: Juno Release), I have a Windows 7 Home Premium PC.

    My question is in Java using Eclipse, how would I change the color of a panel or button of a GUI window? How can I add a tabbed pane to the program I have? Would I need to create a new class for each? Would I need to create a new program and link it? Would I need to create a new class and then package it somehow? Or would I add to the existing program I want to add to and insert the code there?

    I have this link: http://docs.oracle.com/javase/tutorial/uiswing/components/tabbedpane.html but I don't know how I would add this to my current existing program?

    In the current code I have - the GUI frame is already coded and set to visible (shown below) so to change the panel color - would I just add "mainFrame.setColor(blue);"

    Here's what's already there:
    mainFrame.setSize(260, 150);


    Also to add a tab - would I need to just add the following to the current program:

    JTabbedPane tabbedPane = new JTabbedPane();
    ImageIcon icon = createImageIcon("images/middle.gif");
    JComponent panel1 = makeTextPanel("Panel #1");
    tabbedPane.addTab("Tab 1", icon, panel1, "Does nothing");
    tabbedPane.setMnemonicAt(0, KeyEvent.VK_1);
    JComponent panel2 = makeTextPanel("Panel #2");
    tabbedPane.addTab("Tab 2", icon, panel2, "Does twice as much nothing");
    tabbedPane.setMnemonicAt(1, KeyEvent.VK_2);

    //Add the tabbed pane to this panel.
    //The following line enables to use scrolling tabs.
    tabbedPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT); }
    protected JComponent makeTextPanel(String text) {
    JPanel panel = new JPanel(false);
    JLabel filler = new JLabel(text);
    panel.setLayout(new GridLayout(1, 1));
    panel.add(filler); return panel; }
    /** Returns an ImageIcon, or null if the path was invalid. */
    protected static ImageIcon createImageIcon(String path) {
    java.net.URL imgURL = TabbedPaneDemo.class.getResource(path);
    if (imgURL != null) {
    return new ImageIcon(imgURL);
    } else {
    System.err.println("Couldn't find file: " + path);
    return null; } }

    Thank you in advance for any help - much appreciated!
  2. aranea


    Aug 16, 2012
    All descendands of JComponent have a method called setBackground() that you may use to set a new bacground color to a copmponent.
        btOK = new JButton("OK");
    Typically you need an area on the screen controles by your program. In your case I think you should create a JForm, wich is a window on the screen. The form has a ContentPane where you can add components like a tabbed pane. Maybe you should also add a LayoutManager to your form.
    Let's say you have the tabbed pane created. If it's nothing but a tabbed pane you won't see anything. You need to add named tabs to it. And you nbeed to add components to its different tab panes in order to see it working.
        form = new JForm();
        form.setSize(new Dimension(400,400));
        form.setLayout(new BorderLayout());
    No, there is no need for that. But if its function is not much related to the rest of the progam or it needs much code to work, it is usually better to separate it in its own class.

    When you subclass a pane you will probably add some extra components, colors or other features to it in the constructor. Then you can concentrate on creating the panel just by calling the constructor and all the other components will be created with the panel. You may, of course, choose to create the panel by calling the JPanel constructor and then add all components, colors and what else to it from the calling program.
As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 733,556 other people just like you!

Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/1064049