Original Post
🔒 Locked
[java] gui help
Advertisement
quote:
Original post by phaelax
Thx, that's exactly what I wanted. Is there a way to prevent the user from editing the fields? I checked the documentation, but couldn't find anything that suggested that.
You have to create a TreeModel that takes care of that... I think the method you want to override is
public boolean isEditable(int col,int row){
return false;
}
something like that should do it....
[edited by - garazdawi on March 3, 2004 11:55:00 AM]
Ok I got another question.
I have several panels(info). Each panel has a JLabel followed by a JTextField. All these panels are then added to another panel (bottom)using a GridLayout. I put all the labels and textfields into seperate panels to keep the textfield size. How do I align these info panels inside the bottom panel so that they are left-aligned instead of all of them being centered?
I have several panels(info). Each panel has a JLabel followed by a JTextField. All these panels are then added to another panel (bottom)using a GridLayout. I put all the labels and textfields into seperate panels to keep the textfield size. How do I align these info panels inside the bottom panel so that they are left-aligned instead of all of them being centered?
Here''s the source if that''ll help you get an idea what I''m talking about.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
//import javax.swing.JTable;
//import javax.swing.JScrollPane;
//import java.awt.Dimension;
import javax.swing.border.EtchedBorder;
import javax.swing.border.TitledBorder;
class MyFrame extends JFrame
{
public MyFrame()
{
super("MP3 Database v.0.1");
//Color used for background highlighting
Color colorBlue = new Color(61,128,223);
// Side navigation data
String[] list = {"My Library", "two", "three", "four", "five", "six", "seven"};
// Dummy test data to represent a mp3 list
Object[][] data = {
{"David Bowie", "Greatest Hits","China Girl", new Double(4.38), "rock"},
{"David Bowie", "Greatest Hits","Space Oddity", new Double(6.53), "rock"},
{"Metallica", "Black", "Unforgiven", new Double(5.42), "metal"},
{"KMFDM", "Symbols", "Megalomaniac", new Double(5.16), "Industrial"},
{"David Bowie", "Greatest Hits","China Girl", new Double(4.38), "rock"},
{"David Bowie", "Greatest Hits","Space Oddity", new Double(6.53), "rock"},
{"Metallica", "Black", "Unforgiven", new Double(5.42), "metal"},
{"KMFDM", "Symbols", "Megalomaniac", new Double(5.16), "Industrial"},
{"David Bowie", "Greatest Hits","China Girl", new Double(4.38), "rock"},
{"David Bowie", "Greatest Hits","Space Oddity", new Double(6.53), "rock"},
{"Metallica", "Black", "Unforgiven", new Double(5.42), "metal"},
{"KMFDM", "Symbols", "Megalomaniac", new Double(5.16), "Industrial"}};
//Column names
String[] columnNames = {"Artist",
"Album",
"Song Title",
"Track Length",
"Genre"};
//Navigation list
JList folderList = new JList(list);
folderList.setSelectionBackground(colorBlue);
folderList.setSelectionForeground(Color.white);
//Table to display mp3 file info
JTable table = new JTable(data, columnNames);
table.setPreferredScrollableViewportSize(new Dimension(800,200));
table.setShowVerticalLines(false);
table.setSelectionMode(0);
table.setSelectionBackground(colorBlue);
table.setSelectionForeground(Color.white);
//Add navigation list and file info table to scroll panes
JScrollPane mp3ListScrollPane = new JScrollPane(table);
JScrollPane fileListScrollPane = new JScrollPane(folderList);
//Set minimum sizes for split pane
fileListScrollPane.setMinimumSize(new Dimension(40, 100));
mp3ListScrollPane.setMinimumSize(new Dimension(40, 100));
//Setup split pane
JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, fileListScrollPane, mp3ListScrollPane);
splitPane.setOneTouchExpandable(true);
splitPane.setDividerLocation(120);
splitPane.setContinuousLayout(true);
//create a new panel which will contain the options for editing tag info
JPanel tagPanel = new JPanel();
tagPanel.setLayout(new GridLayout(6,1));
tagPanel.setBorder(new TitledBorder(new EtchedBorder(), "ID3 Tag", 2, 0));
//create artist panel
JPanel artistPanel = new JPanel();
//create Artist label
JLabel artistLabel = new JLabel("Artist: ");
//create Artist text field
JTextField artistField = new JTextField("David Bowie", 30);
//add Artist label and text field to the album panel
artistPanel.add(artistLabel);
artistPanel.add(artistField);
//add artist panel to tag panel
tagPanel.add(artistPanel);
//create Album panel
JPanel albumPanel = new JPanel();
//create Album label
JLabel albumLabel = new JLabel("Album: ");
//create Album text field
JTextField albumField = new JTextField("Greatest Hits", 30);
//add Album label and text field to the album panel
albumPanel.add(albumLabel);
albumPanel.add(albumField);
//add album panel to tag panel
tagPanel.add(albumPanel);
//create song title panel
JPanel songTitlePanel = new JPanel();
//create song title label
JLabel songTitleLabel = new JLabel("Song Title: ");
//create song title text field
JTextField songTitleField = new JTextField("China Girl", 30);
//add song title label and text field to the song Title Panel
songTitlePanel.add(songTitleLabel);
songTitlePanel.add(songTitleField);
tagPanel.add(songTitlePanel);
//create year panel
JPanel yearPanel = new JPanel();
//create year label
JLabel yearLabel = new JLabel("Year: ");
//create year text field
JTextField yearField = new JTextField("2003", 4);
//add year label and text field to the year Panel
yearPanel.add(yearLabel);
yearPanel.add(yearField);
tagPanel.add(yearPanel);
//create comment panel
JPanel commentPanel = new JPanel();
//create comment label
JLabel commentLabel = new JLabel("Comment: ");
//create comment text field
JTextField commentField = new JTextField("No comment available", 30);
//add comment label and text field to commentPanel
commentPanel.add(commentLabel);
commentPanel.add(commentField);
//create genre panel
JPanel genrePanel = new JPanel();
//create genre label
JLabel genreLabel = new JLabel("Genre: ");
//create genre text field
JTextField genreField = new JTextField("Rock", 4);
//add genre label and text field to genrePanel
genrePanel.add(genreLabel);
genrePanel.add(genreField);
tagPanel.add(genrePanel);
//create menu bar
JMenuBar menuBar = new JMenuBar();
menuBar.add(createFileMenu());
menuBar.add(createOptionsMenu());
menuBar.add(createHelpMenu());
//add split pane and tag panel to this Frame
getContentPane().add(splitPane, BorderLayout.CENTER);
getContentPane().add(tagPanel, BorderLayout.SOUTH);
getContentPane().add(menuBar, BorderLayout.NORTH);
//set default app size
setSize(800,500);
//set default location
setLocation(10,200);
// Window Listeners
addWindowListener( new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}//windowClosing
} );
}
public static void main(String[] args)
{
//JFrame f = new MyFrame();
MyFrame f = new MyFrame();
f.show();
}
public JMenu createFileMenu()
{
JMenu menu = new JMenu("File");
menu.add("Exit");
return menu;
}
public JMenu createOptionsMenu()
{
JMenu menu = new JMenu("Options");
menu.add("Directories");
menu.add("Colors");
return menu;
}
public JMenu createHelpMenu()
{
JMenu menu = new JMenu("Help");
menu.add("Using MP3 Database");
menu.addSeparator();
menu.add("About MP3 Database");
return menu;
}
}
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement