Advertisement

There's no such thing as a stupid question, but they're the easiest to answer.
Login
Search

Advertisement

Search Search
Search for:
Tech Support Guy > > >

Java Programming Question Eclipse


(!)

Simmers's Avatar
Simmers Simmers is offline
Computer Specs
Member with 2 posts.
THREAD STARTER
 
Join Date: Sep 2007
Experience: Intermediate
06-Aug-2012, 10:04 AM #1
Java Programming Question Eclipse
Hello,

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/tutori...abbedpane.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);

mainFrame.setVisible(true);

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.
add(tabbedPane);
//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);
filler.setHorizontalAlignment(JLabel.CENTER);
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!
aranea's Avatar
aranea aranea is offline
Computer Specs
Member with 6 posts.
 
Join Date: Aug 2012
Experience: Advanced
19-Aug-2012, 06:13 AM #2
Quote:
how would I change the color of a panel or button of a GUI window?
All descendands of JComponent have a method called setBackground() that you may use to set a new bacground color to a copmponent.
Example:
Code:
    btOK = new JButton("OK");
    btOK.setBackground(Color.CYAN);
Quote:
How can I add a tabbed pane to the program I have?
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.
Example:
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.
Code:
    form = new JForm();
    form.setSize(new Dimension(400,400));
    form.setLayout(new BorderLayout());
    form.getContentPane.add(my_tabbed_pane,BorderLayout.CENTER);
    form.setVisible(true);
Quote:
Would I need to create a new class for each?
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.


Quote:
Or would I add to the existing program I want to add to and insert the code there?
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

BBC, Reader's Digest, PC Magazine, Today Show, Money Magazine
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.


(clock)
THIS THREAD HAS EXPIRED.
Are you having the same problem? We have volunteers ready to answer your question, but first you'll have to join for free. Need help getting started? Check out our Welcome Guide.

Search Tech Support Guy

Find the solution to your
computer problem!




Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools


WELCOME
You Are Using: Server ID
Trusted Website Back to the Top ↑