• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

controlling size of part of split pane and another error

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am new to Swing.

I've got two problems in the code below javascript: x()
banghead
1)for some reason, the whiteboard is showing up as just a tiny sliver on the left hand side.
2)when I expand the whiteboard panel and start drawing on it, the label and the border for that component get duplicated.

This code should be ready to run (MainPanel is the executable). The only prerequisite is to have some hello.gif in the same folder.



import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.DefaultListModel;
import javax.swing.JCheckBox;
import javax.swing.JColorChooser;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JMenuBar;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.JTree;
import javax.swing.ListSelectionModel;
import javax.swing.table.DefaultTableModel;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreeSelectionModel;

public class MainPanel extends JPanel implements ActionListener {
static JFrame frame;

JTextArea textArea;

JTextField textField;

JList list;

JTable table;

JTree tree;

JColorChooser colorChooser;

JCheckBox toggleDnD;

WebToolBar webToolBar;

private WebBrowserPane browserPane;

private SimpleWhiteboard whiteBoard;

public MainPanel() {
super(new BorderLayout());
JPanel rightPanel = createVerticalBoxPanel();
JPanel leftPanel = createVerticalBoxPanel();
JPanel topPanel = createTopPanel();

// top
browserPane = new WebBrowserPane();
webToolBar = new WebToolBar(browserPane);
topPanel.add(createPanelForComponent(webToolBar, "WebToolBar"));

// RIGHT COLUMN
// Create a color chooser.
colorChooser = new JColorChooser();
rightPanel.add(createPanelForComponent(colorChooser, "JColorChooser"));

// LEFT COLUMN
// Create a textfield.
whiteBoard = new SimpleWhiteboard();
leftPanel.add(createPanelForComponent(whiteBoard, "SimpleWhiteboard"));
// Create the toggle button.
toggleDnD = new JCheckBox("Turn on Drag and Drop");
toggleDnD.setActionCommand("toggleDnD");
toggleDnD.addActionListener(this);

JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,
leftPanel, rightPanel);
splitPane.setOneTouchExpandable(true);
add(topPanel, BorderLayout.NORTH);
add(splitPane, BorderLayout.CENTER);
add(toggleDnD, BorderLayout.PAGE_END);
setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
}

protected JPanel createVerticalBoxPanel() {
JPanel p = new JPanel();
p.setLayout(new BoxLayout(p, BoxLayout.PAGE_AXIS));
p.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
return p;
}

protected JPanel createTopPanel() {
JPanel p = new JPanel();
p.setLayout(new BoxLayout(p, BoxLayout.PAGE_AXIS));
p.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
return p;
}

public JPanel createPanelForComponent(JComponent comp, String title) {
JPanel panel = new JPanel(new BorderLayout());
panel.add(comp, BorderLayout.CENTER);
if (title != null) {
panel.setBorder(BorderFactory.createTitledBorder(title));
}
return panel;
}

public void actionPerformed(ActionEvent e) {
if ("toggleDnD".equals(e.getActionCommand())) {
boolean toggle = toggleDnD.isSelected();
textArea.setDragEnabled(toggle);
textField.setDragEnabled(toggle);
list.setDragEnabled(toggle);
table.setDragEnabled(toggle);
tree.setDragEnabled(toggle);
colorChooser.setDragEnabled(toggle);
}
}

/**
* Create the GUI and show it. For thread safety, this method should be
* invoked from the event-dispatching thread.
*/
private static void createAndShowGUI() {
// Make sure we have nice window decorations.
JFrame.setDefaultLookAndFeelDecorated(true);

// Create and set up the window.
frame = new JFrame("BasicDnD");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

// Create and set up the content pane.
JComponent newContentPane = new MainPanel();
newContentPane.setOpaque(true); // content panes must be opaque
frame.setContentPane(newContentPane);

// Display the window.
frame.pack();
frame.setVisible(true);
}

public static void main(String[] args) {
// Schedule a job for the event-dispatching thread:
// creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}



-------------------------------------


import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class SimpleWhiteboard extends JPanel {

private Point oldPoint, newPoint;
protected ImageIcon image;
private boolean start = true;

// set up GUI and register mouse event handler
public SimpleWhiteboard() {
// super( "A simple whiteboard program" );
setBackground( Color.WHITE );
image = new ImageIcon( getClass().getResource( "hello.gif" ) );

addMouseMotionListener(

new MouseMotionAdapter() { // anonymous inner class

// store drag coordinates and repaint
public void mouseDragged( MouseEvent event ) {

if ( start ) {
newPoint = event.getPoint();
start = false;
}
else {
oldPoint = newPoint;
newPoint = event.getPoint();
repaint();
}
}

public void mouseMoved( MouseEvent event ) {
start = true;
}
} // end anonymous inner class

); // end call to addMouseMotionListener

setSize( 1200, 700 );
setVisible( true );

} // end Painter constructor

// draw oval in a 4-by-4 bounding box at specified location on window
public void paint( Graphics g ) {
if ( start ) {
image.paintIcon( this, g, 0, 0 );
}
else
g.drawLine( oldPoint.x, oldPoint.y, newPoint.x, newPoint.y );
}

public static void main( String args[] ) {
SimpleWhiteboard application = new SimpleWhiteboard();
// application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
}

} // end class Painter


------------------------------------



// WebBrowserPane.java
// WebBrowserPane is a simple Web-browsing component that
// extends JEditorPane and maintains a history of visited URLs.


// Java core packages
import java.util.*;
import java.net.*;
import java.io.*;

// Java extension packages
import javax.swing.*;
import javax.swing.event.*;

public class WebBrowserPane extends JEditorPane {

private List history = new ArrayList();
private int historyIndex;

// WebBrowserPane constructor
public WebBrowserPane()
{
// disable editing to enable hyperlinks
setEditable( false );
}

// display given URL and add it to history
public void goToURL( URL url )
{
displayPage( url );
history.add( url );
historyIndex = history.size() - 1;
}

// display next history URL in editorPane
public URL forward()
{
historyIndex++;

// do not go past end of history
if ( historyIndex >= history.size() )
historyIndex = history.size() - 1;

URL url = ( URL ) history.get( historyIndex );
displayPage( url );

return url;
}

// display previous history URL in editorPane
public URL back()
{
historyIndex--;

// do not go past beginning of history
if ( historyIndex < 0 )
historyIndex = 0;

// display previous URL
URL url = ( URL ) history.get( historyIndex );
displayPage( url );

return url;
}

// display given URL in JEditorPane
private void displayPage( URL pageURL )
{
// display URL
try {
setPage( pageURL );
}

// handle exception reading from URL
catch ( IOException ioException ) {
ioException.printStackTrace();
}
}
}




-----------------------------




// WebToolBar.java
// WebToolBar is a JToolBar subclass that contains components
// for navigating a WebBrowserPane. WebToolBar includes back
// and forward buttons and a text field for entering URLs.


// Java core packages
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.MalformedURLException;
import java.net.URL;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JTextField;
import javax.swing.JToolBar;
import javax.swing.event.HyperlinkEvent;
import javax.swing.event.HyperlinkListener;

public class WebToolBar extends JToolBar
implements HyperlinkListener {

private WebBrowserPane webBrowserPane;
private JButton backButton;
private JButton forwardButton;
private JTextField urlTextField;

// WebToolBar constructor
public WebToolBar( WebBrowserPane browser )
{
super( "Web Navigation" );

// register for HyperlinkEvents
webBrowserPane = browser;
webBrowserPane.addHyperlinkListener( this );

// create JTextField for entering URLs
urlTextField = new JTextField( 25 );
urlTextField.addActionListener(
new ActionListener() {

// navigate webBrowser to user-entered URL
public void actionPerformed( ActionEvent event )
{
// attempt to load URL in webBrowserPane
try {
URL url = new URL( urlTextField.getText() );
webBrowserPane.goToURL( url );
}

// handle invalid URL
catch ( MalformedURLException urlException ) {
urlException.printStackTrace();
}
}
}
);

// create JButton for navigating to previous history URL
backButton = new JButton( new ImageIcon(
getClass().getResource( "images/back.gif" ) ) );

backButton.addActionListener(
new ActionListener() {

public void actionPerformed( ActionEvent event )
{
// navigate to previous URL
URL url = webBrowserPane.back();

// display URL in urlTextField
urlTextField.setText( url.toString() );
}
}
);

// create JButton for navigating to next history URL
forwardButton = new JButton( new ImageIcon(
getClass().getResource( "images/forward.gif" ) ) );

forwardButton.addActionListener(
new ActionListener() {

public void actionPerformed( ActionEvent event )
{
// navigate to next URL
URL url = webBrowserPane.forward();

// display new URL in urlTextField
urlTextField.setText( url.toString() );
}
}
);

// add JButtons and JTextField to WebToolBar
add( backButton );
add( forwardButton );
add( urlTextField );

} // end WebToolBar constructor

// listen for HyperlinkEvents in WebBrowserPane
public void hyperlinkUpdate( HyperlinkEvent event )
{
// if hyperlink was activated, go to hyperlink's URL
if ( event.getEventType() ==
HyperlinkEvent.EventType.ACTIVATED ) {

// get URL from HyperlinkEvent
URL url = event.getURL();

// navigate to URL and display URL in urlTextField
webBrowserPane.goToURL( url );
urlTextField.setText( url.toString() );
}
}
}
 
Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try this implementation of SimpleWhiteboard. Seems to work okay now.
 
Alex Turo
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks, so much, that was very helpful.
I implemented your suggestion and it worked!
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic